Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USE C programming Language and write a program to read many files as test cases

ID: 3757280 • Letter: U

Question

USE C programming Language and write a program to read many files as test cases for given prompt.

Fourth: Matrix Multiplication (10 Points) This program will test your ability to manage memory using malloc() and provide some experience dealing with 2D arrays in C. Your task is to create a program that multiplies two matrices and outputs the resulting matrix. The input matrices can be the same or different sizes. Input-Output format: Your program will take the file name as input. The first line in the file will provide the number of rows and columns in the matrix separated by a tab. The subsequent lines will provide the contents of the matrix. The numbers in the same row are tab separated and the rows are separated with new lines. This will be followed by the same format for the dimensions and content of the second matrix. For example, a sample input file "filel.txt": 2 3 1 2 3 4 5 6 3 2 3 4 5 6 The first number (2 refers to the number of rows and the second number (3) refers to the number of columns in the matrix. The dimensions of the of the first matrix will be 2x3 and the second matrix will be 3x2. The output on executing the program with the above input is shown below. The outputted numbers should be tab separated in the same row with a newline between rows. There should not be extra tabs or spaces at the end of the line or at the end of the file. $./fourth file1.txt 22 28 49 64 We will not give you improperly formatted files. You can assume all your input files will be in proper format as above with no matrix having 0 rows or columns For matrices that cannot be multiplied your program should output "bad-matrix"

Explanation / Answer

#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);

// Column of first matrix should be equal to column of second matrix and

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);

}

// Storing elements of first matrix.

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]);

}

// Storing elements of second matrix.

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]);

}

// Initializing all elements of result matrix to 0

for(i=0; i<r1; ++i)

for(j=0; j<c2; ++j)

{

result[i][j] = 0;

}

// Multiplying matrices a and b and

// storing result in result matrix

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];

}

// Displaying the result

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;

}

Output

Enter rows and column for first matrix: 3

2

Enter rows and column for second matrix: 3

2

Error! column of first matrix not equal to row of second.

Enter rows and column for first matrix: 2

3

Enter rows and column for second matrix: 3

2

Enter elements of matrix 1:

Enter elements a11: 3

Enter elements a12: -2

Enter elements a13: 5

Enter elements a21: 3

Enter elements a22: 0

Enter elements a23: 4

Enter elements of matrix 2:

Enter elements b11: 2

Enter elements b12: 3

Enter elements b21: -9

Enter elements b22: 0

Enter elements b31: 0

Enter elements b32: 4

Output Matrix:

24 29

6 25