Upcoming SlideShare
×

# 11 1. multi-dimensional array eng

419 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
419
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
12
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 11 1. multi-dimensional array eng

1. 1. Multidimensional Arrays
2. 2. 2Two-Dimensional Arrays Two-Dimensional Array Syntaxdata_type variable_name[ number][ number ];Array dimensionsDeclarations of arrays Remarksint a[100]; a one-demensional arrayint b[2][7]; a two-demensional arrayint c[5][3][2]; a three-demensional array
3. 3. 3Two-Dimensional Arrays Logical placement of int a[3][4]a[0][0] a[0][1] a[0][2] a[0][3]a[1][0] a[1][1] a[1][2] a[1][3]a[2][0] a[2][1] a[2][2] a[2][3]row 0row 1col 0 col 1 col 2 col 3row 2
4. 4. 4Two-Dimensional Arrays Two-Demensional Arrays #include <stdio.h>#define M 3 /* number of rows */#define N 4 /* number of columns */int main(void){int a[M][N], i, j, sum = 0;for ( i = 0; i < M; ++i )for ( j = 0; j < N; ++j )a[i][j] = i + j;for ( i = 0; i < M; ++i ) {for ( j = 0; j < N; ++j )printf(“a[%d][%d] = %d “,i, j, a[i][j] );printf(“n”);}}a[0][0] = 0 a[0][1] = 1 a[0][2] = 2 a[0][3] = 3a[1][0] = 1 a[1][1] = 2 a[1][2] = 3 a[1][3] = 4a[2][0] = 2 a[2][1] = 3 a[2][2] = 4 a[2][3] = 5
5. 5. 5Two-Dimensional Arrays Physical Placement of int a[3][4]– Two-Dimensional Array is actually storedas the format of 1-Dimensional Array in the computer memory.a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]row 0 row 1 row 21000 1016 1032
6. 6. 6Two-Dimensional Arrays[Ex]int a[2][3], *p ;p = &a[0][0];p + 0  &a[0][0]  a[0] + 0p + 1  &a[0][1]  a[0] + 1p + 2  &a[0][2]  a[0] + 2p + 3  &a[1][0]  a[0] + 3  a[1] + 0p + 4  &a[1][1]  a[0] + 4  a[1] + 1p + 5  &a[1][2]  a[0] + 5  a[1] + 2a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
7. 7. Two-Dimensional Arrays Physical placement of int a[3][4]– If int a[3][4] begins at address 1000, What’s the next value?7a = ?a + 1 = ?a[0] = ?a[0] + 1 = ?a[1] = ?a[1] + 1 = ?a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]a[0] a[1] a[2]1000 1016 1032
8. 8. 8Two-Dimensional Arrays Physical placement of int a[3][4]– a is a constant, type is int (*)[4]– a[0], a[1], a[2] are each constant, type is int*a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]a[0] a[1] a[2]1000 1016 1032&a[0][0] ==a[0]==(int*)1000 &a[1][0]==a[1]==(int*)1016 &a[2][0]==a[2]==(int*)1032a==&a[0]=(int**)1000 (a+1)==&a[1]=(int**)1016 (a+2)==&a[2]=(int**)1032
9. 9. 9Two-Dimensional Arrays Many ways to access the element of Two-Dimensional array– a[ i ] : a element in i th line of array a– a[ i ][ j ] : a element in i th line and j th row of array a– Array name a is equal to &a[0]Expressions equivalent to a[ i ][ j ]*( a[ i ] + j )( *( a + i ) ) [ j ]*( ( *( a + i ) ) + j )*( &a[0][0] + 4 * i + j )int a[3][4]
10. 10. 10Two-Dimensional Arrays#include <stdio.h>void main() {int a[3][4], j, k, sum = 0 ;for( j = 0 ; j < 3 ; j++ )for( k = 0 ; k < 4 ; k++ )scanf( “%d”, &a[j][k] ) ;for( j = 0 ; j < 3 ; j++ )for( k = 0 ; k < 4 ; k++ )sum += a[j][k] ;printf( “%dn”, sum ) ;} Passing Two-Dimensional Array to function#include <stdio.h>int sum(?????) { ... }void main() {int a[3][4], j, k, sum = 0 ;for( j = 0 ; j < 3 ; j++ )for( k = 0 ; k < 4 ; k++ )scanf( “%d”, &a[j][k] ) ;printf( “%dn”, sum(????) ) ;}
11. 11. 11Two-Dimensional Arrays Passing Two-Dimensional Array to functionint sum( int num[][4], int size ){for( j = 0 ; j < size ; j++ )for( k = 0 ; k < 4 ; k++ )sum += num[j][k] ;}printf( “%dn”, sum(a, 3) ) ;int (*num)[4]
12. 12.  Passing Two-Dimensional Array to function– Why not this?Two-Dimensional Arrays12int sum( int num[][], int size0, int size1 ){for( j = 0 ; j < size0 ; j++ )for( k = 0 ; k < size1 ; k++ )sum += num[j][k] ;}printf( “%dn”, sum(a, 3, 4) ) ;If you wrote ‘ int num[3][4] ‘,C compiler converts from num[i][j] to *(base_address + 4*i + j).Thus, If don’t specify 2nd size(4)Then can not convert from num[i][j] to *(base_address + 4*i + j).
13. 13. Two-Dimensional Arrays Passing Two-Dimensional Array to function– Type conversion from 2-Dimensional array to 1-Dimensionalarray13int sum( int num[], int size0, int size1 ){for( j = 0 ; j < size0 ; j++ )for( k = 0 ; k < size1 ; k++ )sum += *(num+ size1*j + k) ;}printf( “%dn”, sum( (int*)a, 3, 4) ) ;
14. 14. 14Multidimensional Arrays Passing Three-Dimensional Array to functionint sum( int num[][4][5], int size ){for( j = 0 ; j < size ; j++ )for( k = 0 ; k < 4 ; k++ )for( l = 0 ; l < 5 ; l++ )sum += num[j][k][l] ;}printf( “%dn”, sum(a, 3) ) ;int (*num)[4][5]
15. 15. Multidimensional Arrays Passing Three-Dimensional Array to function– Converting to the pointer of 1-dimensional array15int sum( int num[], int s0, int s1, int s2 ){for( j = 0 ; j < s0 ; j++ )for( k = 0 ; k < s1 ; k++ )for( l = 0 ; l < s2 ; l++ )sum += *(num+ s1*s2*j + s2*k + l) ;}printf( “%dn”, sum((int*)a, 3, 4, 5) ) ;
16. 16. 16Multidimensional Arrays Initialization of Multi-dimensional Array[Ex]int a[ 2 ][ 2 ][ 3 ] = { { {1,1,0}, {2,0,0} }, { {3,0,0}, {4,4,0} } };[Ex]int a[ 2 ][ 2 ][ 3 ] = { 0 }; All element are initialized with 0[Ex]int a[ ][ 2 ][ 3 ] = { { {1, 1}, {2} }, { {3}, {4, 4} } };[Ex]int a[ 2 ][ 2 ][ 3 ] = { 1, 1, 0, 2, 0, 0, 3, 0, 0, 4, 4, 0 } ;