SlideShare a Scribd company logo
1 of 9
TWO-DIMENSIONAL ARRAYS
TWO-DIMENSIONAL ARRAYS 
C++ also allows an array to have more than one dimension. 
For example, a two-dimensional array consists of a certain number of rows 
and columns: 
const int NUMROWS = 3; 
const int NUMCOLS = 7; 
int Array[NUMROWS][NUMCOLS]; 
0 1 2 3 4 5 6 
0 4 18 9 3 -4 6 0 
1 12 45 74 15 0 98 0 
2 84 87 75 67 81 85 79 
Array[2][5] 3rd value in 6th column 
Array[0][4] 1st value in 5th column 
The declaration must specify the number of rows and the number of columns, 
and both must be constants.
PROCESSING A 2-D ARRAY 
A one-dimensional array is usually processed via a for loop. Similarly, a two-dimensional 
array may be processed with a nested for loop: 
for (int Row = 0; Row < NUMROWS; Row++) { 
for (int Col = 0; Col < NUMCOLS; Col++) { 
Array[Row][Col] = 0; 
} 
} 
Each pass through the inner for loop will initialize all the elements of the current 
row to 0. 
The outer for loop drives the inner loop to process each of the array's rows.
INITIALIZING IN DECLARATIONS 
int Array1[2][3] = { {1, 2, 3} , {4, 5, 6} }; 
int Array2[2][3] = { 1, 2, 3, 4, 5 }; 
int Array3[2][3] = { {1, 2} , {4 } }; 
If we printed these arrays by rows, we would find the following initializations 
had taken place: 
Rows of Array1: 
1 2 3 
4 5 6 
Rows of Array2: 
1 2 3 
4 5 0 
Rows of Array3: 
1 2 0 
4 0 0 
for (int row = 0; row < 2; row++) { 
for (int col = 0; col < 3; col++) { 
cout << setw(3) 
<< Array1[row][col]; 
} 
cout << endl; 
}
EXAMPLE: INPUT USING CIN 
 Nested for loops are often used when 
inputting and assigning values to a two-dimensional array. 
 Nested loops are generally useful for getting around the 2D 
arrays… 
for (int i=0; i<RSIZE; ++i) //every row 
for (int j=0; j<CSIZE; ++j )//every col 
cin >> table[i][j];
2-D ARRAYS AS PARAMETERS 
When passing a two-dimensional array as a parameter, the base address is 
passed, as is the case with one-dimensional arrays. 
But now the number of columns in the array parameter must be specified. 
This is because arrays are stored in row-major order, and the number of 
columns must be known in order to calculate the location at which each row 
begins in memory: 
address of element (r, c) = base address of array 
+ r*(number of elements in a row)*(size of an 
element) 
+ c*(size of an element) 
void Initialize(int TwoD[][NUMCOLS], const int NUMROWS) { 
for (int i = 0; i < NUMROWS; i++) { 
for (int j = 0; j < NUMCOLS; j++) 
TwoD[i][j] = -1; 
} 
}
FUNCTION TO DISPLAY CONTENT OF A TWO 
DIMENSIONAL ARRAY A 
#include <iostream> 
#include <iomanip> 
using namespace std; 
void print(int A[][3],int N, int M) 
{ 
for (int R = 0; R < N; R++){ 
cout <<endl; 
for (int C = 0; C < M; C++) 
cout << setw(10) <<A[R][C]; 
} 
}
FUNCTION TO FIND THE SUM OF TWO DIMENSIONAL 
ARRAYS A AND B 
void addition(int A[][3], int B[][3],int N, int M) 
{ 
for(int R=0;R<N;R++){ 
cout<<endl; 
for(int C=0;C<M;C++) 
cout<<setw(10) <<A[R][C]+B[R][C]; 
} 
}
FUNCTION TO FIND & DISPLAY SUM OF ROWS & SUM 
OF COLS. OF A 2D ARRAY A 
void SumRowCol(int A[][20], int N, int M) 
{ 
for(int R=0;R<N;R++) 
{ 
int SumR=0; 
for(int C=0;C<M;C++) 
SumR+=A[R][C]; 
cout<<"Row("<<R<<")="<<SumR<<endl; 
} 
} 
for(int R=0;R<M;R++) 
{ 
int SumC=0; 
for(int C=0;C<N;C++) 
SumC+=A[C][R]; 
cout<<"Column("<<R<<")="<<SumC<<endl; 
} 
}

More Related Content

What's hot

What's hot (20)

C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Arrays
ArraysArrays
Arrays
 
Typedef
TypedefTypedef
Typedef
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
String functions in C
String functions in CString functions in C
String functions in C
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Stack application
Stack applicationStack application
Stack application
 
Strings
StringsStrings
Strings
 
C functions
C functionsC functions
C functions
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Strings in C
Strings in CStrings in C
Strings in C
 
C string
C stringC string
C string
 

Similar to 2- Dimensional Arrays

Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-array
Deepak Singh
 
19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt
AqeelAbbas94
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 

Similar to 2- Dimensional Arrays (20)

Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-array
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Structured data type
Structured data typeStructured data type
Structured data type
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Qno 3 (a)
Qno 3 (a)Qno 3 (a)
Qno 3 (a)
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Unit 2
Unit 2Unit 2
Unit 2
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 

More from Education Front

Generic Software Process Models
Generic Software Process ModelsGeneric Software Process Models
Generic Software Process Models
Education Front
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
Education Front
 

More from Education Front (20)

Improving Pronunciation
Improving PronunciationImproving Pronunciation
Improving Pronunciation
 
Generic Software Process Models
Generic Software Process ModelsGeneric Software Process Models
Generic Software Process Models
 
Problem Sloving
Problem SlovingProblem Sloving
Problem Sloving
 
Problem Solving - 1
Problem Solving - 1Problem Solving - 1
Problem Solving - 1
 
Introduction To Stack
Introduction To StackIntroduction To Stack
Introduction To Stack
 
Process Models
Process ModelsProcess Models
Process Models
 
Process Models
Process ModelsProcess Models
Process Models
 
Data Representation
Data RepresentationData Representation
Data Representation
 
Introduction to Algorithm
Introduction to AlgorithmIntroduction to Algorithm
Introduction to Algorithm
 
Revised Process of Communication
Revised Process of CommunicationRevised Process of Communication
Revised Process of Communication
 
Importance of Language in Communication
Importance of Language in CommunicationImportance of Language in Communication
Importance of Language in Communication
 
Lecture1 (SE Introduction)
Lecture1 (SE Introduction)Lecture1 (SE Introduction)
Lecture1 (SE Introduction)
 
Lecture 2 (Software Processes)
Lecture 2 (Software Processes)Lecture 2 (Software Processes)
Lecture 2 (Software Processes)
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Facing Today’s Communication Challenges
Facing Today’s Communication ChallengesFacing Today’s Communication Challenges
Facing Today’s Communication Challenges
 
Processor Basics
Processor BasicsProcessor Basics
Processor Basics
 
Register & Memory
Register & MemoryRegister & Memory
Register & Memory
 
Data Representation
Data RepresentationData Representation
Data Representation
 
Computer Evolution
Computer EvolutionComputer Evolution
Computer Evolution
 
Assembly Language Basics
Assembly Language BasicsAssembly Language Basics
Assembly Language Basics
 

Recently uploaded

Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 

Recently uploaded (20)

NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 

2- Dimensional Arrays

  • 2. TWO-DIMENSIONAL ARRAYS C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of rows and columns: const int NUMROWS = 3; const int NUMCOLS = 7; int Array[NUMROWS][NUMCOLS]; 0 1 2 3 4 5 6 0 4 18 9 3 -4 6 0 1 12 45 74 15 0 98 0 2 84 87 75 67 81 85 79 Array[2][5] 3rd value in 6th column Array[0][4] 1st value in 5th column The declaration must specify the number of rows and the number of columns, and both must be constants.
  • 3. PROCESSING A 2-D ARRAY A one-dimensional array is usually processed via a for loop. Similarly, a two-dimensional array may be processed with a nested for loop: for (int Row = 0; Row < NUMROWS; Row++) { for (int Col = 0; Col < NUMCOLS; Col++) { Array[Row][Col] = 0; } } Each pass through the inner for loop will initialize all the elements of the current row to 0. The outer for loop drives the inner loop to process each of the array's rows.
  • 4. INITIALIZING IN DECLARATIONS int Array1[2][3] = { {1, 2, 3} , {4, 5, 6} }; int Array2[2][3] = { 1, 2, 3, 4, 5 }; int Array3[2][3] = { {1, 2} , {4 } }; If we printed these arrays by rows, we would find the following initializations had taken place: Rows of Array1: 1 2 3 4 5 6 Rows of Array2: 1 2 3 4 5 0 Rows of Array3: 1 2 0 4 0 0 for (int row = 0; row < 2; row++) { for (int col = 0; col < 3; col++) { cout << setw(3) << Array1[row][col]; } cout << endl; }
  • 5. EXAMPLE: INPUT USING CIN  Nested for loops are often used when inputting and assigning values to a two-dimensional array.  Nested loops are generally useful for getting around the 2D arrays… for (int i=0; i<RSIZE; ++i) //every row for (int j=0; j<CSIZE; ++j )//every col cin >> table[i][j];
  • 6. 2-D ARRAYS AS PARAMETERS When passing a two-dimensional array as a parameter, the base address is passed, as is the case with one-dimensional arrays. But now the number of columns in the array parameter must be specified. This is because arrays are stored in row-major order, and the number of columns must be known in order to calculate the location at which each row begins in memory: address of element (r, c) = base address of array + r*(number of elements in a row)*(size of an element) + c*(size of an element) void Initialize(int TwoD[][NUMCOLS], const int NUMROWS) { for (int i = 0; i < NUMROWS; i++) { for (int j = 0; j < NUMCOLS; j++) TwoD[i][j] = -1; } }
  • 7. FUNCTION TO DISPLAY CONTENT OF A TWO DIMENSIONAL ARRAY A #include <iostream> #include <iomanip> using namespace std; void print(int A[][3],int N, int M) { for (int R = 0; R < N; R++){ cout <<endl; for (int C = 0; C < M; C++) cout << setw(10) <<A[R][C]; } }
  • 8. FUNCTION TO FIND THE SUM OF TWO DIMENSIONAL ARRAYS A AND B void addition(int A[][3], int B[][3],int N, int M) { for(int R=0;R<N;R++){ cout<<endl; for(int C=0;C<M;C++) cout<<setw(10) <<A[R][C]+B[R][C]; } }
  • 9. FUNCTION TO FIND & DISPLAY SUM OF ROWS & SUM OF COLS. OF A 2D ARRAY A void SumRowCol(int A[][20], int N, int M) { for(int R=0;R<N;R++) { int SumR=0; for(int C=0;C<M;C++) SumR+=A[R][C]; cout<<"Row("<<R<<")="<<SumR<<endl; } } for(int R=0;R<M;R++) { int SumC=0; for(int C=0;C<N;C++) SumC+=A[C][R]; cout<<"Column("<<R<<")="<<SumC<<endl; } }