Classwork 4 : Matrix Addition

What do mathematicians sleep on? Matrices!!

A matrix is a rectangular arrangement of numbers into rows and columns. Each number in a matrix refers to a matrix element.

To perform matrix addition, you need to have two matrices of the same size. Then you can sum up the entries respectively.

Example:

A=[a1a2a3a4a5a6]A=\begin{bmatrix} a_1 & a_2 \\ a_3 & a_4 \\ a_5 & a_6 \end{bmatrix}

We call the matrix above a 3×23\times2matrix with entries a1a_1to a6a_6

Now, given another 3×23\times2 matrix BB

B=[b1b2b3b4b5b6]B=\begin{bmatrix} b_1 & b_2 \\ b_3 & b_4 \\ b_5 & b_6 \end{bmatrix}

Since the size of AA and BB are the same, we can perform matrix addition:

A+B=[a1+b1a2+b2a3+b3a4+b4a5+b5a6+b6]A+B=\begin{bmatrix} a_1+b_1 & a_2+b_2 \\ a_3+b_3 & a_4+b_4 \\ a_5+b_5 & a_6+b_6 \end{bmatrix}

Task

Implement the code to perform matrix addition.

#include <stdio.h>

void display_matrix(int matrix[][2], int num_row) {
  for (int r = 0; r < num_row; r++) {
    for (int c = 0; c < 2; c++) {
      printf("\t%d", matrix[r][c]);
    }
    printf("\n");
  }
}

int main() {
  int matrix_A[3][2] = {
    {0, 1},
    {2, 3},
    {4, 5}
  };
  
  int matrix_B[3][2] = {
    {0, 1},
    {2, 3},
    {4, 5}
  };
  
  int result[3][2] = {0};
  
  // your code starts here
  
  // your code ends here
  
  printf("A = \n");
  display_matrix(matrix_A, 3);
  printf("\nB = \n");
  display_matrix(matrix_B, 3);
  printf("\nA + B = \n");
  display_matrix(result, 3);
  return 0;
}

Last updated