ET204 Programming Project 12 A matrix is a two-dimensional amangement of numbers
ID: 3821214 • Letter: E
Question
ET204 Programming Project 12 A matrix is a two-dimensional amangement of numbers Celements") hat have a common name. For example, the 3x3 "three by three matrix A would be written For each element ar the first subscript indicates the row la horizontal arrangement of elements) while the second subscript indicates the column vertical arrangement of elements) Contrary to he practice of numbering two-dimensional antays in C.matrix rows and columns are traditionally numbered beginning with In certain cases two matrices may be multiplied, there is another marria Irwe multiply square matrices having identical dimensions, the result is another matrix of the same dimension. To illustrate the process of matrix mul tiplication, consider two R3 matricesA and Band the product C-AB Each element of C is computed using the formula la words, this femula indieates that a given element ofCoccupying the frow and i column is found by first multiplying corresponding elements of the row of A and the columaofB and then adding the results. Your task in this project is write a CProgram that multiplies two 3x3 matrices. Spocifically, should do the following: Read two 33 matrices A and Bfrom an input file that has one row ofmannia elements per 2. Calla function that calculates the productABand retums theresul in athird matri C. Your function should have the prototype void matrix mult(double X 4204), double Y[4][4], double ZI4][4]); where X and Y are amays that contain the matrices lo be multiplied and Zis an amay that contains the product is highly recommended that you dimension these amays with four rows (0through 30 and four columns (0throu 3) using rows l through 3 and columnsExplanation / Answer
#include<stdio.h>
void readTwoMatrix(double a[4][4], double b[4][4], char *fileName) { //read two matrix
FILE *inFile = fopen(fileName, "r"); //open file in readMode
int i;
for (i = 1; i<4; i++)
fscanf(inFile,"%lf %lf %lf", &a[i][1], &a[i][2], &a[i][3]); //scan every row of matrix a
for (i = 1; i<4; i++)
fscanf(inFile,"%lf %lf %lf", &b[i][1], &b[i][2], &b[i][3]); //scan every row of matrix b
}
void matrix_mult(double X[4][4], double Y[4][4], double Z[4][4]) { //method to multiply
int i, j, k;
for (i = 1; i<4; i++)
for (j = 1; j<4; j++) {
Z[i][j] = 0; // reset item
for (k = 1; k<4; k++)
Z[i][j] += X[i][k] * Y[k][j]; //calculate multiplication. notice that A have fixed Row and B has fixed Column
}
}
int main() {
double a[4][4];
double b[4][4];
double c[4][4];
int i, j;
readTwoMatrix(a, b, "data.txt");
matrix_mult(a, b, c);
printf(" A= ");
for (i = 1; i<4; i++) {
for (j = 1; j<4; j++)
printf("%lf ",a[i][j]);
printf(" ");
}
printf(" B= ");
for (i = 1; i<4; i++) {
for (j = 1; j<4; j++)
printf("%lf ",b[i][j]);
printf(" ");
}
printf(" C= ");
for (i = 1; i<4; i++) {
for (j = 1; j<4; j++)
printf("%lf ",c[i][j]);
printf(" ");
}
}
I kept the code as simple as possible with no shortcuts and no complex calculations just to make your understanding easy. I have also commented the code to make your life easy. If incase you need more clarification, you may comment below. I shall be glad to help you with your doubts.
-----EDIT----
output:
$ ./a.exe
A=
1.000000 2.000000 3.000000
4.000000 5.000000 6.000000
7.000000 8.000000 9.000000
B=
5.000000 9.000000 1.000000
2.000000 7.000000 3.000000
8.000000 4.000000 6.000000
C=
33.000000 35.000000 25.000000
78.000000 95.000000 55.000000
123.000000 155.000000 85.000000
inputfile:
1 2 3
4 5 6
7 8 9
5 9 1
2 7 3
8 4 6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.