Child pages
  • Question 10 - Matrix multiplication program
Skip to end of metadata
Go to start of metadata
//Example in C++, you can use netbeans and run it.
//This example multiplies two matrices A(M,N)*B(M,N)
//First input is the numbers of rows M and  columns N
// after that you must write the components of
//each matrix

#include <iostream>
#define MAXROWS 10
#define MAXCOLS 10

void main()
{
int A[MAXROWS][MAXCOLS], B[MAXROWS][MAXCOLS], C[MAXROWS][MAXCOLS];
int M, N;

/*Function declarations*/

void readMatrix(int arr[][MAXCOLS], int M, int N);
void printMatrix(int arr[][MAXCOLS], int M, int N);
void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],
int M, int N);

//clrscr();

printf("Enter the value of M and N\n");
scanf("%d %d",&M, &N);
printf ("Enter matrix A \n");
readMatrix(A,M,N);
printf("Matrix A\n");
printMatrix(A,M,N);

printf ("Enter matrix B \n");
readMatrix(B,M,N);
printf("Matrix B \n");
printMatrix(B,M,N);

productMatrix(A,B,C, M,N);

printf ("The product matrix is \n");
printMatrix(C,M,N);
}

/*Input matrix A*/
void readMatrix(int arr[][MAXCOLS], int M, int N)
{
int i, j;
for(i=0; i< M ; i++)
{
for ( j=0; j < N; j++)
{
scanf("%d",&arr\[i\]\[j\]);
}
}
}
void printMatrix(int arr[][MAXCOLS], int M, int N)
{
int i, j;
for(i=0; i< M ; i++)
{
for ( j=0; j < N; j++)
{
printf("%3d",arr[i][j]);
}
printf("\n");
}
}

/\* Multiplication of matrices \*/
void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],
int M, int N)
{
int i, j, k;
for(i=0; i< M ; i++)
{
for ( j=0; j < N; j++)
{
C[i][j] = 0 ;
for (k=0; k < N; k++)
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
}

Second version:

//Example in C++, you can use netbeans and run it.
//This example multiplies two matrices A(M,N)*B(N,P)
//First input is the numbers M, N and P
//after that you must write the components of
//each matrix

#include <iostream>
#define MAXROWS 10
#define MAXCOLS 10

void main()
{
int A[MAXROWS][MAXCOLS], B[MAXROWS][MAXCOLS], C[MAXROWS][MAXCOLS];
int M, N, P;

/*Function declarations*/

void readMatrix(int arr[][MAXCOLS], int ROW, int COL);
void printMatrix(int arr[][MAXCOLS], int ROW, int COL);
void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],
int M, int N, int P);

//clrscr();

printf("Enter the value of M, N and P\n");
scanf("%d %d %d",&M, &N, &P);
printf ("Enter matrix A \n");
readMatrix(A,M,N);
printf("Matrix A\n");
printMatrix(A,M,N);

printf ("Enter matrix B \n");
readMatrix(B,N,P);
printf("Matrix B \n");
printMatrix(B,N,P);

productMatrix(A,B,C, M,N,P);

printf ("The product matrix is \n");
printMatrix(C,M,P);
}

/*Input matrix */
void readMatrix(int arr[][MAXCOLS], int ROW, int COL)
{
  int i, j;
  for(i=0; i< ROW ; i++)
  {
    for ( j=0; j < COL; j++)
    {
      scanf("%d",&arr[i][j]);
    }
  }
}
void printMatrix(int arr[][MAXCOLS], int ROW, int COL)
{
  int i, j;
  for(i=0; i< ROW ; i++)
  {
    for ( j=0; j < COL; j++)
  {
  printf("%3d",arr[i][j]);
  }
  printf("\n");
  }
}

/* Multiplication of matrices */
void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],
int M, int N, int P)
{
  int i, j, k;
  for(i=0; i< M ; i++)
  {
    for ( j=0; j < P; j++)
    {
      C[i][j] = 0 ;
      for (k=0; k < N; k++)
      {
       C[i][j] = C[i][j] + A[i][k] * B[k][j];
      }
    }
  }
}

Third Version: uses row major ordered vectors to represent A, B, and C.

#define IDX(row,col,numcols) row*numcols+col

/* Perform matrix multiply. Matrix is stored in vector in row major order 
   A(rA,cA), B(rB,cB) : Note: cA == rB  
   produces C(rA,cB)   */
int *mat_mult_serial(int *A, int rA, int cA, int *B, int rB, int cB, int *C)
{
	int ii = 0;
	int jj = 0;
	int kk = 0;
	
	/* Perform matrix multiply. Matrix is stored in vector in row major order */
	for(ii = 0; ii < rA; ii++)
	{
		for(jj = 0; jj < cB; jj++)
		{
			for(kk = 0; kk < cA; kk++)
			{
				C[ IDX(ii,jj,cB) ] += A[ IDX(ii,kk,cA) ] * B[ IDX(kk,jj,cB) ];
			}
		}
	}						
	return(C);							
}
  • No labels