Write two functions an iterative and a recursive function wh

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:

Solution

#include<stdio.h>

void multiply(int r1,int c1,int a[10][10], int r2,int c2, int b[10][10], int c[10][10])
{
   int i=0,j=0,k=0;
   if(i>=r1)
       return;
   else if(i<r1)
   {
       if(j<c2)
       {
           if(k<c1)
           {
               c[i][j]+=a[i][k]*b[k][j];
               k++;
               multiply(r1,c1,a,r2,c2,b,c);
           }
           k=0;
           j++;
           multiply(r1,c1,a,r2,c2,b,c);
       }
       j=0;
       i++;
       multiply(r1,c1,a,r2,c2,b,c);
   }
}


void multiplied(int r1,int c1,int a[10][10], int r2,int c2, int b[10][10], int c[10][10])
{
   int i=0,j=0,k=0,sum;
   for(i=0;i<r1;i++)
       for(j=0;j<c2;j++)
       {
           sum=0;
           for(k=0;k<c1;k++)
               sum=sum+a[i][k]*b[k][j];
           c[i][j]=sum;
       }
}


void main()
{
   int r1,c1,a[10][10],r2,c2,b[10][10],i,j,c[10][10];
   printf(\"Enter the size of matrix A\ \");
   scanf(\"%d%d\",&r1,&c1);  
   printf(\"Enter the size of matrix B\ \");
   scanf(\"%d%d\",&r2,&c2);
   if(c2!=r2)
       printf(\"Matrix multiplication not possible\");
   else
   {
       printf(\"\ Enter the elements of matrix A:\ \");
       for(i=0;i<r1;i++)
           for(j=0;j<c1;j++)
               scanf(\"%d\",&a[i][j]);
       printf(\"\ Enter the elements of matrix B:\ \");
       for(i=0;i<r2;i++)
           for(j=0;j<c2;j++)
               scanf(\"%d\",&b[i][j]);
   }
   printf(\"\ Multiplication using recursive method:\ \");
   multiply(r1,c1,a,r2,c2,b,c);
   for(i=0;i<r1;i++)
   {
           for(j=0;j<c2;j++)
               printf(\"%d \",c[i][j]);
           printf(\"\ \");
   }
   printf(\"\ \ Multiplication using iterative method:\ \");
   multiplied(r1,c1,a,r2,c2,b,c);
   for(i=0;i<r1;i++)
   {
           for(j=0;j<c2;j++)
               printf(\"%d \",c[i][j]);
           printf(\"\ \");
   }
}

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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site