Building a New, Sum Matrix

Yet another fun way to mess with a matrix is to add its values to those in another matrix, creating a wonderful third matrix of the sums. Not everyone is going to agree that this notion is “fun.”

Well, I find it fun. In fact, it was relatively easy to code.

As with last week’s Lesson and this entire matrix series (minus Keanu Reeves), the first two matrixes are created as two-dimensional arrays and assigned random values. Defined constant SIZE sets the grids row and column counts.

Once the first two arrays are created, the third array is created by adding the values from the corresponding cells of the original arrays. This step was way too easy, so I decided to complicate things by outputting the three arrays in a single row. Here is the code I cobbled together:

2023_03_11-Lesson.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 3

int main()
{
    int x,y;
    int a[SIZE][SIZE],b[SIZE][SIZE],c[SIZE][SIZE];

    /* seed the randomizer */
    srand( (unsigned)time(NULL) );

    /* populate the grids */
    for( x=0; x<SIZE; x++ )
        for( y=0; y<SIZE; y++ )
        {
            a[x][y] = rand()%10 + 1;
            b[x][y] = rand()%10 + 1;
        }

    /* generate grid c */
    for( x=0; x<SIZE; x++ )
        for( y=0; y<SIZE; y++ )
            c[x][y] = a[x][y] + b[x][y];

    /* output the grids - side by side */
    for( x=0; x<SIZE; x++ )
    {
        /* grid a */
        for( y=0; y<SIZE; y++ )
            printf(" %2d ",a[x][y]);
        putchar('\t');

        /* grid b */
        for( y=0; y<SIZE; y++ )
            printf(" %2d ",b[x][y]);
        putchar('\t');

        /* grid c */
        for( y=0; y<SIZE; y++ )
            printf(" %2d ",c[x][y]);
        putchar('\n');
    }

    return(0);
}

Line 23 shows the nested loops that create grid c. The abhorrently creative expression is:

c[x][y] = a[x][y] + b[x][y];

That’s it. The reason it’s so uninspiring is why I decided to raise the bar in the output. Rather than have each matrix displayed in a column, which is easier to code, I used a set of nested for loops to output the results in a row.

Line 29 contains the outer loop, which processes each row in the three grids. Then the inner loops work on the columns, using the x value from the outer loop to set the row. Tabs separate the matrixes. Here is sample output:

  5   8   3       8  10   9      13  18  12 
  5  10   4       6   2   9      11  12  13 
  6   2   1       1   4   9       7   6  10 

To perform other math between the two original grids, just substitute the + operator at Line 26 with another operator. Oh, and I suppose you could fancy up the output by adding + and = characters between the grids. I leave such a challenge to you, though I recognize that your motivation to do so is rather low.

As with the examples in weeks past, adjusting the SIZE constant changes the grid size and the output. This convenience demonstrates how nifty such constants can be. Here is the output with the grid size set to five (which fits in a standard terminal window):

  4   2   9   1   7       5   8   7   7   4       9  10  16   8  11 
  7   7   6   4   4       9   4   5   5   4      16  11  11   9   8 
 10   9   6   4   2       6  10   4   6   6      16  19  10  10   8 
  6  10   4   4   7       7   8   8   6   2      13  18  12  10   9 
  9   2   8   4   2       3   8   7   1   8      12  10  15   5  10 

In my earlier programming days, I was unaware of the constant trick. Changing grid size back then involved lots of search and replace and ugliness. I suppose having to endure such an ordeal is what prompts me to enjoy and fully use constants like SIZE in my code.

2 thoughts on “Building a New, Sum Matrix

  1. Matrix multiplication isn’t just multiplying corresponding values in two arrays of the same shape. It’s difficult to describe in words, it needs either a diagram or the ability to wave your arms around a lot, but I’m sure there’s loads of info on that Internet thing. Also, you can’t divide matrices.

  2. That’s good know, thanks Chris. I’m not a math genius so it’s a concept I’m unfamiliar with. I figure, you know: Brute force.

    O the things I learn…

Leave a Reply