A directory of Objective Type Questions covering all the Computer Science subjects. Here you can access and discuss Multiple choice questions and answers for various compitative exams and interviews.

C program to perform matrix multiplication


Problem Description


This C programs perform multiplication of two matrices. In case of matrix multiplication

- The number of columns of the 1st matrix must be equal the number of rows of the 2nd matrix.
- And the result will have the same number of rows as the 1st matrix, and the same number of columns as the 2nd matrix.

To compute the matrix multiplication, we need to do the dot product of rows and columns. For instance if (1, 2, 3) is first row of first matrix and (7, 9, 11) is first column of second matrix, then dot product is 

(1, 2, 3) • (7, 9, 11) = 1×7 + 2×9 + 3×11  = 58


C program to perform matrix multiplication - Source code
     
                
  #include<stdio.h>

void readMatrix(int arr[10][10], int rows, int columns);
void printMatrix(int arr[10][10], int rows, int columns);
void productMatrix(int array1[10][10], int array2[10][10],
int array3[10][10], int rowsFirst, int columnsFirst, int rowsSecond, int columnsSecond);


int main()
{
	int mtrx1[10][10], mtrx2[10][10], mtrxMult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;

	printf("Enter rows and column for first matrix: ");
	scanf("%d %d", &rowFirst, &columnFirst);

	printf("Enter rows and column for second matrix: ");
	scanf("%d %d", &rowSecond, &columnSecond);

	// If column of first matrix in not equal to row of second matrix, asking user to re-enter the size of matrix.
	while (columnFirst != rowSecond)
	{
		printf("Number of columns of first matrix not equal to row of second.\n");
		printf("Enter rows and column for first matrix: ");
		scanf("%d%d", &rowFirst, &columnFirst);
		printf("Enter rows and column for second matrix: ");
		scanf("%d%d", &rowSecond, &columnSecond);
	}

	 printf("Enter elements of first Matrix \n");
	 readMatrix(mtrx1, rowFirst, columnFirst);

	 printf("First Matrix:\n");
    printMatrix(mtrx1, rowFirst, columnFirst);

    printf("Enter elements of second Matrix \n");
    readMatrix(mtrx2, rowSecond, columnSecond);

    printf("Matrix B \n");
    printMatrix(mtrx2, rowSecond, columnSecond);

    productMatrix(mtrx1, mtrx2, mtrxMult,rowFirst, columnFirst,rowSecond, columnSecond);
    printf("The product matrix is \n");
    printMatrix(mtrxMult, rowFirst, columnSecond);
}



/* Function to store Matrices */
void readMatrix(int arr[10][10], int rows, int columns)
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < columns; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
}

/* Function to display Matrices */
void printMatrix(int arr[10][10], int rows, int columns)
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < columns; j++)
        {
            printf("%3d", arr[i][j]);
        }
        printf("\n");
    }
}



/*  Function to perform matrix multiplication */
void productMatrix(int array1[10][10], int array2[10][10],
int array3[10][10], int rowsFirst, int columnsFirst, int rowsSecond, int columnsSecond)
{
    int i, j, k;
    for (i = 0; i < rowsFirst; i++)
    {
        for (j = 0; j < columnsSecond; j++)
        {
            array3[i][j] = 0;
            for (k = 0; k < columnsFirst ; k++)
            {
                array3[i][j] = array3[i][j] + array1[i][k] *array2[k][j];
            }
        }
    }
}

     
      

Program Output


Enter rows and column for first matrix: 2
3
Enter rows and column for second matrix: 3
3
Enter elements of first Matrix
1
2
3
4
5
6
First Matrix:
  1  2  3
  4  5  6
Enter elements of second Matrix
1
2
3
4
5
6
7
8
9
Matrix B
  1  2  3
  4  5  6
  7  8  9
The product matrix is
 30 36 42
 66 81 96

Program Explanation


1. In this program three functions are declared and defined. One function (readMatrix) to take the matrices as input, second function (printMatrix) to display the matrices and another function (productMatrix) to perform matrix multiplication.

2. First, user is asked to input the number of columns of both matrices one by one. If number of columns of first matrix are not equal to rows of second matrix, user is asked to re-renter them again.

3. Next, the matrix elements are accepted by the user. Here note that same function (readMatrix) is called with different arguments for both the matrix.

4. Both the matrices are displayed using the function printMatrix.

5. Function productsMatrix is called with first first matrix, second matrix, multiplication matrix and rows and columns of both matrices as arguments.

6. In function productMatrix, dot product is performed. Here note that the elements of the multiplication matrix are initialized to zero before assigning them the product values.

7. At last the resultant matrix is displayed using the printMatrix function.