In: Computer Science
Create a C++ project in visual studio.
You can use the C++ project that I uploaded to complete this project.
1. Write a function that will accept two integer matrices A and B by reference parameters, and two integers i and j as a value parameter. The function will return an integer m, which is the (i,j)-th coefficient of matrix denoted by A*B (multiplication of A and B). For example, if M = A*B, the function will return m, which is equal to M[i][j].
Explain the time complexity of this function inside of the code as a comment.
2. Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j].
Explain the time complexity of this function inside of the function code as a comment.
3. Write a function that will accept one integer matrix E and one column vector F by reference parameters, and one integer I as a value parameter. The function will return an integer v, which is the i-th coefficient of row vector denoted by E*F (multiplication of E and F). For example, if V = E*F, the function will return v, which is equal to V[i].
Explain the time complexity of this function inside of the function code as a comment.
//Question1:-
//rename arr1->A and arr2->B
//time complexity for the loop is n^3 as wee use 3 loops in it
#include <iostream>
int print(int arr1[3][3],int arr2[3][3],int i,int j)
{
int matrix[i][i];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int sum=0;
for(int k=0;k<3;k++){
sum=sum+(arr1[i][k]*arr2[k][j]);
}
matrix[i][j]=sum;
}
}
for (int x = 0; x< 3; x++){
for (int y = 0; y < 3; y++){
std::cout<<matrix[x][y]<<"\t";
}
std::cout<<"\n";
}
return matrix[i][j];
}
int main()
{
int i=1,j=2;
int arr1[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int arr2[3][3] = {{3,4,5,},{7,5,4},{5,8,2}};
int m=print(arr1,arr2,i,j);
if(m!=0)
std::cout<<m<<"\t"<<"Element found";
else
std::cout<<"\t"<<"Element not found";
return 0;
}
//Question2:-
//time complexity for the loop is n^2 as wee use 2 loops in it
#include <iostream>
void print(int C[3][3],int D[3][3])
{
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
D[i][j]=C[j][i];
}
}
}
int main()
{
int C[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int D[3][3];
std::cout<<"Before Transpose"<<"\n";
for (int x = 0; x< 3; x++){
for (int y = 0; y < 3; y++){
std::cout<<C[x][y]<<"\t";
}
std::cout<<"\n";
}
print(C,D);
std::cout<<"After Transpose"<<"\n";
for (int x = 0; x< 3; x++){
for (int y = 0; y < 3; y++){
std::cout<<D[x][y]<<"\t";
}
std::cout<<"\n";
}
}
I hope you like the solution if you do so then press that thumbs up button to support.