SlideShare a Scribd company logo
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

Array
ArrayArray
Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sorting
Ajharul Abedeen
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
أحمد محمد
 
Arrays
ArraysArrays
Sql subquery
Sql  subquerySql  subquery
Sql subquery
Raveena Thakur
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
Smit Parikh
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
Mazharul Islam
 
Array in c++
Array in c++Array in c++
Array in c++
Mahesha Mano
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
DevoAjit Gupta
 
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
Recursion
RecursionRecursion
Recursion
Abdur Rehman
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
Vardhil Patel
 
Array
ArrayArray
Array in c
Array in cArray in c
Array in c
Ravi Gelani
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
Sajid Hasan
 
Python recursion
Python recursionPython recursion
Python recursion
Prof. Dr. K. Adisesha
 

What's hot (20)

Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
Array
ArrayArray
Array
 
Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sorting
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Arrays
ArraysArrays
Arrays
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Array in c++
Array in c++Array in c++
Array in c++
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Recursion
RecursionRecursion
Recursion
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
Array
ArrayArray
Array
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
Array in c
Array in cArray in c
Array in c
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Python recursion
Python recursionPython recursion
Python recursion
 

Similar to 2- Dimensional Arrays

Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-arrayDeepak Singh
 
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
rohinitalekar1
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
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++
Kashif Nawab
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
AnkurRajSingh2
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
AnisZahirahAzman
 
Structured data type
Structured data typeStructured data type
Structured data type
Omkar Majukar
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
Dr. Md. Shohel Sayeed
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
GC University Faisalabad
 
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 arraysAseelhalees
 
Unit 2
Unit 2Unit 2
Unit 2
TPLatchoumi
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
naveed jamali
 

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

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

Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Steel & Timber Design according to British Standard
Steel & Timber Design according to British StandardSteel & Timber Design according to British Standard
Steel & Timber Design according to British Standard
AkolbilaEmmanuel1
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABSDESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
itech2017
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 

Recently uploaded (20)

Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Steel & Timber Design according to British Standard
Steel & Timber Design according to British StandardSteel & Timber Design according to British Standard
Steel & Timber Design according to British Standard
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABSDESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 

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