Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write c++ code: generateMat() : that generates a matrix with random values. The

ID: 672996 • Letter: W

Question

write c++ code:

generateMat(): that generates a matrix with random values. The size and the smallest and largest possible values of the matrix are passed to the function

matrixStat(): that calculates the mean and standard deviation of the matrix elements.

display():an overloaded function that displays either the elements of the matrix in row by row manner or column by column manner depending on an a parameter passed to it or it displays the previously calculated statistics or the previously calculated histogram of the matrix.

Choose and implement these functions using the most appropriate headers. See Figure 2

Note

Dimensions (rows and columns) of the generated matrix should be between 2 and 8

Elements of the generated matrix should be random integers between 0 and 15.

CWindowslsystem321cmd.exe Please typei a 2D natrix G to generate a 2D natrix S to calculate statistics of a previously generated matrix D to display the values of a previously generated matrix H to display a histogram of a previously generated matrix Q to quit Your choice: G Please enter two integes (between 2 and 8 representing the dimentions Crows & ols> of the matrix: 4 4 integers (between 2 and ” represent ing the dinention.

Explanation / Answer

#include <iostream>
#include <math.h>
using namespace std;

void generateMat(int A[10][10], int rows, int cols)
{
srandom(time(NULL));
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
A[i][j] = random() % 16;
cout<<"A matrix has been generated"<<endl;
}

void matrixStat(int A[10][10], int rows, int cols)
{
int sum = 0;
float mean;
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
sum += A[i][j];
mean = sum / (rows + cols);
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
sum += pow(A[i][j] - mean, 2);
float var = sum / (rows + cols);
float std = sqrt(var);
cout<<"Mean is: "<<mean<<endl;
cout<<"Std is: "<<std<<endl;
}
void display(int A[10][10], int rows, int cols, int dch)
{
if(dch == 1)
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
cout<<A[i][j]<<" ";
cout<<endl;
}
else
for(int i = 0; i < cols; i++)
{
for(int j = 0; j < rows; j++)
cout<<A[j][i]<<" ";
cout<<endl;
}
}
void histogram(int A[10][10], int rows, int cols, char dc)
{
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
{
for(int k = 0; k < A[i][j]; k++)
cout<<dc;
cout<<endl;
}
}

int main()
{
char ch,dc;
int rows, cols, dch, A[10][10];
while(1)
{
cout<<"Please type:"<<endl;
cout<<"G to generate a 2D matrix"<<endl;
cout<<"S to calculate statistics of a previously generated matrix"<<endl;
cout<<"D to display the values of a previously generated matrix"<<endl;
cout<<"H to display a histogram of a previously generated matrix"<<endl;
cout<<"Q to quit"<<endl<<endl;
cout<<"Your choice: ";
cin >>ch;
switch(ch)
{
case 'G':
           cout<<"Please enter two integers (between 2 and 8) representing the dimensions(rows & cols) of a matrix: ";
           cin>>rows>>cols;
           generateMat(A,rows,cols);
           cout<<"Press any key to continue . . ."<<endl;
           break;
case 'S':
           matrixStat(A,rows,cols);
           cout<<"Press any key to continue . . ."<<endl;
           break;
case 'D':
           cout<<"How do you want to display the matrix"<<endl;
           cout<<"1 --> in a 2D form (original matrix)"<<endl;
           cout<<"2 --> in a 2D form (transpose of the matrix)"<<endl;
           cout<<"Your choice: ";
           cin>>dch;
           display(A,rows,cols,dch);
           cout<<"Press any key to continue . . ."<<endl;
           break;
case 'H':
           cout<<"Please enter the display character: ";
           cin>>dc;
           cout<<"The histogram"<<endl;
           histogram(A,rows,cols,dc);
           cout<<"Press any key to continue . . ."<<endl;
           break;
case 'Q':
           exit(1);
default :
           cout<<"Invalid Choice. Please go with the menu."<<endl;
}
}
}