TYPES OF ARRAYS IN C LANGUAGE

                             ARRAYS IN C LANGUAGE

In  this topic we will learn following words


1-dimensional and 2-dimensional in c language
1-dimensional and 2-dimensional in c language



                                              WHAT ARE ARRAYS
For understanding, the arrays properly ,let us consider the following program :

#include<stdio.h>
int main()
{
int x;
x=5;
x=10;
printf("x=%d\n",x);
return 0;
}

          No doubt,this program will print the value of x as 10. why so? because ,when a value 10 is assigned to x,the earlier value of x , i.e 5,is lost. Thus ,ordinary variables (the once which we have use  so far) are capable of holding only one value at time (as in this principle). However ,there are situation in which we would want to store more than one value at time in a single variable. 
         For example,suppose we wish to arrange percentage marks obtain by 100 students in ascending order. In a such case ,we have two option to store this marks in memory:
A) Construct 100 variables to store percentage marks obtained by 100 different students that is each variable containing one student mark.
B) Construct one variable (called array or sub scripted variable) capable of storing or holding all the 100 value.
Obviously,the second alternative is better. A simple reason for this is, it would be much easier to handle one variable. Moreover, there are certain logic that cannot be dealt with, without the use of an array. Now a formal definition of an array- An array is a collective name given to a group of 'similar quantities'. These similar quantities could be percentage marks of 100 students,or salaries of 300 employees, or ages of 50 employees. What is important is that the quantities must be 'similar'. Each  member in the group of numbers,which represent percentage marks marks obtained by five students.  

per={ 48,88,34,23,96 } 

if we want to refer to the second number of the group,the usual notation used is per.similarly the fourth number of group is referred as per.however,in c,the fourth number is referred as per.This is because n c,the counting of elements begin with 0 and not with 1.Thus,in this example per[3] refer to 23 and per[4] refer to 96 in general notation would be per[I],where,i can take a value 0,1,2,3,4 depending on the position of the element being referred .Here, per is the subscribed variable ,whereas i is its subscript 

Thus,an array is a collection of similar elements could be all ints, or all floats,or all chars etc.usually,the array of characters is called a 'strings' whereas an array of ints. or floats is called simply an array. Remember that all elements of any array must be of the same type.i.e,we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats.


                   A SIMPLE PROGRAM USING ARRAY

We will understand to find a average marks obtained by a class of 30 students in a test.

#include<stdio.h>
int main()
{
int avg, sum=0;
int i ;
int marks[30] ; /*array declaration*/
 
for(i=0 ; i<=29 ; i++)
{
printf("Enter marks");
scanf("%d",&marks[i] );  /* store data in array/*
}
for(i=0 ; i<=29 ; i++)
sum=sum+marks[i] ; /*read data from an array*/
avg=sum/30;
printf("average marks =%d\n",avg);
return 0 ;
}

                                 ARRAY DECLARATION

            To begin with, like other variables , an array needs to declared so that the compiler will know what kind of an array and how large an array we want. In our program, we have done this with the statement :

int marks[30] ;

Here , ints 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] however is new.The number 30 tells how many elements of the type int 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 subscript , the number in the bracket following the array name . This number specifies the element's position in the array. All  the array elements are numbered, starting with 0.Thus,marks [2] is not the 2nd element of the array,but the 3rd .In or program , we are using the variable I has a subscript to refer to various elements of the array.This variable can take different values and hence can refer to the different elements in the array in turn. This ability to use variables to represent subscripts in what makes arrays so useful.  


                     Pointers and arrays

               To be able to see what pointers have got to do with arrays, let us first learn some pointer arithmetic.Consider the following example:

#include<stdio.h>
int main()
int i=3*x ;
float j =1.5*y ;
char k = 'c', *z ;
printf("Value of i=%d\n",i);
printf("value of j =%f\n",j);
printf("Value of k=%c\n",k ) ;
x=&i ;
y= &j ;
z=&k ;
printf("Original address in x=%u\n",x ) ;
printf("Original addresses in y=%u\n",y) ;
printf("Original address in z= %u\n",z ) ;
x++ ;
y++ ;
z++ ;
printf("New address in x = %u\n",x ) ;
printf("New address in y=%u\n",y ) ;
printf("New address in z = %u\n",z ) ;
return 0 ;
}

HERE IS THE OUTPUT OF THE PROGRAM.

value of i=3
value of j=1.500000
value of k=c
original address in x =65524
original address in y = 65520
original address in z= 65519
New address in x =65528
New address in y =65524
New address in z =65520


Thus, the following operations can be performed on a pointer 

A) Addition of a number to a pointer.For example,

int i = 4, *j,*k ;
j = &i ;
j = j+1;
j = j+9 ;
k =  j+3 ;
 

B) Subtraction of a number from a pointer.For example,

int i = 4,*j,*k ;
j = &i ;
j = j-2 ;
j = j-5 ;
k = j-6 ;


C) Subtraction of one pointer from another.

              One pointer variable can be subtracted can be subtracted from another provided both variables to point to elements of the same array.The resulting value indicates the number of elements separating the corresponding array elements.This is illustrated in the following programs.

#include<stdio.h>
int main()
{
int arr[] = {10 , 20 , 30 , 45 , 67 , 56 , 74 } ;
int *i , *j ;
 i = &arr[1] ;
j = &arr[5] ;
 print("%d%d\n" , j-1 , *j-*i ) ;
return 0 ;
}

      Here i and j have been declared as integer pointers holding address to first and fifth element of the array respectively.

Suppose the array begins at location 65502,then the elements arr[1]and arr[5]would be present at location 65506 and 65522 is the respectively,since each integer in the array occupies two bytes in memory.The expression j-i would print a value for an not 8 . This is because j and i pointing to locations that are four integers a part but what will be the result of the expression *j-*i? 36,since *j*i written the values present at address contain in the pointers j and i.

D) Comparison of two pointer variables

                Pointer variables can be compared provided both variables point to objects of the same data type.Such comparisons can be useful when two objects of the same data type.Such comparison can be useful when both pointer variables point to elements of the same array. The comparison can test for either equality or inequality. Moreover, pointer variable can be compared with 0 (usually expressed as a NULL). The following program illustrates how the comparison is carried out. 

#include<stdio.h>
int main()
{
int arr[] = { 10 , 20 , 36 , 72 , 45 , 36 } ;
int *j , *k ;
j = &arr[4] ;   
k = ( arr+4) ;
if( j==k)
  printf("The two pointers 'point to the same location\n") ;
else
printf("The two pointers do not point to the same location\n" ) ;
 
return 0 ;
}
    
       A word of caution ! do not attempt the following operations on pointers...they would never work out

a) Addition of two pointers
b) Multiplication of a pointer with a constant.
c) Division of a pointer with a constant.
Now we will try to correlate the following two facts ,which we have learnt above 
a) Array elements are always stored in contiguous memory locations.
b) A pointer when incremented always points to an immediately next location of its type.


                       TWO DIMENSIONAL  ARRAYS

                   So far ,we have explored arrays with only one dimension. It is also possible for arrrays to have two or more dimensions. The two-dimensional  array is also called a matrix.

#include<stdio.h>
int main(
{
int stud[4][2] ;
int i ; j ;
for ( i = 0 ; i <= 3  ; i++ )
{
print(" Enter roll no and marks ") ;
scanf (" %d %d ", &stud[i][0] , &stud[i][1] ) ; 
}
for( i = 0 ; i <= 3 ; i++ )
printf ("%d %d\n", stud[i][0] , stud[i][1] ) ;
return 0 ;
}

     There are two parts to the program -in the first part ,through  for loop ,we read in the value of roll no. and marks ,whereas , in the second part through another for loop ,we print out these values.

Look at the scanf() statement used in the first for loop:

scanf("%d %d",&stud[i][1]) ;

In stud[i][0] and stud[i][1] , the first subscript of the variable stud, is row number which changes for every student. The second subscript tells which of the two columns are we talking about-the zeroth column which contains the marks. Remember the counting of rows and columns begin with zero. The complete array arrangement is shown below.

row                                                   col.no.0                                          col.no.1
 row no 0                        1234                                              56                                               
 row no 1    1212 33
 row no 2 1434 80
 row.no.3 1312 78

              Thus,1234 is stored in stud[0][0],56 is stored in stud[0][1] and so on.The above arrangement highlight the fact that a two-dimensional array is nothing but a collection of a number of one-dimensional arrays placed one below the other.
In our sample program, the array elements have been stored row-wise and accessed row-wise . However ,you can access the array elements column wise as well. Traditionally ,the array elements are being stored and accessed row-wise; therefore we would also stick to same strategy. 

Initializing a 2-dimension array.

int stud[4][2] ={
                             { 1234,56},
                             { 1212,33},
                             { 1434, 80},
                              { 1312,78}
                                      };
or even this would work.....

int stud[4][2]={1234,56,1212,33,1434,80,1312,78};

of course with a corresponding loss in readability.
It is important to remember that, while initializing a 2-D array ,it is necessary to mention the second dimension ,whereas the first dimension is optional.
Thus the declarations,

int arr[2][3]={12,34,23,45,56,45};
int arr[][3] ={12,34,23,45,56,45} ;

are perfectly acceptable,
whereas,

int arr[2][]={12,34,23,45,56,45};
int arr[][] ={12,34,23,45,56,45} ;
would never work.

Previous
Next Post »
}; //]]>