Skip to main content

Multi-dimensional arrays

                Multi-dimensional arrays are those which have more than one dimensions. Multi-dimensional arrays are defined in much the same manner as one dimensional array, except that a separate pair of square brackets is required for each subscript. Thus, two dimensional arrays will require two pairs of square brackets; three dimensional arrays will require three pairs of square brackets and so on. The two dimensional array is also called matrix. Multi-dimensional array can be defined as following
  storage_class  data_type  array_name[expression1] [expression2] ...[expression n];
                  Here, expression1, expression2…..expression n are positive valued integer expressions that indicate the number of array elements associated with each subscript. An m*n two dimensional array can be thought as tables of values having m rows and n columns.
 An example
 int X[3][3] can be shown as follows

Col 1
Col 2
Col 3
Row 1
X[0][0]
X[0][1]
X[0][2]
Row 2
X[1][0]
X[1][1]
X[1][2]
Row 3
X[2][0]
X[2][1]
X[2][2]






Initialization of multi-dimensional array:
         Like one dimensional array, multi-dimensional arrays can be initialized at the time of array definition or declaration. A few example of 2-D array initialization are:
int marks[2][3]={   {2, 4, 6},
                                   {8, 10, 12}
                                };
      int marks1[][3]={   {2, 4, 6},
                                   {8, 10, 12}
                                };
   int marks2[2][3]={ 2,4,6,8, 10, 12};
   int marks3[ ][3]={ 2,4,6,8, 10, 12};
   int marks4[ ][3]={ 2,4,6,8, 10};

    What is the value of marks4[1][2]?


 
 
    
All above examples are valid. While initializing an array, it is necessary to mention the second dimension (column) whereas the first dimension (row) is optional.
   Thus, following are invalid
       int marks3[3][]={ 2,4,6,8, 10, 12};
       int marks4[][]={ 2,4,6,8, 10};    /* error!!!*/
Example: Write a program to read two 3*3 matrixes and display their sum.
#include<stdio.h>
#include<conio.h>
void main()
  {
  int a[3][3],b[3][3],sum[3][3],i,j;
  clrscr();
  printf("Enter First matrix row by row\n");
  for(i=0;i<3;i++)
     for(j=0;j<3;j++)
       scanf("%d",&a[i][j]);

  printf("Enter second matrix as first\n");
  for(i=0;i<3;i++)
     for(j=0;j<3;j++)
      scanf("%d",&b[i][j]);
   for(i=0;i<3;i++)
   {  for(j=0;j<3;j++)
      { sum[i][j]=a[i][j]+b[i][j];}
    printf("\nThe sum matrix is:\n");}
    for(i=0;i<3;i++)
      {
       for(j=0;j<3;j++)
         {
          printf("\t%d",sum[i][j]);
          }
          printf("\n");
         }
    getch();
  }

 


Comments

Popular posts from this blog

Passing arrays to functions in C programming

Like any other variables, we can also pass entire array to a function. An array name can be named as an argument for the prototype declaration and in function header. When we call the function no need to subscript or square brackets. When we pass array that pass as a call by reference because the array name is address for that array. /* Program to illustrate passing array to function */ #include<stdio.h> void display(int) ; /* function prototype */ main( ) { int num[5] = {100, 20, 40, 15, 33, i ; clrscr( ) ; printf (“\n The content of array is \n”) ; for (i=0; i<5; i++) display (num[i]) ; /*Pass array element fo fun */ getch{ } ; } void display(int n) { printf (“\t%d”, n ) ; } Output:     The content of array is 100      20       40       15 3 /* Program to read 10 numbers from keyboard to store these num into array and then c...

Software Development Life Cycle

Software Crisis In earlier days, same codes needed to be written time and again increasing the complexity and time required for development This resulted in software not being completed on time and the cost increased than expected Solution: Object Oriented Programming Software Evolution Investigation Feasibility Study Analysis Design Implementation Maintenance Investigation First step in preparation of the IS includes the preliminary study of proposed information system investigates the information needs of the users and determines the resource requirement, costs, benefits and feasibility of a proposed project 2. Feasibility Study feasibility study is an analysis of the practicality of an idea focuses on helping answer the essential question of “should we proceed with the proposed project idea?” The feasibility study is conducted in four different areas: 1. Organizational feasibility 2. Economic feasibility 3. Technical feasibility 4. Operation...

Explanation of Graphics in C programming

   The header file graphics.h should be included to use graphics related built in functions. Again, the variable gDriver is an integer variable that specifies graphics driver to be used. Device drivers are small programs which talk directly to the hardware. Graphics drivers are a subset of device drivers and are applicable only in the graphics mode. The initialization of this variable to DETECT is to set another variable gMode to the highest resolution available for the detected driver.  The value of this variable gDriver may be another than DETECT (e.g. 1 for CGA, 9 for VGA.). The variable gMode is an integer that specifies initial graphics mode.       The built in function initgraph() is for graphics initialization. The syntax for this is as   initgraph(&graphics_driver, &graphics_mode, path_to_driver);   path_to_driver specifies the directory path where initgraph() looks for graphics drivers ( .BGI files). Thi...