SlideShare a Scribd company logo
2
Most read
4
Most read
5
Most read
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

PPT
Two dimensional array
PPTX
ARRAY
PPT
Arrays in c
PPTX
Data types in c++
PPT
One Dimensional Array
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Presentation on array
PPTX
Two dimensional array
ARRAY
Arrays in c
Data types in c++
One Dimensional Array
Array Introduction One-dimensional array Multidimensional array
Presentation on array

What's hot (20)

PPTX
2D Array
PPT
PPT
Stacks
PPTX
Arrays in c
PPT
Strings
PPT
Queue implementation
PPTX
concept of Array, 1D & 2D array
PPTX
Queue in Data Structure
PPT
Array
PDF
13. Pointer and 2D array
PPTX
PPTX
PPTX
List in Python
PDF
Arrays in python
PPTX
Array in c programming
PPTX
Application of Stack - Yadraj Meena
PPTX
Array in c language
PPT
C++ Arrays
PPTX
Two dimensional arrays
PDF
sparse matrix in data structure
2D Array
Stacks
Arrays in c
Strings
Queue implementation
concept of Array, 1D & 2D array
Queue in Data Structure
Array
13. Pointer and 2D array
List in Python
Arrays in python
Array in c programming
Application of Stack - Yadraj Meena
Array in c language
C++ Arrays
Two dimensional arrays
sparse matrix in data structure
Ad

Similar to 2- Dimensional Arrays (20)

PDF
Chapter13 two-dimensional-array
PPTX
C (PPS)Programming for problem solving.pptx
PPT
Fp201 unit4
PPTX
Arrays with the help of Computer Programming
PPT
Array 31.8.2020 updated
PPT
PDF
PPTX
Arrays in C++
PPTX
Arrays & Strings.pptx
PPTX
Chapter 13.pptx
PPTX
Structured data type
PPT
Lecture#5-Arrays-oral patholohu hfFoP.ppt
PDF
Arrays and library functions
PDF
ARRAYS
PPT
Computer Programming- Lecture 9
PDF
Introduction to Arrays in C
PPT
Arrays in c programing. practicals and .ppt
PPT
PDF
Arrays and strings in c++
PPT
19-Lec - Multidimensional Arrays.ppt
Chapter13 two-dimensional-array
C (PPS)Programming for problem solving.pptx
Fp201 unit4
Arrays with the help of Computer Programming
Array 31.8.2020 updated
Arrays in C++
Arrays & Strings.pptx
Chapter 13.pptx
Structured data type
Lecture#5-Arrays-oral patholohu hfFoP.ppt
Arrays and library functions
ARRAYS
Computer Programming- Lecture 9
Introduction to Arrays in C
Arrays in c programing. practicals and .ppt
Arrays and strings in c++
19-Lec - Multidimensional Arrays.ppt
Ad

More from Education Front (20)

PPT
Improving Pronunciation
PPTX
Generic Software Process Models
PPTX
Problem Sloving
PPTX
Problem Solving - 1
PPTX
Introduction To Stack
PPTX
Process Models
PPTX
Process Models
PPTX
Data Representation
PPTX
Introduction to Algorithm
PPT
Revised Process of Communication
PPT
Importance of Language in Communication
PPT
Lecture1 (SE Introduction)
PPTX
Lecture 2 (Software Processes)
PPTX
Introduction to data structure
PPTX
Facing Today’s Communication Challenges
PPTX
Processor Basics
PPTX
Register & Memory
PPTX
Data Representation
PPTX
Computer Evolution
PPT
Assembly Language Basics
Improving Pronunciation
Generic Software Process Models
Problem Sloving
Problem Solving - 1
Introduction To Stack
Process Models
Process Models
Data Representation
Introduction to Algorithm
Revised Process of Communication
Importance of Language in Communication
Lecture1 (SE Introduction)
Lecture 2 (Software Processes)
Introduction to data structure
Facing Today’s Communication Challenges
Processor Basics
Register & Memory
Data Representation
Computer Evolution
Assembly Language Basics

Recently uploaded (20)

PPT
Mechanical Engineering MATERIALS Selection
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Foundation to blockchain - A guide to Blockchain Tech
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Geodesy 1.pptx...............................................
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Digital Logic Computer Design lecture notes
PPTX
additive manufacturing of ss316l using mig welding
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
ETO & MEO Certificate of Competency Questions and Answers
Mechanical Engineering MATERIALS Selection
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Foundation to blockchain - A guide to Blockchain Tech
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
OOP with Java - Java Introduction (Basics)
Geodesy 1.pptx...............................................
Model Code of Practice - Construction Work - 21102022 .pdf
Digital Logic Computer Design lecture notes
additive manufacturing of ss316l using mig welding
Arduino robotics embedded978-1-4302-3184-4.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
ETO & MEO Certificate of Competency Questions and Answers

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