Skip to study material
← Question index

QUESTION 34 · Matrices and Dimensions

Write a C program using a function to add two matrices.

5 marks

Answer in brief

Matrix addition adds corresponding elements of two matrices with identical dimensions: C[i][j] = A[i][j] + B[i][j].

Algorithm

  1. Read the common row and column counts and check their supported range.
  2. Read all elements of A and B.
  3. For each valid i and j, calculate C[i][j] = A[i][j] + B[i][j].
  4. Display the result row by row.

Worked example

For A rows [1, 2] and [3, 4], and B rows [5, 6] and [7, 8], the result rows are [6, 8] and [10, 12]. Each result position uses only the two elements at that same position.

Function interface

The add function receives both input arrays, the output array and the active dimensions. All row widths are MAX because the actual objects are declared with that width. The function writes its results into C. Reading and validation are separated from the calculation.

Input limits

The sample accepts 1 through 5 rows and columns, with element values between -1000 and 1000. These bounds keep each sum within the minimum guaranteed range of int. A failed input read stops the program before calculation.

Time complexity

There are r × c output elements, and each requires one addition. Thus the addition routine takes O(rc) time. Reading and printing all elements have the same growth rate.

Space complexity

The separate result contains rc elements, so output storage is O(rc). Apart from that output, the addition uses O(1) auxiliary space for counters. This sample allocates fixed MAX-by-MAX arrays; the dimension-based description applies to the active matrix problem.

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
#define MAX 5
int read_matrix(int a[][MAX], int rows, int cols)
{
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            if (scanf("%d", &a[i][j]) != 1 || a[i][j] < -1000 || a[i][j] > 1000) {
                return 0;
            }
        }
    }
    return 1;
}
void add(int a[][MAX], int b[][MAX], int c[][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]; }
    }
}

int main(void)
{
    int rows, cols, a[MAX][MAX], b[MAX][MAX], c[MAX][MAX];
    if (scanf("%d%d", &rows, &cols) != 2 || rows < 1 || rows > MAX || cols < 1 || cols > MAX) {
        puts("Unsupported dimensions"); return 1;
    }
    if (!read_matrix(a, rows, cols) || !read_matrix(b, rows, cols)) {
        puts("Unsupported input"); return 1;
    }
    add(a, b, c, rows, cols);
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            if (j > 0) { putchar(' '); }
            printf("%d", c[i][j]);
        }
        putchar('\n');
    }
    return 0;
}

Sample input and output

Input

2 2
1 2 3 4
5 6 7 8

Output

6 8
10 12

Related exam questions

Related questions in the multidimensional arrays category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • July 2021 · Q3(a) · 7 marks

    Using a two-dimensional array, write a C program to transpose a 2 × 2 matrix.

    Paper P04 · page 1
  • September 2023 · Q1(d) · 1 marks

    What is a two-dimensional array?

    Paper P06 · page 1

View this category in the exam question bank →