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

Database Management System(DBMS)

Data and Information  Data is an unprocessed raw fact which does  not give complete meaning about any object,person, entity etc. But when various inter-related data arranged in a row, it gives a complete meaning which is referred to as information. For Example: ID Name Address Program 1101 Ram Shrestha Biratnagar BScIT         is information of a specific person. Database Database is a repository(storage) for the organized collection of related information  of data items. It Consist of meaningful arrangement of data, relationship, constraints, schema etc. In modern days we define database as a computerized record-keeping system used to store information and allows users to retrieve and update these information on their request. Some example of databases are telephone directory, data of SLC result, student and employee records etc. Database Management System(DBMS) Database is the softw...

What is Java Applets?

An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible web browser. Furthermore, an applet is downloaded on demand, without further interaction with the user. If the user clicks a link that contains an applet, the applet will be automatically downloaded and run in the browser. Applets are intended to be small programs. They are typically used to display data provided by the server, handle user input, or provide simple functions, such as a loan calculator, that execute locally, rather than on the server. In essence, the applet allows some functionality to be moved from the server to the client.

What is manipulator in C++?

Answer: Manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream. Table-2 shows some important manipulator functions that are frequently used. To access these manipulators, the file < iomanip.h > should be included in program.                               Table-2               Manipulators                                                    Equivalent ios function               set...