SlideShare una empresa de Scribd logo
1 de 16
Descargar para leer sin conexión
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 } ;

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
Pf presntation
Pf presntationPf presntation
Pf presntation
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Multi-Dimensional Lists
Multi-Dimensional ListsMulti-Dimensional Lists
Multi-Dimensional Lists
 
Day 1c access, select ordering copy.pptx
Day 1c   access, select   ordering copy.pptxDay 1c   access, select   ordering copy.pptx
Day 1c access, select ordering copy.pptx
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
C arrays
C arraysC arrays
C arrays
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 

Similar a 11 1. multi-dimensional array eng

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

PPS Arrays Matrix operations
PPS Arrays Matrix operationsPPS Arrays Matrix operations
PPS Arrays Matrix operations
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
unit-2-dsa.pptx
unit-2-dsa.pptxunit-2-dsa.pptx
unit-2-dsa.pptx
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
7. arrays
7. arrays7. arrays
7. arrays
 
Vcs16
Vcs16Vcs16
Vcs16
 
Chandan
ChandanChandan
Chandan
 
C programs
C programsC programs
C programs
 
Arrays in CPP
Arrays in CPPArrays in CPP
Arrays in CPP
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
Datastructure tree
Datastructure treeDatastructure tree
Datastructure tree
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
 

Más de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
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웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
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웅식 전
 
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웅식 전
 

Más de 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
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
 

Último

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 

Último (20)

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 

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 } ;