We have started our discussion with a single dimension array in our previous section. Single dimension array is the simplest form and there can be more than one dimensions. Lets take an exaple of a physical object. Suppose it is wire or rope. It only has a single dimension which is length.

1D Object/rope

Our second case is a rectanguler shaped paper. It has length in two directions. One is length and another is width. This is 2D or two dimensional object.

2D Object/paper

Now take an another case of a book. It has length, width and a thichness. So there are three dimensions thus it is a 3D object.

3D Object/box

Same dimension rule is applicable for our C array also. Earlier we discussed single dimension array. It has 0 –(n -1) elemnents.

1D array

Now take one example int_array [row][col] . Here array has two dimension or it is of array type 2D. Here each row has a range of 0 – (col -1) elements and n row can have a range from 0 – (row -1). So total elements are (row x col).

2D array

Now take a case of 3D array- int_array [height][row][col]. Here we have a 2D planes each having a size of row x col starting from 0 – (height -1). 2D array are often used in matrix calculation. Lets say we have two 2D array and we want to add this and show this result.

3D array

Initializing 1D array:
Int_array[<size>] = { <value1>, <value2>, ..};
Initializing 2D array:
Int_array[<row size>][<col size>] = {{ <value1>, <value2>, ..}, { <value1>, <value2>, ..}, … };

/* 4x3 Matrix addition using 2D array */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int a[4][3], b[4][3],c[4][3], val;
  int i, j;
  printf ("4x3 Matrix addition\n");
  printf ("Ener elements for matrix A\n");
  for (= 0 ; j < 4; j++) {
      for (= 0; i < 3; i++) {
          printf ("A [%d][%d] = ", j, i);
          scanf ("%d", &val);
          a [j][i] = val;
      }
  }
  printf ("Ener elements for matrix B\n");
  for (= 0 ; j < 4; j++) {
      for (= 0; i < 3; i++) {
          printf ("B [%d][%d] = ", j, i);
          scanf ("%d", &val);
          b [j][i] = val;
      }
  }
  printf ("Matrix A\n");
  for (= 0 ; j < 4; j++) {
      for (= 0; i < 3; i++) {
          printf ("%.2d ", a[j][i]);
      }
      printf ("\n");
  }
  printf ("Matrix B\n");
  for (= 0 ; j < 4; j++) {
      for (= 0; i < 3; i++) {
          printf ("%.2d ", b[j][i]);
      }
      printf ("\n");
  }
  printf ("Matrix A + B\n");
  for (= 0 ; j < 4; j++) {
      for (= 0; i < 3; i++) {
          c[j][i] = a[j][i] + b[j][i];
          printf ("%.2d ", c[j][i]);
      }
      printf ("\n");
  }
  return 0;
}
Output:
4x3 Matrix addition
Ener elements for matrix A
A [0][0] = 1
A [0][1] = 2
A [0][2] = 3
A [1][0] = 4
A [1][1] = 5
A [1][2] = 6
A [2][0] = 7
A [2][1] = 8
A [2][2] = 9
A [3][0] = 10
A [3][1] = 11
A [3][2] = 12
Ener elements for matrix B
B [0][0] = 13
B [0][1] = 14
B [0][2] = 15
B [1][0] = 16
B [1][1] = 17
B [1][2] = 18
B [2][0] = 19
B [2][1] = 20
B [2][2] = 21
B [3][0] = 22
B [3][1] = 23
B [3][2] = 24
Matrix A
01  02  03
04  05  06
07  08  09
10  11  12
Matrix B
13  14  15
16  17  18
19  20  21
22  23  24
Matrix A + B
14  16  18
20  22  24
26  28  30
32  34  36

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#