Aug 8, 2014

C Programming #40: Array - two dimension

Following article will discuss two dimension arrays in C language. We already know why arrays are requires and already Single dimension array is discussed CPROG#39 Array - single dimension. Two dimension array is actually extension of idea of single dimension array. 


Since single dimension array is linear and memory is also linear, array and memory organisation was straight forward. When it comes to two dimension array, way memory is allocated is different. Since two dimension array mimic the matrix very well. Below is example of matrix addition.
Writing C program that would do the same 


#include <stdio.h>

int main() {
   int a[2][3] = { { 1, 2, 3}, { 4, 5, 6}};
   int b[2][3] = { { 5, 2, 6}, { 1, 9, 2}};
   int c[2][3];
   int row, col;

   for(row = 0; row < 2; row++) {
      for(col = 0; col < 3; col++) {                                            
         c[row][col] = a[row][col] + b[row][col];
         printf(" %d ", c[row][col]);
      }
      printf("\n");
   }

   return 0;
}

Output of the above program is


 6  4  9 
 5  14  8 
Explanation

Declaration


int a[MAX_ROW][MAX_COL] = { { 1, 2, 3}, { 4, 5, 6}};

Here MAX_ROW is maximum number of rows array would have and MAX_COL is maximum number of columns array would have.

The initialization as follows 



  • {1, 2, 3} corresponds to initial values of first row.
  • {4, 5, 6} corresponds to initial values of second row.

Note the syntax of how initialization is done. If partial initialization is done then other elements in the matrix are initialized to zero. Here same rules for single dimension array is extended to Two dimension array.

Accessing the elements


Elements can be accessed as follows

a[row][col]
  • row is the row index. Note that row index starts from 0, Not 1.
  • col is the column index. also column index starts from 0, Not 1.
Lets do something more interesting so that Two dimension array is understood better. Lets multiple two matrix. You can find more information of how to multiply two matrix here.


#include <stdio.h>

int main() 
{
   int a[2][3] = { { 1, 2, 3}, { 4, 5, 6}};
   int b[3][2] = { { 7, 8}, {9, 10}, {11, 12}};
   int c[2][2];
   int row, col;
   int dot_idx;

   for(row = 0; row < 2; row++) {
      for(col = 0; col < 2; col++) {
         int sum = 0;
         for(dot_idx = 0; dot_idx < 3; dot_idx++) {
            sum = sum + a[row][dot_idx] * b[dot_idx][col];
         }
         c[row][col] = sum;;
         printf("  %d  ", c[row][col]);
      }
      printf("\n");
   }
   return 0;
}

Output of the above program is


  58    64  
  139    154  

Explanation 
  • Resultant matrix is 2 x 2, if you have not understood this you need to go through link again.
  • First loop goes through row of the result matrix, we already know it is 2 in size.
  • Second loop goes through column of the result matrix, which is 2.
  • Third loop is doing the dot product of row of matrix a with column of matrix b.
  • See how nicely the dot product is calculated.
    • sum = sum + a[row][dot_idx] * b[dot_idx][col];
    • Think about it, you will appreciate the usage of array.
Now lets see how the two dimension array is allocated memory. We know the memory is linear, But Two dimension array is not linear. See stores two dimension array in memory by linearisation. Let me write the program so that we can go little in detail.


#include <stdio.h>

int main() 
{
   int a[2][3] = { { 1, 2, 3}, { 4, 5, 6}};
   int row, col;

   printf("Address of a is %p\n", &a);

   for(row = 0; row < 2; row++) {
      for(col = 0; col < 3; col++) {
         printf("Row - %d, Col - %d, Value - %d, Address - %p \n",
                row, col, a[row][col], &a[row][col]);
      }
   }
   return 0;
}

Output of above program is


Address of a is 0x7fff0590c100
Row - 0, Col - 0, Value - 1, Address - 0x7fff0590c100 
Row - 0, Col - 1, Value - 2, Address - 0x7fff0590c104 
Row - 0, Col - 2, Value - 3, Address - 0x7fff0590c108 
Row - 1, Col - 0, Value - 4, Address - 0x7fff0590c10c 
Row - 1, Col - 1, Value - 5, Address - 0x7fff0590c110 
Row - 1, Col - 2, Value - 6, Address - 0x7fff0590c114 

Explanation

Below picture will explain all

Same concept can be extended more than two dimensions, but they are seldom used.

Links

Next Article - C Programming #41: Pointer - introduction
Previous Article - C Programming #39: Array - single dimension
All Article - C Programming

No comments :

Post a Comment