Wednesday 7 March 2018

Arrays in C programming

 Array:-

Array:-  Array is a collection of homogeneous data type element.
               When ever we want to represent a collection of same data type elements with a single                   name then we go for array concept.

                Each element of array is represented with one index index value.
                 1st element index value will be[0]
                 2nd element index value will be[1]
                 3rd element index value will be[2]
                 Last element index value will be(Size-1) or (n-1).



    Declare an Array in C:-

                     data_type  variable_name [sire_of_array];

   Initializing array while declaration

              int age[5] = {1, 13, 8, 16, 12}; or


               int age[] = {1, 13, 8, 16, 12};

Initializing array after declaration

  int weight[6];

  weight[0]=10;
  weight[1]=11;
  weight[2]=13;
  weight[3]=14;

  weight[4]=16;
  weight[5]=17;
   
Example:-

        #include <stdio.h>

int main()
{
    int weight [6] = {1,2,3,4,5,6};
    /* To print all the elements of the array
    for (int i=0; i < 6; i++){
       printf("%d", height[i]);
    }
    return 0;
}

        => C programming language supports multidimensional arrays.

Declare of  Two dimensional Array in C:

             
   int a[3][4] = {  
   {0, 1, 2, 3} ,  
   {4, 5, 6, 7} ,   
   {8, 9, 10, 11}
};

Note:- a[3][4] 

a[3] resepents rows and a[4] columns.


Example:-




#include <stdio.h>

int main () {

   /* an array with 6 rows and 2 columns*/
   int a[6][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8} {5,9}};
   int i, j;

   for ( i = 0; i < 6; i++ ) {

      for ( j = 0; j < 2; j++ ) {
         printf("a[%d][%d] = %d\n", i,j, a[i][j] );
      }
   }
   
   return 0;


}



For more info click here

No comments:

Post a Comment