SlideShare a Scribd company logo
Multidimensional Arrays
2
Two-Dimensional Arrays
 Two-Dimensional Array Syntax
data_type variable_name[ number][ number ];
Array dimensions
Declarations of arrays Remarks
int a[100]; a one-demensional array
int b[2][7]; a two-demensional array
int c[5][3][2]; a three-demensional array
3
Two-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 0
row 1
col 0 col 1 col 2 col 3
row 2
4
Two-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] = 3
a[1][0] = 1 a[1][1] = 2 a[1][2] = 3 a[1][3] = 4
a[2][0] = 2 a[2][1] = 3 a[2][2] = 4 a[2][3] = 5
5
Two-Dimensional Arrays
 Physical Placement of int a[3][4]
– Two-Dimensional Array is actually stored
as 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 2
1000 1016 1032
6
Two-Dimensional Arrays
[Ex]
int a[2][3], *p ;
p = &a[0][0];
p + 0  &a[0][0]  a[0] + 0
p + 1  &a[0][1]  a[0] + 1
p + 2  &a[0][2]  a[0] + 2
p + 3  &a[1][0]  a[0] + 3  a[1] + 0
p + 4  &a[1][1]  a[0] + 4  a[1] + 1
p + 5  &a[1][2]  a[0] + 5  a[1] + 2
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
Two-Dimensional Arrays
 Physical placement of int a[3][4]
– If int a[3][4] begins at address 1000, What’s the next value?
7
a = ?
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
Two-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*)1032
a==&a[0]=(int**)1000 (a+1)==&a[1]=(int**)1016 (a+2)==&a[2]=(int**)1032
9
Two-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
Two-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
Two-Dimensional Arrays
 Passing Two-Dimensional Array to function
int 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]
 Passing Two-Dimensional Array to function
– Why not this?
Two-Dimensional Arrays
12
int 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).
Two-Dimensional Arrays
 Passing Two-Dimensional Array to function
– Type conversion from 2-Dimensional array to 1-Dimensional
array
13
int 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
Multidimensional Arrays
 Passing Three-Dimensional Array to function
int 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]
Multidimensional Arrays
 Passing Three-Dimensional Array to function
– Converting to the pointer of 1-dimensional array
15
int 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
Multidimensional 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 } ;

More Related Content

PPT
Multi dimensional arrays
PPT
Multidimensional array in C
PPT
Two dimensional array
PPTX
Two dimensional arrays
PPTX
2- Dimensional Arrays
PDF
Two dimensional array
PPTX
2D Array
PPSX
C Programming : Arrays
Multi dimensional arrays
Multidimensional array in C
Two dimensional array
Two dimensional arrays
2- Dimensional Arrays
Two dimensional array
2D Array
C Programming : Arrays

What's hot (20)

PDF
Multi dimensional array
PPTX
Array,MULTI ARRAY, IN C
PPTX
Pf presntation
PPTX
Introduction to Array ppt
PPTX
Array Introduction One-dimensional array Multidimensional array
PDF
Lecture17 arrays.ppt
PPT
Java: Introduction to Arrays
PPTX
Multi-Dimensional Lists
PDF
Day 1c access, select ordering copy.pptx
PDF
Numpy python cheat_sheet
PPT
Sparse Matrix and Polynomial
PDF
Arrays in C
PDF
Arrays and library functions
PDF
Python3 cheatsheet
PPT
PPT
Arrays
PPT
DOCX
Array
PDF
Numpy tutorial(final) 20160303
Multi dimensional array
Array,MULTI ARRAY, IN C
Pf presntation
Introduction to Array ppt
Array Introduction One-dimensional array Multidimensional array
Lecture17 arrays.ppt
Java: Introduction to Arrays
Multi-Dimensional Lists
Day 1c access, select ordering copy.pptx
Numpy python cheat_sheet
Sparse Matrix and Polynomial
Arrays in C
Arrays and library functions
Python3 cheatsheet
Arrays
Array
Numpy tutorial(final) 20160303
Ad

Similar to 11 1. multi-dimensional array eng (20)

PDF
PPS Arrays Matrix operations
PPTX
Java arrays
PPT
array2d.ppt
PPTX
array ppt of c programing language for exam
PPTX
unit-2-dsa.pptx
PDF
2Darrays.pdf. data structure using c programming
PPT
SP-First-Lecture.ppt
PDF
7. arrays
PPT
Chandan
PDF
C programs
PPTX
Arrays in CPP
PPTX
arrays.pptx
PDF
codes.txt.pdf code presentation engineering
PDF
Solution Manual for Data Structures and Algorithm Analysis in C++, 4/E 4th Ed...
PPTX
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
PPT
02 Arrays And Memory Mapping
PPTX
Datastructure tree
PDF
Programming Fundamentals Arrays and Strings
PDF
An overview of Python 2.7
PPS Arrays Matrix operations
Java arrays
array2d.ppt
array ppt of c programing language for exam
unit-2-dsa.pptx
2Darrays.pdf. data structure using c programming
SP-First-Lecture.ppt
7. arrays
Chandan
C programs
Arrays in CPP
arrays.pptx
codes.txt.pdf code presentation engineering
Solution Manual for Data Structures and Algorithm Analysis in C++, 4/E 4th Ed...
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
02 Arrays And Memory Mapping
Datastructure tree
Programming Fundamentals Arrays and Strings
An overview of Python 2.7
Ad

More from 웅식 전 (20)

PDF
15 3. modulization
PDF
15 2. arguement passing to main
PDF
14. fiile io
PDF
13. structure
PDF
12 2. dynamic allocation
PDF
12 1. multi-dimensional array
PDF
11. array & pointer
PDF
10. pointer & function
PDF
9. pointer
PDF
7. variable scope rule,-storage_class
PDF
6. function
PDF
5 2. string processing
PDF
5 1. character processing
PDF
15 1. enumeration, typedef
PDF
4. loop
PDF
3 2. if statement
PDF
3 1. preprocessor, math, stdlib
PDF
2 3. standard io
PDF
2 2. operators
PDF
2 1. variables & data types
15 3. modulization
15 2. arguement passing to main
14. fiile io
13. structure
12 2. dynamic allocation
12 1. multi-dimensional array
11. array & pointer
10. pointer & function
9. pointer
7. variable scope rule,-storage_class
6. function
5 2. string processing
5 1. character processing
15 1. enumeration, typedef
4. loop
3 2. if statement
3 1. preprocessor, math, stdlib
2 3. standard io
2 2. operators
2 1. variables & data types

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Cloud computing and distributed systems.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
Spectroscopy.pptx food analysis technology
Cloud computing and distributed systems.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Chapter 3 Spatial Domain Image Processing.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Spectral efficient network and resource selection model in 5G networks
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Dropbox Q2 2025 Financial Results & Investor Presentation
Mobile App Security Testing_ A Comprehensive Guide.pdf
cuic standard and advanced reporting.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
MYSQL Presentation for SQL database connectivity
NewMind AI Weekly Chronicles - August'25 Week I

11 1. multi-dimensional array eng

  • 2. 2 Two-Dimensional Arrays  Two-Dimensional Array Syntax data_type variable_name[ number][ number ]; Array dimensions Declarations of arrays Remarks int a[100]; a one-demensional array int b[2][7]; a two-demensional array int c[5][3][2]; a three-demensional array
  • 3. 3 Two-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 0 row 1 col 0 col 1 col 2 col 3 row 2
  • 4. 4 Two-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] = 3 a[1][0] = 1 a[1][1] = 2 a[1][2] = 3 a[1][3] = 4 a[2][0] = 2 a[2][1] = 3 a[2][2] = 4 a[2][3] = 5
  • 5. 5 Two-Dimensional Arrays  Physical Placement of int a[3][4] – Two-Dimensional Array is actually stored as 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 2 1000 1016 1032
  • 6. 6 Two-Dimensional Arrays [Ex] int a[2][3], *p ; p = &a[0][0]; p + 0  &a[0][0]  a[0] + 0 p + 1  &a[0][1]  a[0] + 1 p + 2  &a[0][2]  a[0] + 2 p + 3  &a[1][0]  a[0] + 3  a[1] + 0 p + 4  &a[1][1]  a[0] + 4  a[1] + 1 p + 5  &a[1][2]  a[0] + 5  a[1] + 2 a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
  • 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? 7 a = ? 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 Two-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*)1032 a==&a[0]=(int**)1000 (a+1)==&a[1]=(int**)1016 (a+2)==&a[2]=(int**)1032
  • 9. 9 Two-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 Two-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 Two-Dimensional Arrays  Passing Two-Dimensional Array to function int 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.  Passing Two-Dimensional Array to function – Why not this? Two-Dimensional Arrays 12 int 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. Two-Dimensional Arrays  Passing Two-Dimensional Array to function – Type conversion from 2-Dimensional array to 1-Dimensional array 13 int 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 Multidimensional Arrays  Passing Three-Dimensional Array to function int 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. Multidimensional Arrays  Passing Three-Dimensional Array to function – Converting to the pointer of 1-dimensional array 15 int 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 Multidimensional 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 } ;