rendered paste body/* Rodney Biggs Summer 2005 */
/* COP 3223 prg4_matrices.c 07/24/05 */
/* Multiplying 3x3 Matrices */
#include <stdio.h>
int a[3][3] = {{1, 3, 5}, {2, -2, 1}, {1, 4, 7}};
int b[3][3] = {{6, 2, 1}, {8, -3, 5}, {2, 4, 1}};
int c[3][3] = {0};
void mat_mult(int a[3][3], int b[3][3]);
int main(void)
{
/* The following lines just reproduce the 'a' and 'b' matrices and present */
/* them in the 3x3 format. This wasn't requested, but it looked neat. :) */
printf("\nThis program multiplies the following two matrices together.\n");
/* 3x3 format displayed for matrix 'a' */
printf("\n |%2d %2d %2d|\n", a[0][0], a[0][1], a[0][2]);
printf("a = |%2d %2d %2d|\n", a[1][0], a[1][1], a[1][2]);
printf(" |%2d %2d %2d|\n", a[2][0], a[2][1], a[2][2]);
/* 3x3 format displayed for matrix 'b' */
printf("\n |%2d %2d %2d|\n", b[0][0], b[0][1], b[0][2]);
printf("b = |%2d %2d %2d|\n", b[1][0], b[1][1], b[1][2]);
printf(" |%2d %2d %2d|\n", b[2][0], b[2][1], b[2][2]);
/* Function call for 'c' - passing both 'a' and 'b' matrices */
mat_mult(a, b);
return 0;
}
/* Function to produce the product matrix 'c' */
void mat_mult(int a[3][3], int b[3][3])
{
printf("\n ------------------------\n");
printf("| and the answer is... |\n");
printf(" ------------------------\n");
/* The following lines produce the numbers to fill the 'c' 3x3 matrix */
c[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0] + a[0][2] * b[2][0];
c[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1] + a[0][2] * b[2][1];
c[0][2] = a[0][0] * b[0][2] + a[0][1] * b[1][2] + a[0][2] * b[2][2];
c[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0] + a[1][2] * b[2][0];
c[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1] + a[1][2] * b[2][1];
c[1][2] = a[1][0] * b[0][2] + a[1][1] * b[1][2] + a[1][2] * b[2][2];
c[2][0] = a[2][0] * b[0][0] + a[2][1] * b[1][0] + a[2][2] * b[2][0];
c[2][1] = a[2][0] * b[0][1] + a[2][1] * b[1][1] + a[2][2] * b[2][1];
c[2][2] = a[2][0] * b[0][2] + a[2][1] * b[1][2] + a[2][2] * b[2][2];
/* 3x3 format displayed for matrix 'c' */
printf("\n |%3d %3d %3d|\n", c[0][0], c[0][1], c[0][2]);
printf("c = |%3d %3d %3d|\n", c[1][0], c[1][1], c[1][2]);
printf(" |%3d %3d %3d|\n", c[2][0], c[2][1], c[2][2]);
return ;
}