Matris multiplicatio ltera and Recurenston) which will perform the multiplicatio
ID: 3741371 • Letter: M
Question
Matris multiplicatio ltera and Recurenston) which will perform the multiplication 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: Enter rows and columns of first matrix:3 2 Enter rows and columns of second matrix:2 Enter first matrix: Enter second matrix: 3 4 1 3 3 8 The new matrix is: 18 20 34 27 33 30 9 10 1'?Explanation / Answer
#include<iostream>
using namespace std;
void recursiveMulti(int row1, int col1, int A[][MAX],
int row2, int col2, int B[][MAX],
int C[][MAX])
{
static int i = 0, j = 0, k = 0;
if (i >= row1)
return;
if (j < col2)
{
if (k < col1)
{
C[i][j] += A[i][k] * B[k][j];
k++;
recursiveMulti(row1, col1, A, row2, col2,
B, C);
}
k = 0;
j++;
recursiveMulti(row1, col1, A, row2, col2, B, C);
}
j = 0;
i++;
recursiveMulti(row1, col1, A, row2, col2, B, C);
}
void multiply(int m1, int m2, int A[][m2],
int n1, int n2, int B[][n2])
{
int x, i, j;
int res[m1][n2];
for (i = 0; i < m1; i++) {
for (j = 0; j < n2; j++) {
res[i][j] = 0;
for (x = 0; x < m2; x++) {
*(*(res + i) + j) += *(*(A + i) + x) *
*(*(B + x) + j);
}
}
}
for (i = 0; i < m1; i++) {
for (j = 0; j < n2; j++) {
cout << *(*(res + i) + j);
}
cout << endl;
}
}
int main ()
{
int r1, c1, r2, c2;
int i, j, k;
cout << "Enter rows and columns of first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns of second matrix: ";
cin >> r2 >> c2;
int A[r1][c1], B[r2][c2];
if (c1 != r2)
{
cout << "Matrices cannot be multiplied!";
}
else{
cout << "Enter first matrix: ";
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
cin >> A[i][j];
cout << "Enter second matrix: ";
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
cin >> B[i][j];
iterativeMulti(r1, c1, A, r2, c2, B);
}
return 0;
}
**Comment for any further queries.