0% found this document useful (0 votes)
6 views

Multidimensional Array

Uploaded by

Shan Priya S
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Multidimensional Array

Uploaded by

Shan Priya S
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Multi Dimensional Arrays

Multi Dimensional Arrays


• It is sometimes useful to arrange data into rows and columns (two dimensional
arrays), or even higher dimensionality.

•A particular cell in a three-dimensional array may be accessed as follows:


Arr_name[dim1][dim2]
Multi Dimensional Arrays -Example
• For eg., if there are eight students and ten questions, and the answers can be stored
in a two-dimensional array as given.

• Similarly, the distances between the cities can be represented using a two-
dimensional array as given.
Multi Dimensional Arrays
• An array can be declared with multiple dimensions.
2 Dimensional 3 Dimensional
Can you specify the dimension of the array and show how individual elements can
be accessed?

Answer….
A. 2X4 D. 3X3
B. 2X2 E. 3x1
C. 4X5 F. 5X3
One-Dimensional array

Two-Dimensional array

Array_name[row][column] - C/C++

Array_name[row, column] (or)


Array_name(row, column)
Multidimensional Arrays

A two-dimensional array consists of a certain number of rows and columns:

integer NUMROWS = 3;
integer NUMCOLS = 7;
Array[NUMROWS][NUMCOLS];

0 1 2 3 4 5 6
0 4 18 9 3 -4 6 0
1 12 45 74 15 0 98 0
2 84 87 75 67 81 85 79

Array[2][5] 3rd value in 6th column


Array[0][4] 1st value in 5th column

The declaration must specify the number of rows and the number of columns, and both must
be constants.
Processing a 2-D Array Row Row<2 Column Column
<3

A one-dimensional array is usually processed via a for loop. 0 true 0 true


Similarly, a two-dimensional array may be processed with a nested for 1 true
loop:
2 true
for (int Row = 0; Row < NUMROWS; Row++)
{ 3 False
for (int Col = 0; Col < NUMCOLS; Col++)
1 true 0 true
{
1 true
Array[Row][Col] = 0;
} 2 true
}
Example Array(2x3): 3 False
000
000 2 false

Each pass through the inner for loop will initialize all the elements of the
current row to 0.
Questions ?
• Given two matrix A and B, Can they multiplied?
Algorithm to Add two matrix

• Step 1: Start
• Step 2: Input the size of matrix 1 and matrix 2.
• Step 3: If (matrix1_row == matrix2_row) && (matrix1_column== matrix2_column)
step 3.1 :for i=1 to rows[matrix 1]
step 3.2 : for j=1 to columns [matrix 1]
step3.2.1: Input matrix 1 [i,j] Input matrix 2 [i,j]
step3.2.2: matrix 3 [i,j]= matrix 1 [i,j] + matrix 2 [i,j];

else addition not possible Similarly ,.. We can perform


matrix subtraction
• Step 4 :Display matrix 3 [i,j];
• Step 5: Stop
Write the algorithm to transpose the matrix.

You might also like