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

Fill an 8x8 matrix with random numbers between 1 and 100. You must then search y

ID: 3546257 • Letter: F

Question

Fill an 8x8 matrix with random numbers between 1 and 100. You must then search your matrix for the minimum and maximum values.

If a minimum or maximum value occurs more than one time, your program should keep track of the number of occurences.

Once this is accomplished you must sort your matrix into descending order by rows.

For example:

77 45

23 3

Finally, your program should compute the average of all the values in the matrix.


Your program must print out the following:

a. the matrix initially filled with random numbers.

b. the minimum value in the matrix and number of occurrences

c. the maximum value in the matrix and number of occurrences

d. the matrix sorted in descending order

e. the average value

Explanation / Answer

#include <iostream>
#include <stdlib.h>
#include <time.h> //For generating random numbers
#include <algorithm> //For sorting using the inbuilt sorting function
#include <vector> //Used for sorting
using namespace std;
bool myfunction (int i,int j) { return (i>j); } //To make the sort in descending order
int main()
{
int matrix[8][8];
srand (time(NULL)); //Used for generating random numbers
for(int i=0;i<8;i++) //Filling the random numbers
{
for(int j=0;j<8;j++)
{
matrix[i][j] = rand() % 100+1;
}
}
cout << "after filling with random numbers"<< endl;

//Necessary variables
int sum=0;
int min=101;
int minCount=0;
int max=0;
int maxCount=0;

//Printing the numbers, calculating the sum, Finding the minimum, maximum and their occurences

for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{

cout << matrix[i][j] << " ";
sum=sum+matrix[i][j];
if(matrix[i][j]<min)
{
min = matrix[i][j];
minCount=1;
}
else if(matrix[i][j] == min)
{
minCount++;
}
if(matrix[i][j]>max)
{
max = matrix[i][j];
maxCount=1;
}
else if(matrix[i][j] == max)
{
maxCount++;
}

}

cout << endl;
}

//Outputting all the calculated values
cout << endl;
cout << "Number" << min << " is the minimum occured " << minCount << " times "<< endl;
cout << endl;
cout << "Number" << max << " is the maximum occured " << maxCount << " times "<< endl;
cout << endl;
cout << "Average is " << (float)sum/64 << endl;
cout << endl;
cout << "Matrix in Descending order" << endl;
cout << endl;

//Sorting the created matrix in descending order using the inbuilt sort function in algorithm library
for(int i=0;i<8;i++)
{
std::vector<int> myvector (matrix[i], matrix[i]+8);
std::sort (myvector.begin(), myvector.begin()+8, myfunction);
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
{
std::cout << ' ' << *it;
}
cout << endl;
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote