Array-in-C .

In this session i have discussed array in C .


The multidimensional array stored the data in matrix form (tabular form) . It may be two dimesional or three dimensional in C .

CSharp array image

Multidimensional Array Syntax :

   data_type arr_name[row][column];
   
   int arr[4][3];
   

In the above code array name is arr 4 is the number of rows and 3 is number of column .


Example : Write the program that demonstrate the multidimensional array .

    #includ<stdio.h> 
    int main()
    {
            int i ,j;
            int arr[2][2];
            arr[0][0]=50;
            arr[0][1]=10;
            arr[1][0]=15;
            arr[1][1]=20;
            
            for(i=0;i<2;i++)
            {
                for (j = 0; j <2;j++ )
                {
                    printf("%d",arr[i][j]);                 
                }
                printf("\n");//change the new line
            }
            return 0;
    }
   
   OUTPUT :  50 10
             15 20
   

Example : Write the program that search the value from multidimensional array .

    #includ<stdio.h> 
    int main()
    {
            int i,j,k;
            int search;
            k=0;
            int arr[2][2];
            arr[0][0]=10;
            arr[0][1]=20;
            arr[1][0]=30;
            arr[1][1]=40;
            for (i = 0; i < 2; i++)
            {
                for ( j = 0; j < 2; j++)
                {
                    printf("%d",arr[i][j]);
                    printf(" ");
                }
                printf("\n");
            }
            printf("Enter the value for search : ");
            scanf("%d",&search);
            for(int i=0;i<2;i++)
            {
                for (int j = 0; j <2;j++ )
                {
                      if(arr[i][j]==search)
                      {
                          k=1;
                          break;
                      }
                }
            }
            if(k==1)
            {
                printf("Search value found ");
            }
            else
            {
                printf("Search value not found");
            }
            return 0;        
    }
   
   OUTPUT :  10 20
             30 40
             Enter the value for search : 20
             Search value found
              
   

Example : Write the program that declares and initialized the multidimensional array at same time .

    #includ<stdio.h> 
    int main()
    {
            int i,j;
            int arr[2][2] ={{10,20},{30,40}};
            for (i = 0; i < 2; i++)
            {
                for ( j = 0; j < 2; j++)
                {
                    printf("%d",arr[i][j]);
                    printf(" ");
                }
                printf("\n"); //change the new line
            }
            return 0;
    }
   
   OUTPUT :  10 20
             30 40
              
   

Example : Write the program that add two matrix and print the result in third matrix .

    #includ<stdio.h> 
    int main()
    {
            int i,j;
            int first[2][2] ={{1,2},{3,4}};
            int second[2][2] = { { 5, 6 }, { 7, 8 } };
            int result[2][2];
            
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    result[i][j] = first[i][j] + second[i][j];
                }
            }
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    printf("%d",result[i][j]);
                    printf(" ");
                }
                printf("\n");//change the new line
            }
            return 0;
    }
   
   OUTPUT :  6  8
             10 12
              
   

Welcome to AndroidATPoint.

back image                     next image