SlideShare a Scribd company logo
1 of 11
2D Arrays: Defining, Accessing, Usages
Mehedi Hasan Raju
2D Arrays: Definition
A two-dimensional array (2D array) is, in essence, a list of one-dimensional arrays. It is the simplest
form of the multidimensional array. To declare a two-dimensional integer array of size x, y −
data_type array_Name [ x ] [ y ];
int 2D_array [10] [20];
What is the size of a 2D array?
The total number of elements that can be stored in a multidimensional array can be calculated
by multiplying the size of all the dimensions.
type arrayName [ x ] [ y ]; -> (x * y)
int 2D_array [10] [20]; -> (10 * 20) -> 200
int x[5][10][20] -> ?
2D Arrays: Definition
A two-dimensional array can be thought as a table, which will have x number of rows and y number of
columns. A 2-dimensional array a, which contains three rows, and four columns can be shown
as below −
Thus, every element in array a is identified by an element name of the form a[ i ][ j ].
2D Arrays: Initialization
2D arrays may be initialized by specifying bracketed values for each row.
• Following is an array with 3 rows and each row have 4 columns.
int 2D_array[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{2, 3, 4, 5} , /* initializers for row indexed by 1 */
{4, 5, 6, 7} /* initializers for row indexed by 2 */
};
• The nested braces, which indicate the intended row, are optional.
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
2D Arrays: Accessing elements
An element in 2-dimensional array is accessed by using the subscripts
• row index and column index of the array
int val = a[2][3];
The above statement will take 4th element from the 3rd row of the array. You can verify it in the above diagram.
Row Column
2D Arrays: Accessing elements
• One dimensional array is accessed via for loop (usually).
• Similarly, 2D array can be accessed with a nested for loop.
for (int row = 0; row < RowNumber; row++){
for (int column = 0; column < Colnumber; column ++){
cout << 2D_array [row][column]<< endl;
}
}
2D Arrays: Accessing elements
#include<iostream>
using namespace std;
int main()
{
// initialization- an array with 3 rows and 4 columns.
int a[3][4] = {{0,1,2,3}, {2,3,4,5}, {4,5,6,7}};
int RowNumber = 3;
int ColNumber = 4;
// accessing the elements of the array output each array element's value
for (int row = 0; row < RowNumber; row++){
for (int column = 0; column < ColNumber; column++){
cout << " Element at [" << row <<"][" <<column<<"] is " << a[row][column]<<endl;
}
}
return 0;
}
2D Arrays: Accessing elements (Example)
----------Code------------
#include<iostream>
using namespace std;
int main()
{
// initialization- an array with 3 rows and 4 columns.
int a[3][4] = {{0,1,2,3}, {2,3,4,5}, {4,5,6,7}};
int RowNumber = 3;
int ColNumber = 4;
// accessing the elements of the array output each array element's value
for (int row = 0; row < RowNumber; row++){
for (int column = 0; column < ColNumber; column++){
cout << " Element at [" << row <<"][" <<column<<"] is " << a[row][column]<<endl;
}
}
return 0;
}
------Output-------
Element at [0][0] is 0
Element at [0][1] is 1
Element at [0][2] is 2
Element at [0][3] is 3
Element at [1][0] is 2
Element at [1][1] is 3
Element at [1][2] is 4
Element at [1][3] is 5
Element at [2][0] is 4
Element at [2][1] is 5
Element at [2][2] is 6
Element at [2][3] is 7
2D Arrays: Usages
• Data come naturally in the form of a table, e.g., spreadsheet
• Need a two-dimensional array.
• Multi dimension arrays are used if you want to put arrays inside an array.
• 10 students ----> 3 tests. You can create an array like: arr_name[10][3]
• call arr_name[0][0] -----> you the result of student 1 on lesson 1.
• Call arr_name[5][2] ------> ?
• Multi dimension is-
• easier to understand
• easier to debug
2D Arrays: Common mistakes
• using 1 as the first index
• 0 for rows and/or columns.
• using array.length as the last valid row index
• array.length - 1
• using the wrong starting and ending indicies on loops.
• using array.length for both the number of rows and columns.
• use array[0].length for the number of columns.
• jumping out a loop by using one or more return statements before every value has been processed.
Thank You

More Related Content

What's hot

What's hot (20)

Arrays in c
Arrays in cArrays in c
Arrays in c
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Python list
Python listPython list
Python list
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Arrays
ArraysArrays
Arrays
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Array in c
Array in cArray in c
Array in c
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
Singly Linked List & Data Structure
Singly Linked List & Data StructureSingly Linked List & Data Structure
Singly Linked List & Data Structure
 
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
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Python array
Python arrayPython array
Python array
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 

Similar to 2D arrays

Similar to 2D arrays (20)

Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Arrays
ArraysArrays
Arrays
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
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
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Arrays
ArraysArrays
Arrays
 
ReviewArrays.ppt
ReviewArrays.pptReviewArrays.ppt
ReviewArrays.ppt
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Array in C full basic explanation
Array in C full basic explanationArray in C full basic explanation
Array in C full basic explanation
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 

More from Mehedi Hasan Raju

More from Mehedi Hasan Raju (8)

FPGAs versus GPUs in Data centers
FPGAs versus GPUs in Data centersFPGAs versus GPUs in Data centers
FPGAs versus GPUs in Data centers
 
Evaluating and reducing cloud waste and cost—A data-driven case study from Az...
Evaluating and reducing cloud waste and cost—A data-driven case study from Az...Evaluating and reducing cloud waste and cost—A data-driven case study from Az...
Evaluating and reducing cloud waste and cost—A data-driven case study from Az...
 
Result Management System
Result Management SystemResult Management System
Result Management System
 
Waveguide
WaveguideWaveguide
Waveguide
 
Representation of signals
Representation of signalsRepresentation of signals
Representation of signals
 
Bit error rate
Bit error rateBit error rate
Bit error rate
 
Vector space
Vector spaceVector space
Vector space
 
Inverse function
Inverse functionInverse function
Inverse function
 

Recently uploaded

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 

2D arrays

  • 1. 2D Arrays: Defining, Accessing, Usages Mehedi Hasan Raju
  • 2. 2D Arrays: Definition A two-dimensional array (2D array) is, in essence, a list of one-dimensional arrays. It is the simplest form of the multidimensional array. To declare a two-dimensional integer array of size x, y − data_type array_Name [ x ] [ y ]; int 2D_array [10] [20]; What is the size of a 2D array? The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. type arrayName [ x ] [ y ]; -> (x * y) int 2D_array [10] [20]; -> (10 * 20) -> 200 int x[5][10][20] -> ?
  • 3. 2D Arrays: Definition A two-dimensional array can be thought as a table, which will have x number of rows and y number of columns. A 2-dimensional array a, which contains three rows, and four columns can be shown as below − Thus, every element in array a is identified by an element name of the form a[ i ][ j ].
  • 4. 2D Arrays: Initialization 2D arrays may be initialized by specifying bracketed values for each row. • Following is an array with 3 rows and each row have 4 columns. int 2D_array[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {2, 3, 4, 5} , /* initializers for row indexed by 1 */ {4, 5, 6, 7} /* initializers for row indexed by 2 */ }; • The nested braces, which indicate the intended row, are optional. int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
  • 5. 2D Arrays: Accessing elements An element in 2-dimensional array is accessed by using the subscripts • row index and column index of the array int val = a[2][3]; The above statement will take 4th element from the 3rd row of the array. You can verify it in the above diagram. Row Column
  • 6. 2D Arrays: Accessing elements • One dimensional array is accessed via for loop (usually). • Similarly, 2D array can be accessed with a nested for loop. for (int row = 0; row < RowNumber; row++){ for (int column = 0; column < Colnumber; column ++){ cout << 2D_array [row][column]<< endl; } }
  • 7. 2D Arrays: Accessing elements #include<iostream> using namespace std; int main() { // initialization- an array with 3 rows and 4 columns. int a[3][4] = {{0,1,2,3}, {2,3,4,5}, {4,5,6,7}}; int RowNumber = 3; int ColNumber = 4; // accessing the elements of the array output each array element's value for (int row = 0; row < RowNumber; row++){ for (int column = 0; column < ColNumber; column++){ cout << " Element at [" << row <<"][" <<column<<"] is " << a[row][column]<<endl; } } return 0; }
  • 8. 2D Arrays: Accessing elements (Example) ----------Code------------ #include<iostream> using namespace std; int main() { // initialization- an array with 3 rows and 4 columns. int a[3][4] = {{0,1,2,3}, {2,3,4,5}, {4,5,6,7}}; int RowNumber = 3; int ColNumber = 4; // accessing the elements of the array output each array element's value for (int row = 0; row < RowNumber; row++){ for (int column = 0; column < ColNumber; column++){ cout << " Element at [" << row <<"][" <<column<<"] is " << a[row][column]<<endl; } } return 0; } ------Output------- Element at [0][0] is 0 Element at [0][1] is 1 Element at [0][2] is 2 Element at [0][3] is 3 Element at [1][0] is 2 Element at [1][1] is 3 Element at [1][2] is 4 Element at [1][3] is 5 Element at [2][0] is 4 Element at [2][1] is 5 Element at [2][2] is 6 Element at [2][3] is 7
  • 9. 2D Arrays: Usages • Data come naturally in the form of a table, e.g., spreadsheet • Need a two-dimensional array. • Multi dimension arrays are used if you want to put arrays inside an array. • 10 students ----> 3 tests. You can create an array like: arr_name[10][3] • call arr_name[0][0] -----> you the result of student 1 on lesson 1. • Call arr_name[5][2] ------> ? • Multi dimension is- • easier to understand • easier to debug
  • 10. 2D Arrays: Common mistakes • using 1 as the first index • 0 for rows and/or columns. • using array.length as the last valid row index • array.length - 1 • using the wrong starting and ending indicies on loops. • using array.length for both the number of rows and columns. • use array[0].length for the number of columns. • jumping out a loop by using one or more return statements before every value has been processed.