Question: C++ Problem Now that we have our tools, we can time matrix multiplication (and examine small portions of the answers). Our game will be to
C++ Problem
Now that we have our tools, we can time matrix multiplication (and examine small portions of the answers). Our game will be to generate a sparse matrix (between 0.1% and 10% of the size of the matrix, non-zero) and then "square" the matrix (multiply it by itself) using matrix multiplication. We'll show the lower right small 10x10 square sub-portion of the matrix, and then that same portion of the squared matrix.
Please follow the templete and finish the code. Make sure the user could enter the size.
// Assignment #3 Timing Matrix Multiplication (Part A) // CLIENT
#include
// --------------- main --------------- #define MIN_MAT_SIZE 1 #define MIN_PERCENT 1 #define MAX_PERCENT 10.0
// for Sparse Matrix of dynamically allocated sizes void matMultDynSp(float **matA, float **matB, float **matC, int size); void matShowDynSp(float **matA, int mat_size, int start, int show_size);
int main() { int r, c, mat_size; clock_t startTime, stopTime; double randFrac; int randRow, randCol, numElement; float smallPercent; // work on the Sparse Matrix of different sizes cin >> mat_size >> smallPercent; //check the input values and reset to MIN or MAX if out of range // code provided by student // // // non fixed size dynamic matrix float *matDynSp[mat_size], *matDynSpAns[mat_size];
// allocate rows and initialize to 0 for (r = 0; r < mat_size; r++) // code provided by student // //
// initialize random seed to a fixed start 0 // so it will generate the same random sequence srand (0); // generate small % (between .1 and 10.) non-default values (between 0 and 1.0) numElement = mat_size * mat_size * smallPercent / 100; for (r = 0; r < numElement; r++) { // use the following method to generate a random position first // then fill up this random positon with another random number: // randFrac = rand() / (double) RAND_MAX; // randRow = randFrac * mat_size; // ... // matDynSp[randRow][randCol] = randFrac; // code provided by student // // }
matShowDynSp(matDynSp, mat_size, mat_size - 10, 10);
startTime = clock(); // ------------------ start matMultDynSp(matDynSp, matDynSp, matDynSpAns, mat_size); stopTime = clock(); // ---------------------- stop
matShowDynSp(matDynSpAns, mat_size, mat_size - 10, 10);
cout << " Size = " << mat_size << " Dyn Array Mult Elapsed Time: " << (double)(stopTime - startTime) / (double)CLOCKS_PER_SEC << " seconds." << endl << endl;
// clean up for (r = 0; r < mat_size; r++) { // code provided by student // // } cout << endl; }
void matMultDynSp(float **matA, float **matB, float **matC, int size) { // code provided by student // // }
void matShowDynSp(float **matA, int mat_size, int start, int show_size) { int r, c; // check the display range and make sure only data within the matrix will be shown // code provided by student // //
cout.width(6); cout.precision(2); cout << setiosflags( ios::fixed ) << matA[r][c] << " "; cout << endl; }
Sample Output:
-1 -1
0.00
0.00
5 50
0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.20
0.00 0.78 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00
0.00 0.15 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
