Computer Programming Lab 7
Lab 07: Multi Dimensional Arrays:
Task 1:
Write a program to
perform matrix multiplication of m x n
matrix. Given the condition if number of rows of first matrix equal to the number of rows of the second
matrix.
SourceCode:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int
r,c,x,y;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"
Enter Rows for 1st Matrice A"<<endl;
cin>>r;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"
Enter columns for 1st Matrice A"<<endl;
cin>>c;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<" Enter Rows for 1st Matrice B"<<endl;
cin>>x;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<" Enter columns for
1st Matrice B"<<endl;
cin>>y;
cout<<setw(35)<<setfill('=')<<"="<<endl;
int
m[r][c],n[x][y];
//enter values for matic A
cout<<"Enter Values for Matrix A"<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
for(int
i=0;i<r;i++)
{
for(int
j=0;j<c;j++)
{
cout<<"Enter
values of ["<<i<<"]["<<j<<"] is
";
cin>>m[i][j];
}
}
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Enter
Values for Matrix B"<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
//enter values for matric B
for(int
i=0;i<x;i++)
{
for(int
j=0;j<y;j++)
{
cout<<"Enter
values of ["<<i<<"]["<<j<<"] is
";
cin>>n[i][j];
}
}
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"After multiply matrix A and
Matrix B " <<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
int
o[r][y];
if(c==x)
{
for(int
i=0;i<r;i++)
{
for(int
j=0;j<y;j++)
{
o[i][j]=0;
for(int
k=0;k<c;k++)
{
o[i][j]=o[i][j]+(m[i][k]*n[k][j]);
}
}
}
//printing matricx after multiply
for(int
i=0;i<r;i++)
{
for(int
j=0;j<y;j++)
{
cout<<o[i][j]<<" ";
}
cout<<endl;
}
cout<<setw(35)<<setfill('=')<<"="<<endl;
}
else
{
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"make sure your column of first
matrice"<<endl;
cout<<" and rows of 2nd matrices
are equal "<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
}
return 0;
}
Result:
Task 2:
Write
a C++ Program that computes the sum of two matrices. Each matrix is of 2 rows
and 2 columns and will be created from user input.
Output
of the program is as follows:
Enter
[0][0] of Matrix A: 2
Enter
[0][1] of matrix A: 3
Enter
[1][0] of matrix A: 4
Enter
[1][1] of matrix A: 5
Enter
[0][0] of Matrix B: 6
Enter
[0][1] of matrix B: 7
Enter
[1][0] of matrix B: 8
Enter
[1][1] of matrix B: 9
A
= 2 3 + B
= 6 7 = C
= 8 10
4 5 8 9 12 14
Sourcecode:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int
m[2][2],n[2][2],sum[2][2];
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Enter
valuse for matrices A: "<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
for(int
i=0;i<2;i++)
{
for(int
j=0;j<2;j++)
{
cout<<"enter"<<"["<<i<<"]"<<"["<<j<<"]
of matrix A ";
cin>>m[i][j];
}
}
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Enter
valuse for matrices B: "<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
for(int
i=0;i<2;i++)
{
for(int
j=0;j<2;j++)
{
cout<<"enter"<<"["<<i<<"]"<<"["<<j<<"]
of matrix B ";
cin>>n[i][j];
}
}
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"the
sum of A and B matrices is "<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
for(int i=0;i<2;i++)
{
for(int
j=0;j<2;j++)
{
sum[i][j]=m[i][j]+n[i][j];
}
}
for(int i=0;i<2;i++)
{
for(int
j=0;j<2;j++)
{
cout<<sum[i][j]<<"
";
}
cout<<endl;
}
cout<<setw(35)<<setfill('=')<<"="<<endl;
return
0;
}
Result:
Task 3:
task3
Write a C++ program to calculate the result of three
sections of a semester. Following are the rules for result:
- Three are 3 sections.
- Each section has 8
students.
- Each student takes 5
courses.
Marks for each subject of every student of each section must
be taken from the user.
Calculate the result of every student of each section as
follows:
- Obtained marks (Sum of all
courses marks). Max. marks of each subject are 100
Percentage
Souce code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int
x[3][8][5];
double
percentage;
cout<<setw(35)<<setfill('=')<<"="<<endl;
for(int
i=0;i<3;i++)
{
for(int
j=0;j<8;j++)
{ int sum=0;
for(int
k=0;k<5;k++)
{
cout<<"
section"<<i+1<<" course "<<k+1<< "
student "<<j+1<<" marks are: ";
cin>>x[i][j][k];
sum=sum+x[i][j][k];
}
cout<<setw(35)<<setfill('=')<<"="<<endl;
percentage=(sum/500.00)*100.0;
cout<<"total
marks obtained by student
"<<j+1<<" is "<<sum<<" and his percentage is
"<<percentage<<"%"<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
}
}
return
0;
}
Result:
Task
4:
Write a C++
program, that read 12 integer values from user, store values in Matrix of 4 X
3. Create another Matrix of 4 X 3, divide each element of Matrix1 by five, and
store the result in the Matrix2.
Print
Matrix A, with heading shown, correctly spaced.
Print
Matrix B, with heading shown, correctly spaced.
Your
Program should display output as follows
Source Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int
m[4][3];
double
n[4][3];
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<" Enter Values for matrice
A "<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
for(int
i=0;i<4;i++)
{
for(int
j=0;j<3;j++)
{
cout<<"Value
of ["<<i<<"]["<<j<<"] is ";
cin>>m[i][j];
}
}
//cout
first matrix
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<" Values for matrice A "<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
for(int
i=0;i<4;i++)
{
for(int
j=0;j<3;j++)
{
cout<<m[i][j]<<" ";
}
cout<<endl;
}
//dividing
1st matrices by 5 nd saving in 2nd matrices
for(int
i=0;i<4;i++)
{
for(int
j=0;j<3;j++)
{
n[i][j]=m[i][j]/5.00;
}
}
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<" Matrix A- Divided by 5 "<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl<<endl<<endl;
//print values of 2nd
matrices after dividing by 5
for(int i=0;i<4;i++)
{
for(int
j=0;j<3;j++)
{
cout<<n[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Result:
Task 5:
Write a C++
program to calculate the sum of each row and
colum of a two-dimensional (2D) array of size RxC. for example:
Your Program
should display output as follows:
Sum
Source code:
#include
<iostream>
#include
<iomanip>
using
namespace std;
int
main()
{
int
r ,c;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<" Enter Number of
Rows"<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cin>>r;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Enter Numbers of
Columns"<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cin>>c;
int
X[r][c];
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Enter the values for
matrices "<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
for(int i=0;i<r;i++)
{
for(int
j=0;j<c;j++)
{
cout<<"value
of ["<<i<<"]["<<j<<"] is ";
cin>>X[i][j];
}
}
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"The
matric values are "<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
for(int
i=0;i<r;i++)
{
for(int
j=0;j<c;j++)
{
cout<<X[i][j]<<" ";
}
cout<<endl;
}
//sum
of rows
for(int
i=0;i<r;i++)
{
int
sumrow=0;
for(int
j=0;j<c;j++)
{
sumrow=sumrow+X[i][j];
}
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Sum
of Row "<<i+1<<" is "<<sumrow<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
}
//sum
of columns
for(int
i=0;i<c;i++)
{
int sumcol=0;
for(int
j=0;j<r;j++)
{
sumcol=sumcol+X[j][i];
}
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Sum
of Column "<<i+1<<" is "<<sumcol<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
}
return
0;
}
Comments
Post a Comment