Implement a program that takes one user input (n). Implement a sequential progra
ID: 3880346 • Letter: I
Question
Implement a program that takes one user input (n). Implement a sequential program that multiples matrices from 1*1, 2*2, to n*n (n was given by the user). Make sure you fill out the matrices with random numbers from a random number generator. Ensure that you utilize a for loop in order to multiply the different sized matrices together (from 1*1 to n*n) and a for loop in order to fill the matrices with random numbers. Test the program with several different inputs for n, and make sure that n is large enough for you to notice a pattern and receive enough data for a graph. Finally, make sure you are recording the time it took to multiply each matrix size together, so that you can utilize the values in a graph for analysis purposes later on.
To be programmed using C++
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin>>N;
for(int n=1;n<=N;n++)
{
//generating matrix of size n*n
int a[n][n],b[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=rand();
b[i][j]=rand();
}
}
//multiplication of matrics of size n*n
int c[n][n];
cout<<"multiplication of matrices of size"<<n<<"*"<<n<<endl;
int time=0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
c[i][j] = 0;
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
time++;
}
}
}
cout<<"time for"<<n<<"*"<<n<<endl<<time;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.