Question
This code works for a 3x3 matrix. Can you make it work for a 7000x7000 matrix? Thanks ********THE CODE******* #include //Standard I/O methods def0intions #include
This code works for a 3x3 matrix. Can you make it work for a 7000x7000 matrix? Thanks
********THE CODE*******
#include
using namespace std; //Declare scope
int main ()//Main declaration { struct timespec startProg, startMult, endMult, endProg; //Declare variables to hold timestamps clock_gettime(CLOCK_MONOTONIC, &startProg); //Get initial timestamp double cpu_time_usedMult, cpu_time_usedProg; // varialbles to hold time calculations
srand((double)time(0)); ofstream myfile; myfile.open ("output2.txt");
double matrix1[3][3]; double matrix2[3][3]; double results[3][3] = {0}; double N, D;
for( int i = 0; i < 3; ++i) {for( int j = 0; j < 3; ++j) {N = ((((rand()%2))* (-1)^rand())%2000); D=(rand()%1000+1); D= D/1000; matrix1[i][j] = N +D;} } for( int i = 0; i < 3; ++i) {for( int j = 0; j < 3; ++j) {N = ((((rand()%2))* (-1)^rand())%2000); D=(rand()%1000+1); D= D/1000; matrix2[i][j] = N + D;} }
myfile << fixed << "Matrix x:" << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if(matrix1[i][j] < 0 ) myfile << setprecision(3) << setw(10)<< matrix1[i][j] << " "; else myfile << setprecision(3) << setw(10) << matrix1[i][j] << " "; } myfile << endl; } myfile << endl << "Matrix y:" << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if(matrix2[i][j] < 0 ) myfile << setw(10) << matrix2[i][j] << " "; else myfile << setw(10) << matrix2[i][j] << " "; } myfile << endl; }
clock_gettime(CLOCK_MONOTONIC, &startMult); //Get starting timestamp for matrix multiplication for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { for (int u = 0; u < 3; u++) results[i][j] += matrix1[i][u] * matrix2[u][j]; }
clock_gettime(CLOCK_MONOTONIC, &endMult); //Get ending timestamp for matrix multiplication
myfile << endl << "Output Matrix: " << endl;
for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { myfile << setprecision(4) << setw(15) << results[i][j] << " "; } myfile << setprecision(6) << setw(5) << endl; } myfile << setprecision(9) << endl;
cpu_time_usedMult = endMult.tv_nsec - startMult.tv_nsec; //Calulate time to multiply clock_gettime(CLOCK_MONOTONIC, &endProg); //Get ending timestamp for program execution cpu_time_usedProg = endProg.tv_nsec - startProg.tv_nsec ;//Calculate time to execute
//Output calculated results to file myfile << "System execution Time for Matrix Multiplication: " << fixed << cpu_time_usedMult*1e-9 << " Seconds" << endl; myfile << "Sytem execution Time for Entire Program: " << cpu_time_usedProg*1e-9 << " Seconds"; myfile.close(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started