In: Computer Science
PLEASE SOLVE THIS QUESTION WITHOUT CHANGING THE SOURCE CODE POSTED BELOW. (ONLY ADDING)
#include <iostream>
using namespace std;
const int arrSize = 3;
// QUESTION ii. COMPLETE A displayMatrix(...) FUNCTION HERE
// QUESTION iii. COMPLETE A transposedMatrix(...) FUNCTION HERE
// QUESTION iv. COMPLETE A calculateTotal(...) FUNCTION HERE
int main(){
// variable declaration
int mat[arrSize][arrSize], transMat[arrSize][arrSize] = {0}, total = 0;
// QUESTION i. Prompt user to enter 9 integer numbers and fill in into the matrix
cout << "\nThe original matrix is : \n";
displayMatrix(mat, arrSize); // function called to display matrix
// function called to transpose matrix
transposedMatrix(mat, transMat, arrSize);
cout << "\nThe transposed matrix is : \n";
displayMatrix(transMat, arrSize); // function called to display matrix
calculateTotal(mat, arrSize, total); // function called to calculate total
cout << "\nThe total of the matrix is " << total << endl;
return 0;
}
Full Code--
#include <iostream>
using namespace std;
const int arrSize = 3;
// QUESTION ii. COMPLETE A displayMatrix(...) FUNCTION HERE
void displayMatrix(int mat[][arrSize],int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
// QUESTION iii. COMPLETE A transposedMatrix(...) FUNCTION
HERE
void transposedMatrix(int mat[][arrSize],int transMat[][arrSize],
int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
transMat[j][i]=mat[i][j];
}
}
}
// QUESTION iv. COMPLETE A calculateTotal(...) FUNCTION HERE
void calculateTotal(int mat[][arrSize],int size,int
&total)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
total+=mat[i][j];
}
}
}
int main()
{
// variable declaration
int mat[arrSize][arrSize], transMat[arrSize][arrSize]
= {0}, total = 0;
// QUESTION i. Prompt user to enter 9 integer numbers
and fill in into the matrix
cout<<"Enter the values of the matrix\n";
for(int i=0;i<arrSize;i++)
{
for(int j=0;j<arrSize;j++)
{
cin>>mat[i][j];
}
}
cout << "\nThe original matrix is : \n";
displayMatrix(mat, arrSize); // function called to
display matrix
// function called to transpose matrix
transposedMatrix(mat, transMat, arrSize);
cout << "\nThe transposed matrix is : \n";
displayMatrix(transMat, arrSize); // function called
to display matrix
calculateTotal(mat, arrSize, total); // function
called to calculate total
cout << "\nThe total of the matrix is " <<
total << endl;
return 0;
}
Question wise screenshot--
Question i)
Question ii)
Question iii)
Question iv)
Output Screenshot--
Note--
Please upvote if you like the effort.