BACK TO 10+2 STEM LAB
10+2 MATHEMATICS & DRAWINGS•2016-10-23•4 MIN READ

Multiply Matrix and Multidimensional Array Operation

We have seen our previous post how to declare multidimensional arrays. Here we play with multidimensional arrays like Sum of Matrix, Multiply of Matrix etc. We have already programmed sum and aver...

Key Architecture Takeaways

  • ›
    Point 1: Core computer science and engineering computing concept: Multiply Matrix and Multidimensional Array Operation.
  • ›
    Point 2: Detailed trace of memory representation, algorithmic complexity, and control structures.
  • ›
    Point 3: Includes portable ISO C (C17/C23) code examples verified with modern GCC and Clang.
Notice

Compiler & Toolchain Modernization

While legacy educational environments used 16-bit DOSBox Turbo C++, all code presented here adheres to standard ANSI/ISO C standards. You can compile these programs cleanly on modern Linux, Windows (MSYS2/WSL), or macOS using:

bash
> gcc -Wall -Wextra -O2 program.c -o program
> ./program
> ```


---

[We have seen our previous post how to declare multidimensional arrays](https://www.technhit.in/using-arrays-c-language-example/). Here we play with multidimensional arrays  like Sum of Matrix, Multiply of Matrix etc. [We have already programmed sum and average of single dimension array in ](https://www.technhit.in/c-program-sum-average-array/)ours previous post. As we discuss earlier, more than 3 dimension arrays are difficult to imagine as well as implementation and execution. That's why we first use two dimension array as for example.

## C Program to store values into two-dimensional array:

c // C program to store marks of 3 year Semester and display it. #include <stdio.h>

int main() { int YEAR = 3; int SEM = 2; int marks[3][2]; int i,j; clrscr(); for (i = 0; i < YEAR; ++i) { for(j = 0; j < SEM; ++j) { printf("Year %d, Semester %d: ", i+1, j+1); scanf("%d", &marks[i][j]); } }

printf("\nDisplaying values: \n\n"); for ( i = 0; i < YEAR; ++i) { for( j = 0; j < SEM; ++j) { printf(" You have enter %d Year, %d Semester marks = %d\n", i+1, j+1, marks[i][j]); } } getch(); return 0; }

SHELL
Output:

[![Multiply Matrix](https://www.technhit.in/wp-content/uploads/2016/10/storing-value.png)](https://www.technhit.in/wp-content/uploads/2016/10/storing-value.png)

## C Program to Calculate sum of 2x2 Matrix:

c #include <stdio.h> int main() { float a[2][2], b[2][2], c[2][2]; int i, j;

// Storing Values into matrix a using nested for loop clrscr(); printf("Enter elements of 1st matrix\n"); for(i=0; i<2; ++i) for(j=0; j<2; ++j) { printf("Enter a%d%d: ", i+1, j+1); scanf("%f", &a[i][j]); }

// Storing Values into matrix b using nested for loop printf("Enter elements of 2nd matrix\n"); for(i=0; i<2; ++i) for(j=0; j<2; ++j) { printf("Enter b%d%d: ", i+1, j+1); scanf("%f", &b[i][j]); }

// adding corresponding elements of two arrays into third array c for(i=0; i<2; ++i) for(j=0; j<2; ++j) { c[i][j] = a[i][j] + b[i][j]; }

// Displaying the sum printf("\nSum Of Matrix:\n");

for(i=0; i<2; ++i) for(j=0; j<2; ++j) { printf("%.1f\t", c[i][j]); if(j==1) printf("\n"); } getch(); return 0; }

SHELL
Output:

[![sum](https://www.technhit.in/wp-content/uploads/2016/10/sum.png)](https://www.technhit.in/wp-content/uploads/2016/10/sum.png)

## C Program to Calculate Multiplay of 2x2 Matrix:

An *n* x *m* matrix **A** and *m* x *p* matrix **B** can be multiplied together giving a *n* x *p* matrix **C**. It is important that the first matrix (**A**) has the same number of columns as the second (**B**) has rows.

[To know more details click here](http://l4labs.soton.ac.uk/tutorials/maths/matrices.htm)

c #include <stdio.h>

int main() { int a[2][2], b[2][2], result[2][2], i, j, k;

// Storing elements of first matrix. printf("\nEnter elements of matrix 1:\n"); for(i=0; i<2; ++i) for(j=0; j<2; ++j) { printf("Enter elements a%d%d: ",i+1, j+1); scanf("%d", &a[i][j]); }

// Storing elements of second matrix. printf("\nEnter elements of matrix 2:\n"); for(i=0; i<2; ++i) for(j=0; j<2; ++j) { printf("Enter elements b%d%d: ",i+1, j+1); scanf("%d",&b[i][j]); }

// Initializing all elements of result matrix to 0 for(i=0; i<2; ++i) for(j=0; j<2; ++j) { result[i][j] = 0; }

// Multiplying matrices a and b and // storing result in result matrix for(i=0; i<2; ++i) for(j=0; j<2; ++j) for(k=0; k<2; ++k) { result[i][j]+=a[i][k]*b[k][j]; }

// Displaying the result printf("\nOutput Matrix:\n"); for(i=0; i<2; ++i) for(j=0; j<2; ++j) { printf("%d ", result[i][j]); if(j == 2-1) printf("\n\n"); } return 0; } ```

Output:

multiplymultiply

π
TechNHIT STEM Faculty
Department of Applied Mathematics & Physics
Multiply Matrix and Multidimensional Array Operation | TechNHIT 10+2 STEM Lab | TechNHIT