Lab # 12

 

 

© Manzur Ashraf

Objective:

Learn how to use 2-D arrays

 

Scope:

The student should know the following:

·        Declaring and Referencing 2-D Arrays

 

What is 2_D Array ?. How to declare and  initialize  it ?:

 

Just like single dimensional arrays we can have multidimensional arrays which are arrays of arrays. The multidimensional arrays can be visualized as a matrix.

Consider the two-dimensional array case. The two-dimensional arrays can be declared as single dimension arrays as :

                        #define           SIZE    5

                        #define           SIZE1  25

                        int        grades [SIZE] [10] ;

                        char     letter [10] [4] ;

                        float     xyz [100] [SIZE1] ;

 

The two-dimensional arrays need two sizes as name[m][n]. The total size of the array is the product of the two sizes m x n. The first element is always name[0][0] and the last element is name[m-1][n-1]. The two dimensional array elements are ordered by row-wise as name[0][0], name[0][1], name[0][2]…, name[0][9], name[1][0], name[1][1],…, name[4][8], name[4][9]. The two dimensional array elements are ordered by column-wise as name[0][0], name[1][0], name[2][0]…, name[4][0], name[0][1], name[1][1],…, name[3][9], name[4][9].

 

In the first case the grades is an integer array of size 5 X 10 = 50. The elements can be referenced by using 2 subscripts as grades[1][9], grades[4][9]. The first element is grades[0][0] and the last element is grades[4][9]. Similarly, the letter is an array of characters of size 40 with last element being letter[9][3]; xyz is an array of float of size 2500 with last element being xyz[99][24].

 

The two-dimensional arrays can be initialized as

 

                        int        x [3] [2] = { 12, 56, 67, 4, 6 ,78 } ;

 

where the x[0][0] = 12, x[0][1] = 56, x[1][0] = 67, x[1][1] = 4, x[2][0] = 6, x[2][1] = 78. As with single dimension arrays if the initial values are less than the size of array the remaining values are set to 0.

 

 

 

 

 

How to read and print 2-D Array using two for loops ?:

 

The two-dimensional arrays can also be initialized by using for loop.

 

To initialize, read and print two-dimensional arrays we need two for loops. The first loop controls the first subscript and the second loop controls the second subscript. Consider the following example:

 

                        int        abc[5][10] , i, j ;

 

                        for ( i = 0 ; i < 5 ; ++ i )

                          for ( j = 0; j < 10 ; ++j )

                           {        printf (“Enter the [%d][%d] element \n”, i, j ) ;

                                    scanf (“ %d ”, &abc[i][j] ) ;      

                           }

 

In the above case the array is read in row-major order. To read in column-major order just reverse the two for loops of two subscripts as

                        for ( j = 0 ; j < 10 ; ++ j )

                          for ( i = 0; i < 5 ; ++i )

                           {       

                                    printf (“Enter the [%d][%d] element \n”, i, j ) ;

                                    scanf (“ %d ”, &abc[i][j] ) ;         

                           }

 

Similarly for printing in row-major order

                        for ( i = 0 ; i < 5 ; ++ i )

                          for ( j = 0; j < 10 ; ++j )

                           {       

                                    printf (“The abc[%d][%d] element is %d \n”, i, j, abc[i][j] ) ;      

                          }

 

and printing in column-major order

                        for ( j = 0 ; j < 10 ; ++ j )

                          for ( i = 0; i < 5 ; ++i )

                           {       

                                 printf (“The abc[%d][%d] element is %d \n”, i, j, abc[i][j] ) ;          

                           }

 

Solved Example (i) :

 

/*Matrix  multiplication */

 

#include<stdio.h>

#define row 10

#define col 10

 

int main()

{

int i,j,k, a[row][col], b[row][col], p[row][col] = {0};

int rowsa,colsa,rowsb,colsb;

 

printf("Enter number of rows for Matrix 1: ");

scanf("%d",&rowsa);

printf("Enter number of columns for Matrix 1: ");

scanf("%d",&colsa);

printf("Enter the %d elements of Matrix 1 : \n", rowsa*colsa);

 

for(j=0;j<rowsa; j++)

 for(k=0;k<colsa;k++)

 scanf("%d", &a[j][k]);

 puts("\n");

 printf("Enter number of rows for Matrix 2: ");

scanf("%d",&rowsb);

printf("Enter number of columns for Matrix 2: ");

scanf("%d",&colsb);

printf("Enter the %d elements of Matrix 2 : \n", rowsb*colsb);

for(j=0;j<rowsb; j++)

 for(k=0;k<colsb;k++)

 scanf("%d", &b[j][k]);

 

  /* Multiplication of two matrices */

 if( colsa != rowsb)

 {

 printf("Sorry gentle man you can not multiply these two Matrices ! \n");

 return 0;

 }

 else       /* multiply them */

 {

 for (i=0;i<rowsa; i++)

  for(j=0; j<rowsb; j++)

             for(k=0; k<colsb; k++)

                        p[i][k] += a[i][j] * b[j][k];

  } // end of else

 

  /* Print product of two matrices */

  printf("\nThe product of two Matrices is : \n");

  for(i=0; i<rowsa; i++)

  {

              for (k=0; k<rowsb; k++)

                        printf("%5d ", p[i][k]);

                        printf("\n");

  } // end of for loop

 

  return 0;

  } // end of main

 

Exercises

Problem#1:

Modify Solved Example (i)  to calculate and print product and  sum of two matrices.