The files \"matrix1.dat\" and \"matrix2.dat\" are created using a text editor an
ID: 3824124 • Letter: T
Question
The files "matrix1.dat" and "matrix2.dat" are created using a text editor and contain two rectangular matrices. For example, In both cases the first row contains the size of the matrix (the number of rows and the number of columns). The remaining rows contain the values of elements. (a) Develop programs row and col that read a matrix from a file and display a specified row or column. For example: > (row "matrix1.dat" 2) 4 5 6 > (col "matrix1.dat" 2) 2 5 Matrices should be stored in memory as vectors whose components are vectors. (b) Develop a program for matrix multiplication mmul that multiplies two matrices stored in specified input files, and creates and displays an output file containing the product. For example (mmul "matrix1.dat" "matrix2.dat" "matrix3.dat") 6 12 18 15 30 45 In this example the contents of the new file "matrix3.dat" should be 2 3 6 12 18 15 30 45Explanation / Answer
2.
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of second. ");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
printf(" Enter elements of matrix 1: ");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
printf(" Enter elements of matrix 2: ");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
result[i][j] = 0;
}
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
result[i][j]+=a[i][k]*b[k][j];
}
printf(" Output Matrix: ");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ", result[i][j]);
if(j == c2-1)
printf(" ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.