SlideShare a Scribd company logo
3
Most read
7
Most read
8
Most read
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

PPTX
Arrays in Data Structure and Algorithm
PPTX
Hashing in datastructure
PPTX
2D Array
PPTX
Sparse matrix
PPT
Arrays in c
PPTX
Sparse matrix and its representation data structure
PPTX
PPTX
Introduction to database & sql
Arrays in Data Structure and Algorithm
Hashing in datastructure
2D Array
Sparse matrix
Arrays in c
Sparse matrix and its representation data structure
Introduction to database & sql

What's hot (20)

PPTX
sorting and its types
PPTX
ARRAY
PPT
Insertion sort bubble sort selection sort
PPT
Hash table
PPTX
Divide and conquer 1
PDF
Php array
PPT
Data Structure and Algorithms Heaps and Trees
PPTX
Introduction to Array ppt
PDF
Array data structure
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
PPTX
Data structure power point presentation
PPTX
Linked List
PPT
Two Dimensional Array
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Binary Tree in Data Structure
PPTX
PPTX
STRUCTURE OF SQL QUERIES
PPTX
Two dimensional arrays
sorting and its types
ARRAY
Insertion sort bubble sort selection sort
Hash table
Divide and conquer 1
Php array
Data Structure and Algorithms Heaps and Trees
Introduction to Array ppt
Array data structure
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Data structure power point presentation
Linked List
Two Dimensional Array
Array Introduction One-dimensional array Multidimensional array
Binary Tree in Data Structure
STRUCTURE OF SQL QUERIES
Two dimensional arrays
Ad

Similar to 2D arrays (20)

PPTX
Basic of array and data structure, data structure basics, array, address calc...
PPTX
Chapter 13.pptx
PPTX
PDF
array-191103180006.pdf
PPTX
C (PPS)Programming for problem solving.pptx
PPTX
2- Dimensional Arrays
PPTX
The document discusses arrays in data structures using Cpp programming langua...
PPT
Lecture 15 - Array
PPTX
Ch 8 Multidimensional Array in oop as per gtu.pptx
PPTX
arrays.pptx
PPTX
TwoDArrays which can easily help you understand java 2D arrays.pptx
PPTX
3 DS-Arrays.pptx 3 DS-Arrays.pptx 3 DS-Arrays.pptx
PPT
array2d.ppt
PPTX
Arrays in C++
PPT
Arrays
PPT
Multi dimensional arrays
PPT
ReviewArrays.ppt
PPT
C++ Arrays
PPT
C++ Arrays
PPT
Use syntax highlighting for C++ code (via plugins like "Highlight Code" for P...
Basic of array and data structure, data structure basics, array, address calc...
Chapter 13.pptx
array-191103180006.pdf
C (PPS)Programming for problem solving.pptx
2- Dimensional Arrays
The document discusses arrays in data structures using Cpp programming langua...
Lecture 15 - Array
Ch 8 Multidimensional Array in oop as per gtu.pptx
arrays.pptx
TwoDArrays which can easily help you understand java 2D arrays.pptx
3 DS-Arrays.pptx 3 DS-Arrays.pptx 3 DS-Arrays.pptx
array2d.ppt
Arrays in C++
Arrays
Multi dimensional arrays
ReviewArrays.ppt
C++ Arrays
C++ Arrays
Use syntax highlighting for C++ code (via plugins like "Highlight Code" for P...
Ad

More from Mehedi Hasan Raju (8)

PPTX
FPGAs versus GPUs in Data centers
PPTX
Evaluating and reducing cloud waste and cost—A data-driven case study from Az...
PPTX
Result Management System
PPTX
PPTX
Representation of signals
PPTX
Bit error rate
PPTX
Vector space
PPTX
Inverse function
FPGAs versus GPUs in Data centers
Evaluating and reducing cloud waste and cost—A data-driven case study from Az...
Result Management System
Representation of signals
Bit error rate
Vector space
Inverse function

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PDF
Insiders guide to clinical Medicine.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
master seminar digital applications in india
PPTX
Lesson notes of climatology university.
PDF
Complications of Minimal Access Surgery at WLH
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Cell Structure & Organelles in detailed.
PDF
Classroom Observation Tools for Teachers
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
GDM (1) (1).pptx small presentation for students
Pharma ospi slides which help in ospi learning
Insiders guide to clinical Medicine.pdf
O7-L3 Supply Chain Operations - ICLT Program
Basic Mud Logging Guide for educational purpose
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
master seminar digital applications in india
Lesson notes of climatology university.
Complications of Minimal Access Surgery at WLH
Module 4: Burden of Disease Tutorial Slides S2 2025
VCE English Exam - Section C Student Revision Booklet
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Cell Structure & Organelles in detailed.
Classroom Observation Tools for Teachers
O5-L3 Freight Transport Ops (International) V1.pdf
GDM (1) (1).pptx small presentation for students

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.