Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 19

Addition of two matrices using functions

CArrays, pointers and functionsJNTUH R22 · CS108ES

Aim

To write a C program that reads two matrices of the same order and uses functions to read them, add them and print the sum matrix.

Algorithm

  1. Read the number of rows r and columns c. If either is not between 1 and 10, print an error and stop.
  2. Call read_matrix to read the r × c elements of matrix A, then of matrix B.
  3. Call add_matrices: for every row i and column j, set C[i][j] = A[i][j] + B[i][j].
  4. Call print_matrix to print C row by row.

Program

C
#include <stdio.h>

#define MAX 10

int read_matrix(int m[MAX][MAX], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (scanf("%d", &m[i][j]) != 1) {
                return 0;
            }
        }
    }
    return 1;
}

void add_matrices(int a[MAX][MAX], int b[MAX][MAX], int c[MAX][MAX], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            c[i][j] = a[i][j] + b[i][j];
        }
    }
}

void print_matrix(int m[MAX][MAX], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%5d", m[i][j]);
        }
        printf("\n");
    }
}

int main(void) {
    int a[MAX][MAX], b[MAX][MAX], c[MAX][MAX];
    int rows, cols;

    printf("Enter the number of rows and columns: ");
    if (scanf("%d %d", &rows, &cols) != 2 || rows < 1 || rows > MAX || cols < 1 || cols > MAX) {
        printf("Rows and columns must be between 1 and %d\n", MAX);
        return 1;
    }

    printf("Enter the elements of matrix A (%d x %d):\n", rows, cols);
    if (!read_matrix(a, rows, cols)) {
        printf("Invalid input\n");
        return 1;
    }
    printf("Enter the elements of matrix B (%d x %d):\n", rows, cols);
    if (!read_matrix(b, rows, cols)) {
        printf("Invalid input\n");
        return 1;
    }

    add_matrices(a, b, c, rows, cols);

    printf("Sum matrix A + B:\n");
    print_matrix(c, rows, cols);
    return 0;
}

Output

Output
Enter the number of rows and columns: 2 3
Enter the elements of matrix A (2 x 3):
1 2 3
4 5 6
Enter the elements of matrix B (2 x 3):
7 8 9
-1 0 10
Sum matrix A + B:
    8   10   12
    3    5   16

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

Two matrices can be added only when they have the same number of rows and the same number of columns, so the program reads the order once and uses it for both.

When a 2-D array is a function parameter, the number of columns must be given (int m[MAX][MAX] or int m[][MAX]), because the compiler needs the row length to find m[i][j] in memory.

%5d prints each number in a field 5 characters wide, so the columns line up.

Common mistakes

  • Writing the parameter as int m[][]. This is a compile error; the column size must be given, as in int m[][MAX].

  • Mixing up the loop limits, for example running j up to rows. For a non-square matrix this reads elements that were never entered. Rows go with i, columns with j.

  • Printing the newline inside the inner loop, which puts every element on its own line. Print \n after the inner loop ends, once per row.

  • Trying to add matrices of different orders. Addition needs both matrices to be r × c with the same r and c.

Viva questions

Try to answer each one aloud before you open it.

  • What is the condition for adding two matrices?

    Both matrices must have the same order, that is, the same number of rows and the same number of columns.

  • Why must the column size be given when a 2-D array is passed to a function?

    A 2-D array is stored row after row in memory. To find m[i][j] the compiler computes i × columns + j, so it must know the number of columns.

  • How are the elements of a 2-D array stored in memory in C?

    In row-major order: all elements of row 0 first, then all of row 1, and so on.

  • Is matrix addition commutative?

    Yes. A + B equals B + A, because each element is a sum of two numbers and number addition is commutative.

  • How many additions are needed to add two r × c matrices?

    One addition per element, so r × c additions.

Learn the concepts

All Programming for Problem Solving Lab experiments →