Docsity

Understanding 2-D Arrays in C++: Initialization, Display, and Input, Schemes and Mind Maps of Introduction to Computing

Partial preview of the text

Download Understanding 2-D Arrays in C++: Initialization, Display, and Input and more Schemes and Mind Maps Introduction to Computing in PDF only on Docsity!

Programming Fundamentals

Lecture-

2-D Arrays

2

Two-dimensional Arrays

  • A two-dimensional array is like several

identical arrays put together or array of an

array, known as a multidimensional array.

For example int x[3][4];

  • Here, x is a two-dimensional array. It can hold

a maximum of 12 elements.

Multi-dimensional Array

• Three-dimensional arrays also work in a

similar way.

For example: float x[2][4][3];

• This array x can hold a maximum of 24

elements.

• We can find out the total number of elements

in the array simply by multiplying its

dimensions:

2 x 4 x 3 = 24

Initializing a 2-D Array

• A multidimensional array can be initialized in

more than one way, for example

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

• This method is not preferred and all a 2-d

array elements are initialized as

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

• This array has 2 rows and 3 columns, which is

why we have two rows of elements with 3

elements each.

Displaying the contents of a 2-d array

// C++ Program to display all elements // of an initialised two
dimensional array
#include
using namespace std;
int main()
int test[3][2] = {{2, -5}, {4, 0}, {9, 1}};
// use of nested for loop
// access rows of the array
for (int i = 0; i < 3; ++i)
// access columns of the array
for (int j = 0; j < 2; ++j)
cout << "test[" << i << "][" << j << "] = " << test[i][j] << endl;
} return 0;

Output:

test[0][0] = 2

test[0][1] = -

test[1][0] = 4

test[1][1] = 0

test[2][0] = 9

test[2][1] = 1

Code Description

• In the example on previous slide, we have

initialized a two-dimensional int array

named test that has 3 "rows" and 2 "columns".

• Here, we have used the nested for loop to display

the array elements.

• the outer loop from i == 0 to i == 2 access the

rows of the array

• the inner loop from j == 0 to j == 1 access the

columns of the array

• Finally, we print the array elements in each

iteration.

Code Description

• Here, we have used a nested for loop to take

the input of the 2-d array.

• Once all the input has been taken, we have

used another nested for loop to print the

array members.

11

Arrays of Strings

• A two-dimensional array of characters can be

used as an array of C-strings.

13

Program (continued)

Program Output

January has 31 days.

February has 28 days.

March has 31 days.

April has 30 days.

May has 31 days.

June has 30 days.

July has 31 days.

August has 31 days.

September has 30 days.

October has 31 days.

November has 30 days.

December has 31 days.

Matrix Addition using 2-D Array

• In the example program, user is asked to enter

the number of rows r and columns c. The

value of r and c should be less than 100 in this

program.

• The user is asked to enter elements of two

matrices (of order r*c).

• Then, the program adds these two matrices,

saves it in another matrix (two-dimensional

array) and displays it on the screen.

// Adding Two matrices

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

sum[i][j] = a[i][j] + b[i][j];

// Displaying the resultant sum matrix.

cout << endl << "Sum of two matrix is: " << endl;

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

{

cout << sum[i][j] << " ";

if(j == c - 1)

cout << endl;

}

return 0;

}

Program (continued)

Output

Enter number of rows (between 1 and 100): 2

Enter number of columns (between 1 and 100): 2

Enter elements of 1st matrix:

Enter element a11: -

Enter element a12: 5

Enter element a21: 6

Enter element a22: 8

Enter elements of 2nd matrix:

Enter element b11: 3

Enter element b12: -

Enter element b21: 7

Enter element b22: 2

Sum of two matrix is:

Memory Layout of a 2-D Array

Addressing and Arrays

• 1-D Array name works as a pointer to the base

element (first element) of the array. However in

the case 2D arrays the logic is slightly different.

You can consider a 2D array as collection of

several one dimensional arrays.

• So abc[0] would have the address of first element

of the first row (if we consider the above diagram

number 1).

similarly abc[1] would have the address of the

first element of the second row.