際際滷

際際滷Share a Scribd company logo
Multidimensional Arrays
2
Two-Dimensional Arrays
則 2谿 覦一 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
則 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);
}
return 0;
}
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
則 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 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
則 int a[3][4] 覓朱Μ 覦一
 int a[3][4] 螳 1000覯讌   れ 螳?
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
則 int a[3][4] 覓朱Μ 覦一
 a 企, type int (*)[4]
 a[0], a[1], a[2]  企, type 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
則 2谿 覦一 element襯 access  螳讌 覦覯
 a[ i ] a i覯讌 
 a[ i ][ j ] 覦一伎 i覯讌 螻 j覯讌 伎 
 覦一 企 a &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>
int 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 ) ;
return 0;
}
則 2谿覦一伎  蠍郁鍵
#include <stdio.h>
int sum(?????) { ... }
int 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(????) ) ;
return 0;
}
11
Two-Dimensional Arrays
則 2谿覦一伎  蠍郁鍵
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]
則 2谿覦一伎  蠍郁鍵
  願 蟾?
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) ) ;
int num[3][4]  
num[i][j] 手 磯, C compiler 願 *(base_address + 4*i + j)襦 覲.
(谿瑚: C 語 企朱 num[i][j]朱  譟伎讌 )
磯殊, 覯讌 蠍一 4襯 覈讌 朱, num[i][j]襯 *(base_address + 4*i + j)
襦 覲  .
Two-Dimensional Arrays
則 2谿覦一伎  蠍郁鍵
 蠏碁 蟲褐 蠏碁 朱 螻 矩る,
 2谿覦一伎 1谿覦一企 覲 
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
則 3谿覦一伎  蠍郁鍵
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
則 3谿覦一伎  蠍郁鍵
 1谿 覦一 誤磯 覲 蠍郁鍵
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
則 れ姶 覦一伎 豐蠍壱
[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 }; 覈 れ 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

What's hot (20)

襭蟲譟5覲願
襭蟲譟5覲願襭蟲譟5覲願
襭蟲譟5覲願
KimChangHoen
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 HwpProject#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Kimjeongmoo
伎一 C1 襦 4
伎一 C1 襦 4伎一 C1 襦 4
伎一 C1 襦 4
pkok15
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf
kd19h
伎一 C1 襦 5
伎一 C1 襦 5伎一 C1 襦 5
伎一 C1 襦 5
pkok15
瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
Suhyun Park
R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁
Terry Cho
Insert Sort Algorithm (曙 螻襴讀)
Insert Sort Algorithm (曙  螻襴讀)Insert Sort Algorithm (曙  螻襴讀)
Insert Sort Algorithm (曙 螻襴讀)
Junyeong Choi
殊ろ危 瑚係襾狩 碁Μ - Sogang ICPC Team, 2020 Winter
殊ろ危 瑚係襾狩 碁Μ - Sogang ICPC Team, 2020 Winter殊ろ危 瑚係襾狩 碁Μ - Sogang ICPC Team, 2020 Winter
殊ろ危 瑚係襾狩 碁Μ - Sogang ICPC Team, 2020 Winter
Suhyun Park
襭蟲譟 襦
襭蟲譟 襦襭蟲譟 襦
襭蟲譟 襦
hyungoh kim
Ruby 2 array_hash
Ruby 2 array_hashRuby 2 array_hash
Ruby 2 array_hash
Seung-won Kim
2021 2蠍 蠍 碁碁 4譯殊姶
2021 2蠍 蠍 碁碁 4譯殊姶2021 2蠍 蠍 碁碁 4譯殊姶
2021 2蠍 蠍 碁碁 4譯殊姶
Moonki Choi
R 蠍磯蓋-一危 螳
R 蠍磯蓋-一危 螳R 蠍磯蓋-一危 螳
R 蠍磯蓋-一危 螳
Terry Cho
R ろ磯 る讌
R ろ磯 る讌R ろ磯 る讌
R ろ磯 る讌
Jaeseok Park
Python ろ磯
Python ろ磯Python ろ磯
Python ろ磯
sanghyuck Na
2012 Dm C3 03
2012 Dm C3 032012 Dm C3 03
2012 Dm C3 03
chl132435
R ろ磯 豌覯讌
R ろ磯 豌覯讌R ろ磯 豌覯讌
R ろ磯 豌覯讌
Jaeseok Park
襭蟲譟5覲願
襭蟲譟5覲願襭蟲譟5覲願
襭蟲譟5覲願
KimChangHoen
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 HwpProject#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Kimjeongmoo
伎一 C1 襦 4
伎一 C1 襦 4伎一 C1 襦 4
伎一 C1 襦 4
pkok15
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf
kd19h
伎一 C1 襦 5
伎一 C1 襦 5伎一 C1 襦 5
伎一 C1 襦 5
pkok15
瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
Suhyun Park
R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁
Terry Cho
Insert Sort Algorithm (曙 螻襴讀)
Insert Sort Algorithm (曙  螻襴讀)Insert Sort Algorithm (曙  螻襴讀)
Insert Sort Algorithm (曙 螻襴讀)
Junyeong Choi
殊ろ危 瑚係襾狩 碁Μ - Sogang ICPC Team, 2020 Winter
殊ろ危 瑚係襾狩 碁Μ - Sogang ICPC Team, 2020 Winter殊ろ危 瑚係襾狩 碁Μ - Sogang ICPC Team, 2020 Winter
殊ろ危 瑚係襾狩 碁Μ - Sogang ICPC Team, 2020 Winter
Suhyun Park
襭蟲譟 襦
襭蟲譟 襦襭蟲譟 襦
襭蟲譟 襦
hyungoh kim
Ruby 2 array_hash
Ruby 2 array_hashRuby 2 array_hash
Ruby 2 array_hash
Seung-won Kim
2021 2蠍 蠍 碁碁 4譯殊姶
2021 2蠍 蠍 碁碁 4譯殊姶2021 2蠍 蠍 碁碁 4譯殊姶
2021 2蠍 蠍 碁碁 4譯殊姶
Moonki Choi
R 蠍磯蓋-一危 螳
R 蠍磯蓋-一危 螳R 蠍磯蓋-一危 螳
R 蠍磯蓋-一危 螳
Terry Cho
R ろ磯 る讌
R ろ磯 る讌R ろ磯 る讌
R ろ磯 る讌
Jaeseok Park
2012 Dm C3 03
2012 Dm C3 032012 Dm C3 03
2012 Dm C3 03
chl132435
R ろ磯 豌覯讌
R ろ磯 豌覯讌R ろ磯 豌覯讌
R ろ磯 豌覯讌
Jaeseok Park

Viewers also liked (8)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
14. fiile io
14. fiile io14. fiile io
14. fiile io
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
13. structure
13. structure13. structure
13. structure
11= =釈
11= =釈11= =釈
11= =釈
12 (螻蠍)
12  (螻蠍)12  (螻蠍)
12 (螻蠍)
Week12 chapter11
Week12 chapter11 Week12 chapter11
Week12 chapter11
15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
14. fiile io
14. fiile io14. fiile io
14. fiile io
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
13. structure
13. structure13. structure
13. structure
11= =釈
11= =釈11= =釈
11= =釈
12 (螻蠍)
12  (螻蠍)12  (螻蠍)
12 (螻蠍)
Week12 chapter11
Week12 chapter11 Week12 chapter11
Week12 chapter11

Similar to 12 1. multi-dimensional array (20)

Python Programming: Type and Object
Python Programming: Type and ObjectPython Programming: Type and Object
Python Programming: Type and Object
Chan Shik Lim
R intro
R introR intro
R intro
譯殊
伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶
Han Sung Kim
Python datatype
Python datatypePython datatype
Python datatype
蟇危 蟾
求戟梶梶≡求釈求午 5
求戟梶梶≡求釈求午 5求戟梶梶≡求釈求午 5
求戟梶梶≡求釈求午 5
Yeonah Ki
2012 Ds D0 01 Pdf
2012 Ds D0 01 Pdf2012 Ds D0 01 Pdf
2012 Ds D0 01 Pdf
kd19h
2012 Ds D0 01
2012 Ds D0 012012 Ds D0 01
2012 Ds D0 01
chl132435
襭蟲譟05
襭蟲譟05襭蟲譟05
襭蟲譟05
herojoon1378
螻襴讀螻 襭蟲譟
螻襴讀螻 襭蟲譟螻襴讀螻 襭蟲譟
螻襴讀螻 襭蟲譟
蠍 蟾
SegmentTree Datastructure description and implementation slides
SegmentTree Datastructure description and implementation slidesSegmentTree Datastructure description and implementation slides
SegmentTree Datastructure description and implementation slides
Bomm Kim
[devil's camp] - 螻襴讀 STL (覦語)
[devil's camp] - 螻襴讀  STL (覦語)[devil's camp] - 螻襴讀  STL (覦語)
[devil's camp] - 螻襴讀 STL (覦語)
NAVER D2
Javascript≡== 梶 python 梶п=
Javascript≡== 梶 python 梶п=Javascript≡== 梶 python 梶п=
Javascript≡== 梶 python 梶п=
覿伎る 覲 覦, From c++98 to c++11, 14
覿伎る 覲 覦, From c++98 to c++11, 14 覿伎る 覲 覦, From c++98 to c++11, 14
覿伎る 覲 覦, From c++98 to c++11, 14
覈 蟾
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf
jinwookhong
れ 襦蠏碁覦 覓
れ 襦蠏碁覦 覓れ 襦蠏碁覦 覓
れ 襦蠏碁覦 覓
Kwang Yul Seo
=求メ+求++求≡= 20160311
=求メ+求++求≡= 20160311=求メ+求++求≡= 20160311
=求メ+求++求≡= 20160311
Yong Joon Moon
Python Programming: Type and Object
Python Programming: Type and ObjectPython Programming: Type and Object
Python Programming: Type and Object
Chan Shik Lim
R intro
R introR intro
R intro
譯殊
伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶
Han Sung Kim
Python datatype
Python datatypePython datatype
Python datatype
蟇危 蟾
求戟梶梶≡求釈求午 5
求戟梶梶≡求釈求午 5求戟梶梶≡求釈求午 5
求戟梶梶≡求釈求午 5
Yeonah Ki
2012 Ds D0 01 Pdf
2012 Ds D0 01 Pdf2012 Ds D0 01 Pdf
2012 Ds D0 01 Pdf
kd19h
2012 Ds D0 01
2012 Ds D0 012012 Ds D0 01
2012 Ds D0 01
chl132435
螻襴讀螻 襭蟲譟
螻襴讀螻 襭蟲譟螻襴讀螻 襭蟲譟
螻襴讀螻 襭蟲譟
蠍 蟾
SegmentTree Datastructure description and implementation slides
SegmentTree Datastructure description and implementation slidesSegmentTree Datastructure description and implementation slides
SegmentTree Datastructure description and implementation slides
Bomm Kim
[devil's camp] - 螻襴讀 STL (覦語)
[devil's camp] - 螻襴讀  STL (覦語)[devil's camp] - 螻襴讀  STL (覦語)
[devil's camp] - 螻襴讀 STL (覦語)
NAVER D2
Javascript≡== 梶 python 梶п=
Javascript≡== 梶 python 梶п=Javascript≡== 梶 python 梶п=
Javascript≡== 梶 python 梶п=
覿伎る 覲 覦, From c++98 to c++11, 14
覿伎る 覲 覦, From c++98 to c++11, 14 覿伎る 覲 覦, From c++98 to c++11, 14
覿伎る 覲 覦, From c++98 to c++11, 14
覈 蟾
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf
jinwookhong
れ 襦蠏碁覦 覓
れ 襦蠏碁覦 覓れ 襦蠏碁覦 覓
れ 襦蠏碁覦 覓
Kwang Yul Seo
=求メ+求++求≡= 20160311
=求メ+求++求≡= 20160311=求メ+求++求≡= 20160311
=求メ+求++求≡= 20160311
Yong Joon Moon

More from (20)

10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
9. pointer
9. pointer9. pointer
9. pointer
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
6. function
6. function6. function
6. function
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
4. loop
4. loop4. loop
4. loop
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
2 2. operators
2 2. operators2 2. operators
2 2. operators
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
Goorm ide 蟲′覯 for skku()
Goorm ide 蟲′覯 for skku()Goorm ide 蟲′覯 for skku()
Goorm ide 蟲′覯 for skku()
蟲襴 蠍磯蓋 螳襭
蟲襴 蠍磯蓋 螳襭蟲襴 蠍磯蓋 螳襭
蟲襴 蠍磯蓋 螳襭
Goorm ide 螳 殊企(蟲′ 覯)
Goorm ide 螳 殊企(蟲′ 覯)Goorm ide 螳 殊企(蟲′ 覯)
Goorm ide 螳 殊企(蟲′ 覯)
W14 chap13
W14 chap13W14 chap13
W14 chap13
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
10 覓語危企れ狩企
10 覓語危企れ狩企10 覓語危企れ狩企
10 覓語危企れ狩企
10th
10th10th
10th
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
9. pointer
9. pointer9. pointer
9. pointer
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
6. function
6. function6. function
6. function
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
4. loop
4. loop4. loop
4. loop
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
2 2. operators
2 2. operators2 2. operators
2 2. operators
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
Goorm ide 蟲′覯 for skku()
Goorm ide 蟲′覯 for skku()Goorm ide 蟲′覯 for skku()
Goorm ide 蟲′覯 for skku()
蟲襴 蠍磯蓋 螳襭
蟲襴 蠍磯蓋 螳襭蟲襴 蠍磯蓋 螳襭
蟲襴 蠍磯蓋 螳襭
Goorm ide 螳 殊企(蟲′ 覯)
Goorm ide 螳 殊企(蟲′ 覯)Goorm ide 螳 殊企(蟲′ 覯)
Goorm ide 螳 殊企(蟲′ 覯)
W14 chap13
W14 chap13W14 chap13
W14 chap13
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
10 覓語危企れ狩企
10 覓語危企れ狩企10 覓語危企れ狩企
10 覓語危企れ狩企
10th
10th10th
10th

12 1. multi-dimensional array

  • 2. 2 Two-Dimensional Arrays 則 2谿 覦一 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 則 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); } return 0; } 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 則 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 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 則 int a[3][4] 覓朱Μ 覦一 int a[3][4] 螳 1000覯讌 れ 螳? 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 則 int a[3][4] 覓朱Μ 覦一 a 企, type int (*)[4] a[0], a[1], a[2] 企, type 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 則 2谿 覦一 element襯 access 螳讌 覦覯 a[ i ] a i覯讌 a[ i ][ j ] 覦一伎 i覯讌 螻 j覯讌 伎 覦一 企 a &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> int 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 ) ; return 0; } 則 2谿覦一伎 蠍郁鍵 #include <stdio.h> int sum(?????) { ... } int 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(????) ) ; return 0; }
  • 11. 11 Two-Dimensional Arrays 則 2谿覦一伎 蠍郁鍵 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. 則 2谿覦一伎 蠍郁鍵 願 蟾? 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) ) ; int num[3][4] num[i][j] 手 磯, C compiler 願 *(base_address + 4*i + j)襦 覲. (谿瑚: C 語 企朱 num[i][j]朱 譟伎讌 ) 磯殊, 覯讌 蠍一 4襯 覈讌 朱, num[i][j]襯 *(base_address + 4*i + j) 襦 覲 .
  • 13. Two-Dimensional Arrays 則 2谿覦一伎 蠍郁鍵 蠏碁 蟲褐 蠏碁 朱 螻 矩る, 2谿覦一伎 1谿覦一企 覲 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 則 3谿覦一伎 蠍郁鍵 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 則 3谿覦一伎 蠍郁鍵 1谿 覦一 誤磯 覲 蠍郁鍵 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 則 れ姶 覦一伎 豐蠍壱 [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 }; 覈 れ 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 } ;