Math, asked by kandalauday4377, 1 year ago

2x2 matrix multiplication in c

Answers

Answered by sabiq112
0
I'm trying to write a program to calculate the sum and product of two matrices, but I can't get it the product to work. The sum is fine. I am using Visual Studio.

Here's my program:

#include <stdio.h> #include <stdlib.h> int main() { float a[2][2], b[2][2], c[2][2], d[2][2], sum; int i,j,k; for(i = 0; i < 2; i++) { for(j = 0; j < 2; j++) { d[i][j] = 0; } } printf("Enter the elements of 1st matrix\n"); /* * Reading two dimensional Array with the help of two for loop. If there * was an array of 'n' dimension, 'n' numbers of loops are needed for * inserting data to array. */ 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]); } printf("\nEnter the 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]); } printf("\nMatrix 1:\n"); for (i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) { printf("%.1f\t", a[i][j]); if (j == 1) /* To display matrix sum in order. */ printf("\n"); } printf("\nMatrix 2:\n"); for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) { printf("%.1f\t", b[i][j]); if (j == 1) /* To display matrix sum in order. */ printf("\n"); } for (i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) { /* Writing the elements of multidimensional array using loop. */ c[i][j]=a[i][j]+b


Similar questions