C   9
Matrix Multiplication demo
Guest on 11th July 2024 08:38:30 AM

1. #include <stdio.h>
2. #define N 3
3.
4. main()
5.  { int i, j, A[N][N], B[N][N], C[N][N], U[N], V[N];
6.    printf("Matrix Multiplication demo,  L.Allison, Comp Sci, Monash U, oz\n");
7.
8.    for(i=0; i < N; i++) /* put random #'s in V, A and B */
9.     { V[i] = 1+rand()%9;
10.
11.       for(j=0; j < N; j++)
12.        { B[i][j] = rand()%10;
13.          C[i][j] = rand()%10;
14.        }
15.     }
16.
17.
18.    mult(A, B, C, N, N, N);  /* A[][] = B[][] x C[][] */
19.
20.    for(i=0; i < N; i++)
21.     { for(j=0; j < N; j++) printf(" %3d", A[i][j]);
22.       printf( (i==1) ? " = " : "   " );
23.       for(j=0; j < N; j++) printf("%3d ", B[i][j]);
24.       printf( (i==1) ? " x " : "   " );
25.       for(j=0; j < N; j++) printf("%3d ", C[i][j]);
26.       printf("\n");
27.     }
28.    printf("\n");
29.
30.
31.    VecMatrix(U, V, C, N, N);  /* U[] = V[] * M[][] */
32.
33.    for(i=0; i < N; i++)
34.     { for(j=0; j < N; j++) if(i==1) printf(" %3d", U[j]); else printf("    ");
35.       printf( i==1 ? " = " : "   ");
36.       for(j=0; j < N; j++) if(i==1) printf(" %2d", V[j]); else printf("   ");
37.       printf( (i==1) ? " x " : "   " );
38.       for(j=0; j < N; j++) printf("%2d ", C[i][j]);
39.       printf("\n");
40.     }
41.  }/*main*/
42.
43. /* Test Driver for Matrix Operation */

Raw Paste

or to edit or fork this paste. It's free.