You are on page 1of 30

Two Dimensional Array

Lecture Link:
https://www.youtube.com/watch?v=ElVzkB2z9eA&list=PLVEVLI2v6thVDz7UxUPnU
RUKaqWFK7Z7v&index=26
Two Dimensional Array

The simplest form of the


multidimensional array is the two-
dimensional array. To declare a two-
dimensional integer array of size x,y
you would write syntax as follows .

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..

 Thus, every element in array a is identified


by an element name of the form a[ i ][ j ],
where a is the name of the array, and i and
j are the subscripts that uniquely identify
each element in a.
Addressing Array Elements

a [rowIndex ] [ columnIndex ]

 We can find out the total number of elements in the array


simply by multiplying its dimensions.

int a [ 3 ] [ 4 ] ;
Elements = 3X4 = 12
Initialization of two-dimensional array

 int test[2][3] = {2, 4, 5, 9, 0, 19};

 The above method is not preferred. A


better way to initialize this array with the
same array elements is given below:

 int test[2][3] = { {2, 4, 5}, {9, 0, 19}};


Cont..

 This array has 2 rows and 3 columns, which


is why we have two rows of elements with 3
elements each.
Accessing 2-D Array Elements
#include <iostream>
using namespace std;

int main () {
// an array with 5 rows and 2 columns.
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};

// output each array element's value


for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ ) {

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

for ( row = 0 ; row < maxRows ; row ++ )


{
for ( col = 0 ; col < maxCols ; col ++ )
{
cout<< matrix [ row ] [ col ] << "\t";

}
cout <<"\n";
}}
Memory map of 2-Demensional array

After first outer loop


Input
[0]
5 2 9

After second outer


loop Input

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

cout << "Enter rows and columns of matrix: ";


cin >> row >> column;

cout << "\nEnter elements of matrix: " << endl;

// Storing matrix elements


for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << "Enter element a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
}

// Printing the a matrix


cout << "\nEntered Matrix: " << endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << " " << a[i][j];
if (j == column - 1)
cout << endl << endl;
}
}
Cont..
// Computing transpose of the matrix
for (int i = 0; i < row; ++i)
for (int j = 0; j < column; ++j) {
transpose[j][i] = a[i][j];
}

// Printing the transpose


cout << "\nTranspose of Matrix: " << endl;
for (int i = 0; i < column; ++i)
for (int j = 0; j < row; ++j) {
cout << " " << transpose[i][j];
if (j == row - 1)
cout << endl << endl;
}

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

 Initialization in Three-Dimensional array is same as that of


Two-dimensional arrays. The difference is as the number of
dimension increases so the number of nested braces will
also increase.

 A three dimensional (3D) array can be thought of as an


array of arrays of arrays. A simple C++ program to
initialize three-dimensional (3D) array of dimensions 3*4*2,
then it will access some elements present in the array and
display the element on the screen :
3-D Array Example
#include <iostream>
using namespace std;

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

 Accessing elements in Three-Dimensional


Arrays is also similar to that of Two-
Dimensional Arrays. The difference is we
have to use three loops instead of two
loops for one additional dimension in
Three-dimensional Arrays.
3-D Array Example
#include <iostream>
using namespace std;
int main() {
// This array can store upto 12 elements (2x3x2)
int test[2][3][2] = {
{
{1, 2},
{3, 4},
{5, 6}
},
{
{7, 8},
{9, 10},
{11, 12}
}
};
// Displaying the values with proper index.
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 2; ++k) {
cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl;
}
}}
return 0;
}
Searching and Sorting in
Array

 Lecture Link:
 https://www.youtube.com/watch?v=j6o-
IYTn6Uk&list=PLVEVLI2v6thVDz7UxUPnURUKaqWFK7Z7v&index=28
Linear or Sequential Search in Array

 A linear search, also known as a sequential search, is a


method of finding an element within an array. It checks
each element of the array sequentially until a match is
found for a particular element or the whole array has been
searched.

 If you are looking for an element that is near the front of


the array, the sequential search will find it quickly. The
more data that must be searched, the longer it will take to
find the data that matches the key using this process.
Algorithm to search an element in array
using linear search

 Take number of elements in array as input from user and


store it in a variable N.
 Using a loop, take N numbers as input from user and store it
in array(Let the name of the array be inputArray).
 Ask user to enter element to be searched. Let it be num.
 Now, using a for loop, traverse inputArray from index 0 to N-
1 and compare num with every array element. If num is equal
to any array element then print a message saying "Element
found at index 4" otherwise print "Element Not Present".
C++ Program to Implement Linear Search
 #include<iostream>
 using namespace std;
 int main()
 {
 int arr[10], i, num, index;
 cout<<"Enter 10 Numbers: ";
 for(i=0; i<10; i++)
 cin>>arr[i];
 cout<<"\nEnter a Number to Search: ";
 cin>>num;
 for(i=0; i<10; i++)
 {
 if(arr[i]==num)
 {
 index = i;
 break;
 }
 }
 cout<<"\nFound at Index No."<<index;
 cout<<endl;
 return 0;
 }
Sorting Array Elements
 A sorted array is an array in which each of the elements are
sorted in some order such as numerical, alphabetical etc.
There are many algorithms to sort a numerical array such as
bubble sort, insertion sort, selection sort, merge sort, quick
sort, heap sort etc. we discuss bubble sort algorithm to sort
an array.
 In the bubble sort, as elements are sorted they gradually
"bubble" (or rise) to their proper location in the array, like
bubbles rising in a glass of soda. The bubble sort repeatedly
compares adjacent elements of an array. The first and
second elements are compared and swapped if out of
order. Then the second and third elements are compared
and swapped if out of order. This sorting process continues
until the last two elements of the array are compared and
swapped if out of order.
Bubble sort
 In the bubble sort, as elements are sorted they
gradually "bubble" (or rise) to their proper location in
the array, like bubbles rising in a glass of soda. The
bubble sort repeatedly compares adjacent
elements of an array. The first and second elements
are compared and swapped if out of order. Then the
second and third elements are compared and swapped
if out of order. This sorting process continues until
the last two elements of the array are compared and
swapped if out of order.
Bubble Sort Algorithms

 Step 1: For i = 0 to N-1 repeat Step 2


Step 2: For J = i + 1 to N – I repeat
Step 3: if A[J] > A[i]
Swap A[J] and A[i]
[End of Inner for loop]
[End of Outer for loop]
Step 4: Exit
Example
#include<iostream>
using namespace std;
int main ()
{
int i, j,temp,pass=0;
int a[10] = {10,2,0,14,43,25,18,1,5,45};
cout <<"Input list ...\n";
for(i = 0; i<10; i++) {
cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<10; i++) {
for(j = i+1; j<10; j++)
{
if(a[j] < a[i]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
pass++;
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<10; i++) {
cout <<a[i]<<"\t";
}
cout<<"\nNumber of passes taken to sort the list:"<<pass<<endl;
return 0;
}

You might also like