> For the complete documentation index, see [llms.txt](https://hkust-robotics-team.gitbook.io/hkust-robotics-team-software-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hkust-robotics-team.gitbook.io/hkust-robotics-team-software-tutorial/tutorial/tutorial-1-c-and-cubeide-setup/classwork/classwork-4-matrix-addition.md).

# 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=\begin{bmatrix} a\_1 & a\_2 \ a\_3 & a\_4 \ a\_5 & a\_6 \end{bmatrix}$$

We call the matrix above a $$3\times2$$matrix with entries $$a\_1$$to $$a\_6$$

Now, given another $$3\times2$$ matrix $$B$$

$$B=\begin{bmatrix} b\_1 & b\_2 \ b\_3 & b\_4 \ b\_5 & b\_6 \end{bmatrix}$$

Since the size of $$A$$ and $$B$$ are the same, we can perform matrix addition:

$$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.

```c
#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;
}
```
