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");
10    printf ("Ener elements for matrix A\n");
11    for (= 0 ; j < 4; j++) {
12        for (= 0; i < 3; i++) {
13            printf ("A [%d][%d] = ", j, i);
14            scanf ("%d", &val);
15            a [j][i] = val;
16        }
17    }
18    printf ("Ener elements for matrix B\n");
19    for (= 0 ; j < 4; j++) {
20        for (= 0; i < 3; i++) {
21            printf ("B [%d][%d] = ", j, i);
22            scanf ("%d", &val);
23            b [j][i] = val;
24        }
25    }
26    printf ("Matrix A\n");
27    for (= 0 ; j < 4; j++) {
28        for (= 0; i < 3; i++) {
29            printf ("%.2d ", a[j][i]);
30        }
31        printf ("\n");
32    }
33    printf ("Matrix B\n");
34    for (= 0 ; j < 4; j++) {
35        for (= 0; i < 3; i++) {
36            printf ("%.2d ", b[j][i]);
37        }
38        printf ("\n");
39    }
40    printf ("Matrix A + B\n");
41    for (= 0 ; j < 4; j++) {
42        for (= 0; i < 3; i++) {
43            c[j][i] = a[j][i] + b[j][i];
44            printf ("%.2d ", c[j][i]);
45        }
46        printf ("\n");
47    }
48    return 0;
49  }
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

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

 Vote 0

Similar topics related to this section

FILE, LINE, DATE, TIME, compiler marcos, token pasting, characterizing, array, multidimensional arrays, 2D and 3D dynamic array, add Matrix, multiply Matrix, adjacency Matrix,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan