sum+=A[i][j]*B[j][k];
C[i][j]=sum;
}end for
}end for
}end for
return result
}
(d)Algorithm implemented in C language
// maltidimension.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdlib.h>
//#pragma hdrstop
//#pragma argsused
int main(int argc, char* argv[])
{
int ncol,nrow,argnrow,argncol,i,j,k,p,sum;
int a[20][20],b[20][20],c[20][20];
start: printf("enter the value of ncol");
scanf("%d",&ncol);
printf("enter the value of nrow");
scanf("%d",&nrow);
printf("enter the value of argncol");
scanf("%d",&argncol);
printf("enter the value of argnrow");
scanf("%d",&argnrow);
if(ncol !=argnrow)
{
printf("matrices are incompatible");
goto start;
}
printf("enter matrix A\n");
for(i=0;i<nrow;++i)
{
for(j=0;j<ncol;++j)
{
scanf("%d",&p);
a[i][j]=p;
}
printf("\n");
}
printf("enter matrix B\n");
for(j=0;j<argnrow;++j)
{
for(k=0;k<argncol;++k)
{
scanf("%d",&p);
b[i][j]=p;
}
printf("\n");
}
printf("product");
for(i=0;i<nrow;++i)
{
for(j=0;j<ncol;++j)
{
sum=0;
for(k=0;argncol;++k)
sum+=a[i][j]*b[j][k];
c[i][k]=sum;
}
printf("%d",c[i][k]);
}
return 0;
}
Sample output 1
enter the value of ncol4
enter the value of nrow3
enter the value of argncol3
enter the value of argnrow4
enter matrix A
1 1 1 1
2 2 2 2
3 3 3 3
enter matrix B
4 4 4
1 2 3
1 1 1
3 2 1
product
9 9 9
18 18 18
27 27 27
Press any key to continue
Sampleoutput 2
enter the value of ncol4
enter the value of nrow3
enter the value of argncol3
enter the value of argnrow3
matrices are incompatibleenter the value of ncol4
enter the value of nrow3
enter the value of argncol3
enter the value of argnrow4
enter matrix A
1 1 1 1
2 2 2 2
3 3 3 3
enter matrix B
4 4 4
1 2 3
1 1 1
3 2 1
product
9 9 9
18 18 18
27 27 27
Press any key to continue
(f) In this case the worst case is when you multiply a20*20 matrix and a20*20 matrix since ma program is designed to accept atmost twenty items
refference: http://www.brpreiss.com/books/opus4/html/page104.html