Multiplying Matrixes – Solution

My solution for this month’s Exercise relies upon the solution from last month’s Exercise, specifically the way the matrixes are presented in the main() function and the use of the output() function. What I added is the multiply() function, which multiplies the two matrixes.

I confess that I had trouble working out my solution. I know that a nested loop is required to process values in both matrixes. But the solution requires another inner loop, which is the part that threw me.

To be completely honest, my original solution included brute force code that processed each matrix cell individually. Only after seeing each step written out as its own statement did I code the innermost nested loop required to perform the calculations. Here is my solution:

2024_10-Exercise.c

#include <stdio.h>

struct matrix {
    int *grid;
    int rows;
    int columns;
};

void multiply(struct matrix a,struct matrix b)
{
    int r,c,x,v;

    for( r=0; r<a.rows; r++ )
    {
        for( c=0; c<b.columns; c++ )
        {
            v = 0;
            for( x=0; x<a.columns; x++ )
            {
                v+= *(a.grid+x+r*a.columns) * *(b.grid+x*b.columns+c);
            }
            printf("%4d ",v);
        }
        putchar('\n');
    }
}

void output(char *title,struct matrix m)
{
    int x,y;

    puts(title);
    for( y=0; y<m.rows; y++ )
    {
        for( x=0; x<m.columns; x++ )
        {
            printf(" %3d ",*(m.grid+x+y*m.columns) );
        }
        putchar('\n');
    }
}

int main()
{
    int matrix_a[16] = {
        10, 20, 30,
        11, 21, 31,
        12, 22, 32,
        13, 23, 33
    };
    int matrix_b[6] = {
        1, 2,
        3, 4,
        5, 6
    };
    struct matrix a = { matrix_a, 4, 3 };
    struct matrix b = { matrix_b, 3, 2 };

    /* output each matrix */
    output("Matrix A",a);
    output("Matrix B",b);

    /* multiple the matrixes */
    puts("Matrix A * Matrix B");
    multiply(a,b);

    return 0;
}

The multiply() function contains a nested loop. It works through the rows of the first matrix (a.rows) and the columns of the second (b.columns).

For the inner loop, variable v is initialized to zero. Then the innermost loop processes the rows and columns set from the outer loops. The values from the rows in matrix a (r) are multiplied by the values from the columns in matrix b (c). The total is accumulated in variable v:

v+= *(a.grid+x+r*a.columns) * *(b.grid+x*b.columns+c);

Pointer notation makes the expressions clunky, but the result obtains values from the rows and columns in each matrix. Also, the ** thing is odd in the middle, which is why I added whitespace to separate the multiplication operator (*) from the pointer indirection operator (*). The order of operations maintains the sanity.

Here is output:

Matrix A
  10   20   30 
  11   21   31 
  12   22   32 
  13   23   33 
Matrix B
   1    2 
   3    4 
   5    6 
Matrix A * Matrix B
 220  280 
 229  292 
 238  304 
 247  316 

Of course, the true test of my solution is whether it works on matrixes other than the two sample ones provided. I re-wrote the code to try the results on several different dimensioned matrixes and it worked! (Yes, I was relieved.)

To confirm the results, you can use this online matrix calculating tool to verify the output.

I hope that your solution met with success.

Leave a Reply