To be done Write a function matrix_multiplication() to multipy two matrices and
ID: 3688896 • Letter: T
Question
To be done
Write a function matrix_multiplication() to multipy two matrices and put the result into a third matrix.
The function takes six arguments altogether, i.e. pointers to the matrices to be multiplied and the resulting matrix and three values for the number of rows, columns and the shared dimension. We want to assume that all values are of type integer. This said, the prototype of the function is:
matrix_multiplication(int* left_matrix, int* right_matrix, int* result_matrix, int rows, int cols, int shared);
Write a quick program that makes use of matrix_multiplication(). You are allowed to "hard-code" the values of the example matrices to be multiplied.
What to submit
Submit your program's .c file on Louie. Make sure you documents your code thoroughly.
Grading Guide
- 30 pts for proper comments, indentation and overall form.
- 70 pts functionality, split into 10 pts for the driver's code and 60 pts for the proper coding of the multiplication function itself. 60 points are only applied if you used pointer notation all along. You also need to follow the variables naming convention mentioned above!
Explanation / Answer
#include <math.h>
#include <stdio.h>
#define column 3
#define row 3
#define share 3
int matrix_multiplication(int *left_matrix, int *right_matrix, int *result_matrix, int rows, int cols, int shared) {
int i, j, k;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {//stays within first column and row through first iteration
for (k = 0; k < 3; k++)//ensures inner dimensions match with variable k i.e. ixk * kxj =ixj or A*B=C
*((result_matrix+i*3)+j) += (*((right_matrix+i*3)+k)) * (*((left_matrix+k*3)+j)); //C programming has ability to perform matrix multiplication
//Adds result of each operation to C matrix i.e. +=
printf("%d ", *((result_matrix+i*3)+j)); //matrix C is composed of rows(i) and columns(j)
}//new tab after each column iteration
printf(" "); //new line for each row iteration
}
return 0;
}
int A[][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
},
B[][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}, C[3][3]; //initialize "hard coded" three matrices
int main() {
matrix_multiplication((int *)A, (int *)B, (int *)C, row, column, share); //passes address of each matrix to function
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.