IMPORTANT C++ ONLY multiplication (Iteratively and Recursively) Write two functi
ID: 3576190 • Letter: I
Question
IMPORTANT C++ ONLY
multiplication (Iteratively and Recursively)
Write two functions (an iterative and a recursive function) which will perform the multiplication of two matrices. Write a main program that reads from the user the sizes and elements of two matrices and then, if possible, prints out the new matrix, which is the multiplication of the two matrices. A sample program run is as follows:
DA Users TCPVDesktopldemo.exe nter rows and columns of first matrix 3 Enter ws and columns of second matrix 2 Enter first matrix 2 4 6 3 1 2 Enter second matrix 3 4 1 3 3 8 The new matrix is 18 20 34 27 33 30 9 10 17 Process exited with return value 0 Press any key to continueExplanation / Answer
Iterative apporach
#include<iostream>
using namespace std;
void main(int a[][], int n, int m, int b[][],int p,int q);
int main()
{ int p,q,n,m;
int a[50][50], b[50][50];
cout<<"Enter the number of elements(rows and colums) of first array"<<endl;
cin>>n>>m;
cout<<"Enter the number of elements(rows and colums) of second array"<<endl;
cin>>p>>q;
if(n!=p)
{cout<<"Martices cannot be multiplied"<<endl;
}
else
{
cout<<"Please elements of first array"<<endl;
for(int i= 0;i<n;i++)
{for(int j=0;i<m;j++)
cin>>a[i][j];
}
for(int i= 0;i<n;i++)
{for(int j=0;i<m;j++)
cin>>b[i][j];
}
product(a,n,m,b,p,q);
}
return 0;
}
void main(int a[][], int n, int m, int b[][],int p,int q)
{
int sum=0;
int product[n][m];
for (int i = 0; i<n ; i++)
{
for (int j = 0; j< q; j++)
{
for (int k = 0; k < p; k++)
{
sum = sum + a[i][k]*b[k][j];
}
product[i][j] = sum;
sum = 0;
}
}
cout<<"Product of matrices is"<<endl;
for(int i= 0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cout<<product[i][j]<<" ";
}
cout<<endl;
}
}