Operations on Two-Dimensional arrays (addition, subtraction, multiplication, transpose, search, merge string)

Every 2D-array operation is nested loops wearing a different body: addition and subtraction copy cell by cell, transpose swaps the indexes (b[j][i] = a[i][j]), search scans the grid, and multiplication adds a third loop for row-into-column.

12 min read · 11 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The maths you already passed

In BCA102 you added matrices, transposed them, even multiplied them, on paper, for marks.

Today the same operations return wearing C syntax, and here is the liberating secret: every 2D operation is just nested loops with a different one-line body.

Addition? Nested loops, body adds. Transpose? Nested loops, body flips indexes. Multiplication? One extra loop. If you can write for (i...) for (j...), you can write them all.

Theory

Same tour, different chore

Think of the nested loop as a tour route that visits every cell of the grid, row by row, seat by seat. The route never changes; only the CHORE at each seat does. Addition: "add the twin cells, drop the result here". Transpose: "copy this cell to its mirror seat". Search: "is this the value we want?". One route, many chores, that mental model collapses five exam programs into one.

Theory

Addition, subtraction, transpose, search

For same-order grids a and b (BCA102's rule survives: same order to add):

c[i][j] = a[i][j] + b[i][j]; /* addition */

c[i][j] = a[i][j] - b[i][j]; /* subtraction */

Transpose (rows become columns, m×n becomes n×m):

b[j][i] = a[i][j]; /* indexes swap roles */

Search for a value: tour every cell, and on a match report BOTH coordinates:

if (a[i][j] == target) printf("at row %d col %d", i, j);

Practical

Adding two test grids (2 students × 3 subjects)

#include <stdio.h>

int main() {
    int t1[2][3] = {{70, 80, 90}, {60, 75, 85}};
    int t2[2][3] = {{75, 82, 88}, {65, 70, 90}};
    int total[2][3];
    int i, j;

    for (i = 0; i < 2; i++)
        for (j = 0; j < 3; j++)
            total[i][j] = t1[i][j] + t2[i][j];

    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) printf("%d ", total[i][j]);
        printf("\n");
    }
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

Multiplication: the third loop

BCA102 taught you: c's cell (i, j) = row i of a into column j of b, multiply pairs, add them up. "Add them up" is itself a loop, so multiplication needs THREE:

for (i = 0; i < m; i++)

for (j = 0; j < p; j++) {

c[i][j] = 0; /* start the sum */

for (k = 0; k < n; k++)

c[i][j] += a[i][k] * b[k][j];

}

The new counter k walks the shared (inner) dimension: along row i of a and simultaneously down column j of b. Forgetting c[i][j] = 0 before accumulating is the classic bug.

Think first

Trace the transpose

a is 2×3 with rows {1, 2, 3} and {4, 5, 6}. Apply b[j][i] = a[i][j] for every cell. What is b, and what is its order?

Show the answer

b is 3×2:

1 4

2 5

3 6

Row 0 of a (1, 2, 3) stood up to become COLUMN 0 of b. The index swap b[j][i] = a[i][j] does all the work; the loops just tour. If your b came out 2×3, you declared it with the old order, transpose flips the declaration too.

Quiz

In the multiplication code, what does the third counter k actually walk along?

  1. The shared inner dimension: across row i of a and down column j of b together
  2. The rows of the result matrix c
  3. The columns of the result matrix c
  4. All elements of both matrices randomly
Show the answer

The shared inner dimension: across row i of a and down column j of b together

For one result cell, k pairs a[i][k] with b[k][j]: k-th element of a's row i with k-th element of b's column j, multiplied and accumulated. That is precisely BCA102's row-into-column, mechanised. i and j choose WHICH cell; k COMPUTES it. Naming each loop's job is exactly how examiners phrase this.

Quiz

Two char arrays hold "Ri" and "ya". To merge them into one array holding "Riya", the program copies the first string, then...

  1. starts writing the second string AT the first one's '\0' position, and terminates the result
  2. writes the second string after the first array's last box
  3. adds the ASCII codes of the two strings
  4. uses the + operator: "Ri" + "ya"
Show the answer

starts writing the second string AT the first one's '\0' position, and terminates the result

Merging (concatenation) means overwriting the first string's terminator with the second string's first letter, copying on, then closing with a fresh '\0'. C has NO + for strings (option D compiles into nonsense); the ready-made version of this loop is strcat, arriving in Unit 4. The '\0' bookkeeping is the whole game.

Watch out

Where marks leak

Forgetting c[i][j] = 0 before the k-loop: garbage joins your sum. Order rules travel from BCA102: addition needs identical orders; multiplication needs a's columns = b's rows, state the check in exam answers. Transpose declared wrong: an m×n transposes into an n×m array. And in the merge, dropping the final '\0' leaves a string that never ends, printf %s will happily prove it.

Theory

The unit's second half awaits

You now command grids: build, tour, add, flip, search, multiply. What you do NOT yet know is what an array name really IS, and why scanf never needed & for one. The answer, addresses, opens the most feared and most interview-asked topic in C: pointers. Next lesson defuses it with one picture.

Summary

Key takeaways

  • All 2D operations are nested loops with different bodies (the tour and the chore).
  • Addition/subtraction: cell-wise on same-order grids: c[i][j] = a[i][j] ± b[i][j].
  • Transpose: b[j][i] = a[i][j]; m×n becomes n×m.
  • Multiplication: three loops; k walks the shared dimension; zero c[i][j] first.
  • Search reports both coordinates; string merge overwrites '\0' and re-terminates.
  • Memory hook: same tour, different chore.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of Arrays and Pointer

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Operations on Two-Dimensional arrays (addition, subtraction, multiplication, transpose, search, merge string) · Computer Programming and Programming Methodology (CPPM) · Gri-Learn