C Program to Multiply Two Matrices Using Multi-dimensional Arrays

C Program to Multiply Two Matrices Using Multi-dimensional Arrays

This is the example of c-programming. You can edit and compile code in your own way on clicking button. If you have better solution or you found any error on code, then give us suggestion.

Full Code:


Code Explanation :


You will learn to multiply two matrices in this example, and show them using user-defined functions.

This program asks user to enter the size of two matrices (rows and columns).

For multiply two matrices the number of first matrix columns should be equal to the number of second matrix rows.

The program below asks for the number of rows and columns of two matrices until the above condition is satisfied.

Then, two matrices are multiplied and the result is shown on the screen.

To accomplish this we generated three functions:

  • enterData() - To take inputs for Matrix Elements.
  • multiplyMatrices() - to multiply two matrices.
  • display() - resulting matrix should be shown after multiplication.

We can do same program without using function. this is not recommended method.

Multiply of matrices without function.

#include<stdio.h>
#include<stdlib.h>
int main() {
    int a[10][10], b[10][10], mul[10][10], r, c, i, j, k;
    system("cls");
    printf("enter the number of row=");
    scanf("%d", & r);
    printf("Enter the number of column=");
    scanf("%d", & c);
    printf("Enter the first matrix element=\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
        	printf("Element a[%d]b[%d] = ", i,j);
            scanf("%d", & a[i][j]);
        }
    }
    printf("Enter the second matrix element=n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
        	printf("Element b[%d]b[%d] = ", i,j);
            scanf("%d", & b[i][j]);
        }
    }

    printf("multiply of the matrix=\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            mul[i][j] = 0;
            for (k = 0; k < c; k++) {
                mul[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    //for printing result    
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            printf("%d\t", mul[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Algorithm

STEP 1 : Start

STEP 2 : Declare  array  a[10][10],  b[10][10],  mul[10][10]  with r, c,
        i, j and k

STEP 3 : Read r and c

STEP 4 : Repeat this step until i < r
        STEP 4.1 : Repeat this step until j < c
                STEP 4.1.1 : Read for a[i][j]
                STEP 4.1.2 : j++
        STEP 4.2 : i++

STEP 5 : Repeat this step until i < r
        STEP 5.1 : Repeat this step until j < c
                STEP 5.1.1 : Read for b[i][j]
                STEP 5.1.2 : j++
        STEP 5.2 : i++
        
STEP 6 : Repeat this step until i < r
        STEP 6.1 : Repeat this step until j < c
                STEP 6.1.1 : mul[i][j] = 0
                STEP 6.1.2 : Repeat this step until k < c
                        STEP 6.1.1.1 : mul[i][j] += a[i][k] * b[k][j]
                        STEP 6.1.1.2 : k++
                STEP 6.1.2 : j++
        STEP 6.2 : i++

STEP 7 : Repeat this step until i < r
        STEP 7.1 : Repeat this step until j < c
                STEP 7.1.1 : Print b[i][j] with tab
                STEP 7.1.2 : j++
        STEP 7.2 : Print "n" for line break
        STEP 7.3 : i++

STEP 8 : End
    

Flowchart

C program to multiply two matrices without function

If you got any difficulty on understanding this example, leave comment. we will explain you with more explanation.



Our example mainly covers following topics in any programing

  • Basic Programs
  • Number Programs
  • Array Programs
  • Matrix Programs
  • Pattern Programs
  • String Programs
  • Tree Programs
  • Singly Linked List Programs
  • Circular Linked List Programs
  • Doubly Linked List Programs

Here we share programs on various topics such as array strings, series area & volume of geometric figures, mathematical calculation algorithms for sorting & searching and much more.Our goal is to provide you with the perfect solution to all the programming problems you may have encountered either during interviews or in class assignments. If you don’t find what you are looking for then please drop a line in the comment section of example page or you can request from account page so that we can get it added to the below collection of programs. Happy Learning!!


If you are confident with the above programs and are able to successfully understand and run them without any problems then it is time for you to take a step further and learn comprehensive programming principles using examples and flow diagrams. Here is the link: Programming tutorial lists .


If you find this example helpful, don't forget to share it with your friends. And stay updated with us by subscribing adzetech.


If you find any code error or grammetical error then you can suggest for example improvement on clicking below button.

Leave comment

Comments (1)

// SEARCH

What are you searching for....?

// SUBSCRIBE

Subscribe us on youtube to get our videos updates firstly on youtube.

// SUBSCRIBE

To receive updates on the latest news, tutorials and special offers, subscribe to our email newsletter today!

// CATEGORIES

x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine