7–P5 Write a program to multiply a two-dimensional matrix in the following way:
ID: 3674896 • Letter: 7
Question
7–P5 Write a program to multiply a two-dimensional matrix in the following way: multiply all elements in row i by (1) ii. That is, multiply row 1 by 1, row 2 by +2, row 3 by 3, and so on. Your program should be able to read matrices of size up to 10 × 10. You should query the user for number of rows, number of columns, and then read the matrix element values. These values should be within the range of 8-bit signed numbers (i.e., between 128 to +127). Internally, use words to store the number so that there will not be overflow problems with the multiplication. Make sure to do proper error checking, for example, asking for more than 10 rows or columns, entering an out-of-range value, and so on.
Explanation / Answer
#include<stdio.h>
int main()
{
int r1, c1, r2, c2, matrix1[10][10], matrix2[10][10], result[10][10];
int i, j, k;
printf("Enter the row and column of the first matrix: ");
scanf("%d%d",&r1,&c1);
printf("Enter the row and column of the second matrix: ");
scanf("%d%d",&r2,&c2);
if(c1 != r2){
printf("Matrix multiplication impossible");
}
printf("Enter the first matrix: ");
for(i = 0; i <r1; i++)
for(j = 0; j < c1; j++)
scanf("%d", &matrix1[i][j]);
printf("Enter the second matrix: ");
for(i = 0; i <r2; i++)
for(j = 0; j < c2; j++)
scanf("%d", &matrix2[i][j]);
for(i = 0; i < r1; i++ ){
for(j = 0; j < c2; j++){
result[i][j] = 0;
for(k = 0; k < c1; k++){
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
printf("The multiplication of the matrix is: ");
for(i = 0; i < r1; i++){
for(j = 0; j < c2; j++){
printf("%d", result[i][j]);
printf(" ");
}
printf(" ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.