-->
Search here and hit enter....

Array in C language ( Part 1 )

Hello Viewer! How are you I hope you well? In the previous lesson, we learn  Loop Control Instruction in C language. in this lesson we'll learn Array in C language PART 1...

The C language provides a facility to user/programmer to design a set of similar data types, called array. In this post, we discussed array briefly...
Understand the below program

#include
int main()
{
 int x=5;
 int x=10;
 printf("\nX = &d", x );
 return 0;


The output of the above program is 10. Why so? because when the value of x is assigned 10 then past value of x means 5 is lost, Thus ordinary variable is capable of holding only one value at a time. for example, suppose we want to arrange the percentage marks obtained by 100 students in ascending order. in such a situation, we have two option to store their marks in memory.

1) Construct 100 variable to store percentage marks obtained by 100 students, i.e. each variable containing one student's marks.
2) Construct One variable ( called array or subscripted variable ) capable of storing all 100 student marks.

Obviously, the second alternative is better. A simple reason for this is, it would be easier to handle one variable compared to hundred variable. Now a definition of Array - An array is a collection of similar elements. what is important is that the "quantities" must be "Similar". Each member in the group is referred to by its position in the group. Indexing of Array is always started with 0.
Understand with Example below... 

Suppose we need to store roll no of 5 students.

S= { 10, 20, 30, 40, 50 }
If we want to refer to the second roll of the student, the notation used is S[1]. similarly, we want to access 40 roll then the notation like S[3].
Thus an Array is a collection of similar elements. These similar elements could be all ints or floats or chars, etc.. Usually an array of character is called a "String". 

Understand with the simple example below...
W.A.P. to store & print marks of 5 students.
Program :

#include
int main()
{
 int x[5],i ;
 for( i=0; i<=4 ; i++ )
{
 printf("\nEnter Student Mark : ");
 scanf("%d", &x[i] );
 }
 for( i=0; i<=4 ; i++ )
 {
 printf("\nMarks is ", x [i]);
 return 0;


Array Declaration
To begin with, like other variables, an array needs to declared before it used in the program we have done this with the statement :

int marks [30];

Here int specifies the type of the variable, just as it does with ordinary variables and the word marks specifies the name of the variable. The 30 number tells how many elements of the type int be will be in our array. The Bracket" [ ] " tells the compiler that we are dealing with an Array.

Accessing Elements of an Array
Once an array is declared, let us see how individual elements in the array can be referred. This is done with a subscript, the number in the brackets following the array name. This number specifies the position of the elements in the array. All the array elements are numbered, starting with 0. Thus mark [2] is not the second element of the array.

Entering Data into an Array
Here the section of code that places data into array:

 for( i=0; i<=4 ; i++ )
{
 printf("\nEnter Student Mark : ");
 scanf("%d", &x[i] );
 }

The for loop causes the process of asking for and receiving a student's marks from the user repeated to the five times. the 1st time is through loop i has a value 0, so the scanf() function will cause the valve be typed to be stored in the array element x[ 0 ], the first element of the array. this process repeated until I become 4. this is last time through the loop, which is a good thing because there is no element like x [ 5 ].
In scanf( ) function, we have used the "address of ( & ) " operator on the elements x [ i ] of the array, just as we use it earlier on other variable . 


Reading Data from an Array
The balance of the program reads the data back out of the array and use it to calculate the marks. the for is much the same, but now the body of the loop causes each student's marks to be added to a running total stored in a variable. when all the marks have been added up.


Notes :
1) An Array is a collection of similar elements.
2) The first element in the array is numbered 0, so the last element is 1 less than the size of the array.
3) An Array is also known as "Subscripted Value"
4) Before using an array, its type and dimension must be declared.
5) However big an Array, its elements are always stored in contiguous memory locations.







In this part are so enough 4 today. next part we'll discussed in future post I hope you like it.

Thank You 4 reading my blog!  if you have any problem or issue with this post please comment/contact.

Important :
 In this post, Article/Paragraph is taken from Let Us C book that is written by Yashavant Kanetkar.

FB COMMENTS ()