Coupon Accepted Successfully!


Two-dimensional(2D) Array

A two dimensional array is an array in which each element is itself an array. For instance, an array A[m][n] is an m by n table with m rows and n columns containing m x n elements. The number of elements in a 2-D array can be determined by multiplying number of rows with number of columns.

For example, the number of elements in an array A[7][9] is calculated as 7x 9 =63 The simplest form of a multidimensional array, the two-dimensional array, is an arry having single-dimensiona arrays as its elements. The general form of a two-dimensional array declaration in c is as follows : Type array-name: Where type is the base data type of the array having name array-name; rows, the first indes, refer to the number of rows in the array and columns, the second indes, refers to the number of columns in the array.  

Follolling declaration declares an int array sales of size 5,12 int sales[5][12]; The array sales have 5 elements sales[0], sales[1], sales[2],…. Each of which is itself an int array with 12 elements. The elements of sales are referred to as sales [0][0], sales [0][1], sales [0][2], ………….., sales [0][11], sales [1][0], sales [1][1], ….. and so forth. You can define two- or multi-dimensional arrays. It is taken as an array of an array. Logically, the two-dimensional array 3 X 2 is taken as 3 1 5 2 8 7 Here there are three arrays, i.e. one array in each row. The values are stored as 3 1 5 2 8 7 This style is called row measure form.

Each row array is represented as a[0], which consists of elements 3 and 1. a[1] consists of 5 2 and a[2] consists of 8 7. Each element of a[0] is accessed as a [0] [0] and a[0] [1], thus the value of a[0][0] and a[0][1] is 1.     Example: #include void printarr(int a[ ][ ]); void printdetail(int a[ ][ ]); void print_usingptr(int a[ ][ ]); main() { int a[3][2]; \\ A for(int i = 0;i<3;i++) for(int j=0;j<2 ;j++) { { a[i]=i; } } printdetail(a); } void printarr(int a[ ][ ]) { for(int i = 0;i<3;i++) for(int j=0;j<2;j++) { { printf("value in array %d\n",a[i][j]); } } } void printdetail(int a[ ][ ]) { for(int i = 0;i<3;i++) for(int j=0;j<2;j++) { { printf( "value in array %d and address is %8u\n", a[i][j],&a[i][j]); } } } void print_usingptr(int a[ ][ ]) { int *b; \\ B b=a; \\ C for(int i = 0;i<6;i++) \\ D { printf("value in array %d and address is lu\n",*b,b); b++; // increase by 2 bytes \\ E } }   Explanation Statement A declares a two-dimensional array of the size 3 × 2.

The size of the array is 3 × 2, or 6. Each array element is accessed using two subscripts. You can use two for loops to access the array. Since i is used for accessing a row, the outer loop prints elements row-wise, that is, for each value of i, all the column values are printed. You can access the element of the array by using a pointer. Statement B assigns the base address of the array to the pointer. The for loop at statement C increments the pointer and prints the value that is pointed to by the pointer. The number of iterations done by the for loop, 6, is equal to the array. Using the output, you can verify that C is using row measure form for storing a two-dimensional array.

Multi dimensional arrays

Up to this point what we have discussed is single dimensional arrays. But arrays can have more than one dimension. Consider a matrix or a determinant They are 2-dimensional arrays. Consider a vector in 3-D plane. It is an array of 3-dimensions. Think of Einstein he took another dimension time. So to represent vector in his calculations a 4-D array is needed. More than 4-D arrays are rarely used. The general definition of a multi dimensional array is:   ; As an example int abc[3][4][2] defines abc to be a 3-dimensional array of integer.

To access cells of this array we need 3 loops one for each dimension. Let us see how we can access the cells int i j, k, temp; int pqr[5][6][9]; /* input data into array…………………………..*/ for(i=0;i<5;i++) { for(j=0;j<6;j++) { fo(k=0;k<9;k++) { printf("Enter a number "); scanf("%d", &temp); pqr[i][j][k]=temp; } } } /* output(display) elements of array …………………*/ for(i=0;i<5;i++) { for(j=0;j<6;j++) { for(k=0;k<9;k++) { printf("value at cell %d %d %d = %d\n",pqr[i][j][k]); } } }   above code fragment is capable of reading a number from user store it in a multidimensional array and display it Here is a point to note. Watch the above code carefully, you will see pqr is an array of array of arrays. Just watch array is repeated 3-times. Similarly a 2-D array is a single dimensional array of single dimensional arrays.

Manipulating arrays using pointers

When the pointer is incremented by an increment operator, it is always right incremented. That is, if the pointer points to an integer, the pointer is incremented by 2, and, if it is long, it is incremented by 4.   #include void printarr(int a[]); void printdetail(int a[]); void print_usingptr(int a[]); main() { int a[5]; for(int i = 0;i<5;i++) { a[i]=i; } print_usingptr(a); } void printarr(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d\n",a[i]); } } void printdetail(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d and address is %8u\n",a[i],&a[i]); } } void print_usingptr(int a[]) { int *b; b=a; for(int i = 0;i<5;i++) { printf("value in array %d and address is lu\n",*b,b); b++; // A } }  

Explanation 1. This function is similar to the preceding function except for the difference at statement A. In the previous version, b = b+2 is used. Here b++ is used to increment the pointer. 2. Since the pointer is a pointer to an integer, it is always incremented by 2. You can put values in the memory locations by using pointers, but you cannot assign the memory location to an array to access those values because an array is a pointer constant.  


 Example: #include void printarr(int a[]); void printdetail(int a[]); void print_usingptr_a(int a[]); main() { int a[5]; int *b; int *c; for(int i = 0;i<5;i++) { a[i]=i; } printarr(a); *b=2; \\ A b++; \\ B *b=4; \\ C b++; *b=6; \\ D b++; *b=8; \\ E b++; *b=10; b++; *b=12; b++; a=c; //error \\F printarr(a); } void printarr(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d\n",a[i]); } } void printdetail(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d and address is lu\n",a[i],&a[i]); } } void print_usingptr_a(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d and address is lu\n",*a,a); \\ F a++; // increase by 2 bytes \\ G } }  

Explanation 1. You can assign a value at the location specified by b using statement A. 2. Using statement B, you can point to the next location so that you can specify a value at that location using statement C. Using this procedure, you can initialize 5 locations. 3. You cannot assign the starting memory location as given by statement F to access those elements because a is a pointer constant and you cannot change its value. 4. The function print_usingptr_a works correctly even though you are writing a++. This is because when you pass a as a pointer in an actual parameter, only the value of a is passed and this value is copied to the local variable. So changing the value in the local variable will not have any effect on the outside function.

Test Your Skills Now!
Take a Quiz now
Reviewer Name