Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

University of Engineering and Technology, Taxila

Experiment 4
Arrays : Implementation of multidimensional arrays

CLO-2: Use modern tools to solve problems of varying complexities in data


structures.
CLO-3: Construct the projects of varying complexities using different data
structures
CLO-4: Demonstrate a unique solution of problem under discussion
University of Engineering and Technology, Taxila 1
Two Dimensional Arrays:

Similar to the 1D array, we must specify the data type, the name, and the size of the array.
But the size of the array is described as the number of rows and number of columns. For
example:
int a[MAX_ROWS][MAX_COLS];

This declares a data structure that looks like:

In the case of an array, our old-fashioned one-dimensional array looks like this:

int[] myArray = {0,1,2,3};

And a two-dimensional array looks like this:

int[][] myArray = { {0,1,2,3}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };

For our purposes, it is better to think of the two-dimensional array as a matrix. A matrix can
be thought of as a grid of numbers, arranged in rows and columns, kind of like a bingo board.
We might write the two-dimensional array out as follows to illustrate this point:

int[][] myArray = { {0, 1, 2, 3},


{3, 2, 1, 0},
{3, 5, 6, 1},
{3, 8, 3, 4} };

Like 1D arrays, we can access individual cells in a 2D array by using subscripting


expressions giving the indexes, only now we have two indexes for a cell: its row index and its
column index. The expressions look like:
a[i][j] = 0;
or
x = a[row][col];

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 2
We can initialize all elements of an array to 0 like:
for(i = 0; i < MAX_ROWS; i++)
for(j = 0; j < MAX_COLS; j++)
a[i][j] = 0;

AIM: Multiplication of two Matrices


Description: Here A is a two – dimensional array with M rows and N
columns. B is also a two – dimensional array with X rows and Y columns. This
algorithm multiplies these two arrays. The output of the process will be matrix P with
M rows and Y columns.

Algorithm: (Multiplication of two Matrices)


1. Get dimensions of Matrix- A from user i.e. M x N
[ Read values for Matrix- A ]
2. Repeat for I = 0 to M-1
3. Repeat for J = 0 to N-1
4. Read A[I][J] [ Get value from user ]
[ End of Step-3 for Loop ]
[ End of Step-2 for Loop ]
5. Get dimensions of Matrix- B from user i.e. X x Y
[ Read values for Matrix- B ]
6. Repeat for I = 0 to X-1
7. Repeat for J = 0 to Y-1
8. Read B[I][J] [ Get value from user ]
[ End of Step-7 for Loop ]
[ End of Step-6 for Loop ]
9. If (N ≠ X) then
Write: “Multiplication is not possible, incompatible dimensions”. and exit
10. Repeat for I = 0 to M-1
11. Repeat for J = 0 to Y-1
12. Set P[I][J] = 0
13. Repeat For K = 0 to X-1
14. Set P[I][J] = P[I][J] + A[I][K] * B[K][J]
[ End of Step-13 for Loop ]
[ End of Step-11 for Loop ]
[ End of Step-10 for Loop]
[End of If of step-9 ]
15. Print the Matrix-P
16. End

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 3
Task 1:
This program asks user to enter the size of the matrix (rows and columns).

Then, it asks the user to enter the elements of two matrices and finally it multiplies, adds and
subtracts two matrix and displays the result.

To perform this task three main things to cover are:

1. To take matrix elements from user


2. To display option for Multiply ,Add and Subtract two matrices
3. To display the resultant matrix after Users selected operation.

Help code:
#include<iostream.h>
#include<iomanip.h>
int main()
{ int M,N,X,Y,I,J,K;
int A[20][20], B[20][20], P[20][20];
cout<<"\n\n *** First Matrix ***"<<endl;
cout<<"Number of Rows : ";
cin>>M;
cout<<"Number of Columns : ";
cin>>N;
cout<<"\n Enter The First Matrix"<<endl;
for (I=0 ; I<M ; I++ )
for (J=0 ; J<N ; J++) cin>>A[I][J];
cout<<"\n\n\n *** Second Matrix ***"<<endl;
cout<<"Number of Rows : ";
cin>>X;
cout<<"Number of Columns : ";
cin>>Y;
cout<<"\n \n Enter The Second Matrix"<<endl;
for (I=0 ; I<X ; I++ )
for (J=0 ; J<Y ; J++) cin>>B[I][J];
cout<<"\n\n The First Matrix You Entered"<<endl;
for (I=0 ; I<M ; I++)
{
for (J=0 ; J<N ; J++) cout<<A[I][J]<<"\t ";
cout<<"\n";
}
cout<<"\n\n The Second Matrix You Entered"<<endl;
for (I=0 ; I<X ; I++)
{
for (J=0 ; J<Y ; J++) cout<<B[I][J]<<"\t ";
cout<<endl"\n";
}
// (Column of first matrix-A must equal to Row of second matrix-B)

// The Dimensions of Product matrix-P will be M x Y

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 4

Experiment #02 (Submit in hard and soft form before next lab, late/copied code
will be marked as zero)

Question# 01: Given an N X N integer matrix, rotate it bye 90 degrees in place. In-place
means minimal extra memory to be used, i.e. don’t make a new array to copy into). Rotate
clockwise means top-row becomes right-column, right column becomes bottom-row etc.
eg.

[1, 2, 3, 4] [9, 6, 9, 1]
[9, 8, 5, 6] –> [2, 5, 8, 2]
[6, 5, 3, 7] [6, 3, 5, 3]
[9, 2, 6, 8] [8, 7, 6, 4]

Question# 02: Write user defined functions for square matrix to calculate

1. Left diagonal sum


2. Right diagonal sum

Engr. Rabia Arshad

You might also like