Sunday, September 16, 2012

Arrays in C

Array is a collection of homogenous data stored under unique name. The values in an array is called as 'elements of an array.' These elements are accessed by numbers called as 'subscripts or index numbers.' Arrays may be of any variable type.
Array is also called as 'sub scripted variable.'

Types of an Array :

1. One / Single Dimensional Array
2. Two Dimensional Array


Single / One Dimensional Array :
The array which is used to represent and store data in a linear form is called as 'single or one dimensional array.'
Declaration of one-dimensional array
data_type array_name[array_size];
For example,
int age[5];
Array elements
Size of array defines the number of elements in an array. Each element of array can be accessed and used by user according to the need of program.
 For example:     int age[5];



Note that, the first element is numbered 0 and so  on.
Here, the size of array age is 5 times the size of int because there are 5 elements.
Suppose, the starting address of age[0] is 2120d and the size of int be 4 bytes. Then, the next address (address of a[1]) will be 2124d, address of a[2] will be 2128d and so on.

Initialization:
Arrays can be initialized at declaration time in  this source code as,
int age[5]={2,4,34,3,4};
It is not necessary to define the size of arrays during initialization.
int age[]={2,4,34,3,4};
In this case, the compiler determines the size of array by calculating the number of elements of an array.


Accessing array elements:
In C programming, arrays can be accessed and treated like variables in C.
For example:
scanf("%d",&age[2]);
/* statement to insert value in the third element of array age[]. */

scanf("%d",&age[i]);
/* Statement to insert value in (i+1)th element of array age[]. */
/* Because, the first element of array is age[0], second is age[1], ith is age[i-1] and (i+1)th is age[i]. */

printf("%d",age[0]);
/* statement to print first element of an array. */

printf("%d",age[i]);
/* statement to print (i+1)th element of an array. */

Example program :

#include <stdio.h>
#include <conio.h>
int main(){
     int marks[10],i,n,sum=0;
     printf("Enter number of students: ");
     scanf("%d",&n);
     for(i=0;i<n;++i){
          printf("Enter marks of student%d: ",i+1);
          scanf("%d",&marks[i]);
          sum+=marks[i];
     }
     printf("Sum= %d",sum);
return 0;
}

output:
Enter number of students: 3
Enter marks of student1: 12
Enter marks of student2: 31
Enter marks of student3: 2
sum=45

Multidimensional Arrays:

The array which is used to represent and store data in a tabular form is called as 'two dimensional array.' Such type of array specially used to represent data in a matrix form.
The following syntax is used to represent two dimensional array.
<data-type> <array_nm> [row_subscript][column-subscript];

figure :

Initialization of Multidimensional Arrays:
In C, multidimensional arrays can be initialized in different number of ways.
int c[2][3]={{1,3,0}, {-1,5,9}};
                 OR
int c[][3]={{1,3,0}, {-1,5,9}};
                 OR
int c[2][3]={1,3,0,-1,5,9};

Initialization Of three-dimensional Array:

double c3d[3][2][4]={
{{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}},
 {{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}},
 {{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}}
};
Suppose there is a multidimensional array arr[i][j][k][m]. Then this array can hold i*j*k*m numbers of data.
Similarly, the array of any dimension can be initialized in C programming.

Example program 1 :

#include <stdio.h>
#include <conio.h>
int main(){
   float a[2][2], b[2][2], c[2][2];
   int i,j;
   printf("Enter the elements of 1st matrix\n");
/* Reading two dimensional Array with the help of two for loop. If there was an array of 'n' dimension, 'n' numbers of loops are needed for inserting data to array.*/   
   for(i=0;i<2;++i)      
       for(j=0;j<2;++j){
       printf("Enter a%d%d: ",i+1,j+1);
       scanf("%f",&a[i][j]);
       }
   printf("Enter the elements of 2nd matrix\n");
   for(i=0;i<2;++i)
       for(j=0;j<2;++j){
       printf("Enter b%d%d: ",i+1,j+1);
       scanf("%f",&b[i][j]);
       }
   for(i=0;i<2;++i)
       for(j=0;j<2;++j){
/* Writing the elements of multidimensional array using loop. */
       c[i][j]=a[i][j]+b[i][j];  /* Sum of corresponding elements of two arrays. */
       }
   printf("\nSum Of Matrix:");
   for(i=0;i<2;++i)
       for(j=0;j<2;++j){
       printf("%.1f\t",c[i][j]);  
           if(j==1)             /* To display matrix sum in order. */
              printf("\n");
      }
return 0;
}

output:
Enter the elements of 1st matrix
Enter a11: 2;
Enter a12: 0.5;
Enter a21: -1.1;
Enter a22: 2;
Enter the elements of 2nd matrix
Enter b11: 0.2;
Enter b12: 0;
Enter b21: 0.23;
Enter b22: 23;

Sum Of Matrix:
2.2     0.5
-0.9    25.0
Example program 2 :
#include <stdio.h>
#include <conio.h>
main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];
 
   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of matrix \n");
 
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         scanf("%d",&matrix[c][d]);
      }
   }
 
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         transpose[d][c] = matrix[c][d];
      }
   }
 
   printf("Transpose of entered matrix :-\n");
 
   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",transpose[c][d]);
      }  
      printf("\n");
   }
 
   return 0;
}

output:
Enter the number of rows and columns of matrix
3  3
Enter the elements of matrix
2  5  7
8  3  4
1  4  7
Transpose of entered matrix :-
2  8  1
5  3  4
7  4  7

Limitations of two dimensional array :

1.We cannot delete any element from an array.
2.If we don't know that how many elements have to be stored in a memory in advance, then there will be memory wastage if large array size is specified.


Important questions:

Q. Why is it necessary to give the size of an array in an array declaration?
Ans: When an array is declared, the compiler allocates a base address and reserves enough space in memory for all the elements of the array. The size is required to allocate the required space and hence size must be mentioned.
Q.Is it possible to have negative index in an array?
Ans: Yes it is possible to index with negative value provided there are data stored in this location. Even if it is illegal to refer to the elements that are out of array bounds, the compiler will not produce error because C has no check on the bounds of an array.
Q.Difference between a array name and a pointer variable?
Ans: A pointer variable is a variable where as an array name is a fixed address and is not a variable.
 A pointer variable must be initialized but an array name cannot be initialized. An array name being a constant value , ++ and — operators cannot be applied to it.

1 comment:

  1. Nice n precise... Q&A part is good and needs many more. Please keep writing on C language...

    ReplyDelete