Professional Documents
Culture Documents
Lecture Link:
https://www.youtube.com/watch?v=ElVzkB2z9eA&list=PLVEVLI2v6thVDz7UxUPnU
RUKaqWFK7Z7v&index=26
Two Dimensional Array
type arrayName [ x ][ y ];
int a [ 3 ] [ 4 ] ;
A two-dimensional array can be think 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 −
Cont..
a [rowIndex ] [ columnIndex ]
int a [ 3 ] [ 4 ] ;
Elements = 3X4 = 12
Initialization of two-dimensional array
int main () {
// an array with 5 rows and 2 columns.
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}
return 0;
}
Initializing Two Dimensional Array-Example
#include <iostream>
using namespace std;
int main () {
int maxRows = 2;
int maxCols = 3 ;
int matrix [ 2] [ 3 ];
int row , col ;
for ( row = 0 ; row < maxRows ; row ++ )
{
for ( col = 0 ; col < maxCols ; col ++ )
{
cout << "Please enter value of "<< row << "" << col;
cin >> matrix [ row ] [ col ] ;
}
}
}
cout <<"\n";
}}
Memory map of 2-Demensional array
[0] 5 2 9
[1] 7 0 4
Square Matrix
Number of rows are equal to number of columns
rows
arraySize = cols
Square Matrix
a ij = a ji
i = rows
j = columns
Example-Transpose of Matrix
#include <iostream>
using namespace std;
int main() {
int a[10][10], transpose[10][10], row, column, i, j;
return 0;
}
Three Dimensional Arrays
Lecture Link:
https://www.youtube.com/watch?v=UfUKFAfHqyo&list=PLVEVLI2v6thVDz7UxUPnURU
KaqWFK7Z7v&index=27
Three Dimensional
Arrays
Three dimensional (3D) array contains three for loops in
programming. So, to initialize and print three dimensional
array, you have to use three for loops. Third for loop
(the innermost loop) forms 1D array, Second for loop
forms 2D array and the third for loop (the outermost
loop) forms 3D array, as shown here in the following
program.
int x [ ] [ ] [ ] ;
3-D Array
Initializing Three-Dimensional Array
int main() {
int arr[3][4][2] = { { {1, 2}, {3, 4}, {5, 6}, {7, 8} },
{ {9, 10}, {11, 12}, {13, 14}, {15, 16} },
{ {17, 18}, {19, 20}, {21, 22}, {23, 24} } };
cout<<"arr[0][0][0] = "<<arr[0][0][0]<<"\n";
cout<<"arr[0][2][1] = "<<arr[0][2][1]<<"\n";
cout<<"arr[2][3][1] = "<<arr[2][3][1]<<"\n";
}
Here, the outer array has three elements, each of which
is a 2-D array of four 1-D arrays, each of which contains
two integers. It means, a 1-D array of two elements is
constructed first. Then four such 1-D arrays are placed
one below the other to give a 2-D array containing four
rows. Then, three such 2-D arrays are placed one behind
the other to yield a 3-D array containing three 2-D arrays.
Accessing elements in Three-Dimensional
Arrays
Lecture Link:
https://www.youtube.com/watch?v=j6o-
IYTn6Uk&list=PLVEVLI2v6thVDz7UxUPnURUKaqWFK7Z7v&index=28
Linear or Sequential Search in Array