Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matrix multiplication C = A*B can be written using two subscripts as follows: fo

ID: 3555494 • Letter: M

Question

Matrix multiplication C = A*B can be written using two subscripts as follows:

for(i=0; i<n; i++)
for(j=0; j<n; j++)
for(c[i][j]=k=0; k<n; k++)
c[i][j] += a[i][k]*b[k][j];
Write two matrix multiplication programs: one that uses static matrix with two
subscripts, and an equivalent program that uses dynamic matrices and pointer notation:

*(c+i*n+j)+= *(a+i*n+k) * *(b+k*n+j);

In both cases write functions that show matrices and illustrate the work of your
program using the multiplication of two 4*4 matrices.

Explanation / Answer

1.

#include<cstdio>
#include<cstdlib>
#include<iostream>

using namespace std;

int a[10][10],b[10][10],c[10][10],n,m,p,q;

void printarray() {
for(int i=0;i<n;i++)
   {
       for(int j=0;j<q;j++)
       {
           printf("%d   ",c[i][j]);
       }
       printf(" ");
   }
}
int main(void)
{
   printf("Enter size of 1st 2d array : ");
   scanf("%d %d",&n,&m);
   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           printf("Enter element no. %d %d :",i,j);
           scanf("%d",&a[i][j]);

       }
   }
   printf("Enter size of 2nd 2d array : ");
   scanf("%d %d",&p,&q);
   for(int i=0;i<p;i++)
   {
       for(int j=0;j<q;j++)
       {
           printf("Enter element no. %d %d :",i,j);
           scanf("%d",&b[i][j]);
       }
   }
   if(m!=p)
   {
       printf("Multiplication cannot be done ");
       exit (0);
   }


   cout<<"Using array"<<endl;
   int sum=0;
   for(int i=0;i<n;i++){
for(int j=0;j<q;j++){
for(int k=0;k<m;k++){
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
sum=0;
}
}
printarray();

   return 0;
}

2.

#include<cstdio>
#include<cstdlib>
#include<iostream>

using namespace std;

int main(void)
{
   int a[10][10],b[10][10],c[10][10],n=0,m=0,i=0,j=0,p=0,q=0,k=0;
   int *pt,*pt1,*pt2;
   printf("Enter size of 1st 2d array : ");
   scanf("%d %d",&n,&m);
   for(i=0;i<n;i++)
   {
       for(j=0;j<m;j++)
       {
           printf("Enter element no. %d %d :",i,j);
           scanf("%d",&a[i][j]);

       }
   }
   printf("Enter size of 2nd 2d array : ");
   scanf("%d %d",&p,&q);
   for(i=0;i<p;i++)
   {
       for(j=0;j<q;j++)
       {
           printf("Enter element no. %d %d :",i,j);
           scanf("%d",&b[i][j]);
       }
   }
   if(m!=p)
   {
       printf("Multiplication cannot be done ");
       exit (0);
   }
   pt=&a[0][0];
   pt1=&b[0][0];
   pt2=&c[0][0];
   for(i=0;i<n;i++)
   {
       for(k=0;k<q;k++)
       {
           *(pt2+(i*10+k))=0;
           for(j=0;j<m;j++)
           {
               *(pt2+(i*10+k))+=*(pt+(i*10+j))**(pt1+(j*10+k));
           }
       }
   }
   for(i=0;i<n;i++)
   {
       for(j=0;j<q;j++)
       {
           printf("%d   ",c[i][j]);
       }
       printf(" ");
   }
   return 0;
}

for 3rd part: http://en.wikipedia.org/wiki/Matrix_multiplication