2077 5

BIT102 ยท 2077 ยท 5 marks

What do you mean by multi-dimensional array? Write program logic to add and display the sum of two m x n matrices.

What do you mean by multi-dimensional array? Write program logic to add and display the sum of two m x n matrices. [5]

C ProgrammingChapter 6: Arrays and StringsMulti-dimensional arrays

Quick answer

A multi-dimensional array is an array that has more than one dimension (index). It can be thought of as an array of arrays. The most common form is the two-dimensional (2D) array, which represents data in rows and columns (like a matrix or table).

Multi-Dimensional Array

Definition

General Syntax (C language):

data_type array_name[size1][size2]...[sizeN];

For a 2D array (matrix of m rows and n columns):

int A[m][n];

Elements are accessed using two indices: A[i][j] where i is the row index and j is the column index.


Program Logic to Add Two m x n Matrices and Display the Sum

Algorithm / Logic

  1. Declare three m x n matrices: A, B, and C (for the result).
  2. Input all elements of matrix A.
  3. Input all elements of matrix B.
  4. For each element at position [i][j], compute C[i][j] = A[i][j] + B[i][j].
  5. Display the resultant matrix C.

C Program

#include <stdio.h>

int main() {
    int m, n;
    int A[10][10], B[10][10], C[10][10];
    int i, j;

    /* Step 1: Input dimensions */
    printf("Enter number of rows (m): ");
    scanf("%d", &m);
    printf("Enter number of columns (n): ");
    scanf("%d", &n);

    /* Step 2: Input elements of Matrix A */
    printf("\nEnter elements of Matrix A (%d x %d):\n", m, n);
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            printf("A[%d][%d] = ", i, j);
            scanf("%d", &A[i][j]);
        }
    }

    /* Step 3: Input elements of Matrix B */
    printf("\nEnter elements of Matrix B (%d x %d):\n", m, n);
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            printf("B[%d][%d] = ", i, j);
            scanf("%d", &B[i][j]);
        }
    }

    /* Step 4: Add the two matrices */
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            C[i][j] = A[i][j] + B[i][j];
        }
    }

    /* Step 5: Display the resultant matrix C */
    printf("\nSum Matrix C = A + B:\n");
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            printf("%d\t", C[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Sample Output

Enter number of rows (m): 2
Enter number of columns (n): 2

Enter elements of Matrix A (2 x 2):
A[0][0] = 1   A[0][1] = 2
A[1][0] = 3   A[1][1] = 4

Enter elements of Matrix B (2 x 2):
B[0][0] = 5   B[0][1] = 6
B[1][0] = 7   B[1][1] = 8

Sum Matrix C = A + B:
6    8
10   12

Key Points

ConceptDetail
StorageElements stored in row-major order in memory
AccessA[i][j] accesses element at row i, column j
ConditionBoth matrices must have the same dimensions (m x n) for addition
Time ComplexityO(m x n) for addition

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.