Experiment 4: University of Engineering and Technology, Taxila
Experiment 4: University of Engineering and Technology, Taxila
Experiment 4
Arrays : Implementation of multidimensional 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];
In the case of an array, our old-fashioned one-dimensional array looks like this:
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:
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.
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)
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