##write a code in C language to multiply matrices of order 2×2? Hurry up !!! No idiotic answer plzz and don't copy from internet.
Answers
#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[i][j]; /* Sum of corresponding elements of two arrays. */
}
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) /* To display matrix sum in order. */
printf("\n");
}
for(i = 0 ; i < 2 ; i++) {
for(j = 0 ; j < 2 ; j++) {
sum = 0 ;
for(k = 0 ; k < 2 ; k++) {
sum = sum + a[i][k] * b[k][j];
printf("a: %d; b: %d\n", a[i][k], b[k][j]);
printf("%d", sum);
}
d[i][j]=sum;
}
}
printf("\nThe multiplication matrix is : \n\n") ;
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf("%d \t", d[i][j]) ;
}
printf("\n") ;
}
system("PAUSE");
return 0;
}
Thanks
#include<stdio.h>
int main(void)
{
int c, d, p, q, m, n, k, tot = 0;
int fst[10][10], sec[10][10], mul[10][10];
printf(" Please insert the number of rows and columns for first matrix \n ");
scanf("%d%d", &m, &n);
printf(" Insert your matrix elements : \n ");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &fst[c][d]);
printf(" Please insert the number of rows and columns for second matrix\n");
scanf(" %d %d", &p, &q);
if (n != p)
printf(" Your given matrices cannot be multiplied with each other. \n ");
else
{
printf(" Insert your elements for second matrix \n ");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &sec[c][d] );
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
tot = tot + fst[c][k] * sec[k][d];
}
mul[c][d] = tot;
tot = 0;
}
}
printf(" The result of matrix multiplication or product of the matrices is: \n ");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d \t", mul[c][d] );
printf(" \n ");
}
}
return 0;
}