2D Array Problems
0%

Define 2D Arrays —
3 Problems

Solutions for problems (e), (f), and (g) from your textbook — all three are 3×4 integer arrays with different value patterns. Each solution includes the matrix visual, the pattern formula, and full C code.

(e) Multiples pattern
(f) Mixed values + zeros
(g) Diagonal + zeros
(e)

Problem (e) — 3×4 Array with Multiples Pattern

Textbook Problem (e)

Define a two-dimensional, 3 × 4 integer array called n. Assign the following values to the array elements:

Required values — problem (e)

col 0
col 1
col 2
col 3
row 0
10
12
14
16
row 1
20
22
24
26
row 2
30
32
34
36
n[0][0]=10   n[0][1]=12   n[0][2]=14   n[0][3]=16  |  n[1][0]=20 ...  |  n[2][0]=30 ...

Pattern analysis — find the formula before writing code

Row 0
10, 12, 14, 16
starts at 10, step +2
Row 1
20, 22, 24, 26
starts at 20, step +2
Row 2
30, 32, 34, 36
starts at 30, step +2
Formula: n[i][j] = (i+1) * 10 + j * 2
Example: n[1][2] = (1+1)*10 + 2*2 = 20 + 4 = 24 ✓

Two approaches to assign values:

  • Method 1 — Initialise at declaration using nested braces { {...}, {...} }
  • Method 2 — Use nested for loops with the formula (i+1)*10 + j*2
problem_e.c
C
#include <stdio.h>

int main() {

    /* ── Method 1: Direct initialisation ──────────────── */
    int n[3][4] = {
        {10, 12, 14, 16},   /* row 0 */
        {20, 22, 24, 26},   /* row 1 */
        {30, 32, 34, 36}    /* row 2 */
    };

    /* ── Method 2: Assign using formula in nested loops ── */
    int m[3][4];
    int i, j;

    for (i = 0; i < 3; i++)
        for (j = 0; j < 4; j++)
            m[i][j] = (i + 1) * 10 + j * 2;
    /* n[0][0]=(0+1)*10+0*2=10  n[0][1]=(0+1)*10+1*2=12 */
    /* n[1][0]=(1+1)*10+0*2=20  n[2][3]=(2+1)*10+3*2=36 */

    /* ── Print the array ──────────────────────────────── */
    printf("Array n (Problem e):\n");
    printf("%-8s %-8s %-8s %-8s\n",
           "col 0", "col 1", "col 2", "col 3");
    printf("--------------------------------\n");

    for (i = 0; i < 3; i++) {
        printf("row %d: ", i);
        for (j = 0; j < 4; j++)
            printf("%-8d", n[i][j]);
        printf("\n");
    }

    /* Verify element access */
    printf("\nn[0][0] = %d\n", n[0][0]);  /* 10 */
    printf("n[1][2] = %d\n", n[1][2]);  /* 24 */
    printf("n[2][3] = %d\n", n[2][3]);  /* 36 */

    return 0;
}
terminal
output
Array n (Problem e):
col 0    col 1    col 2    col 3
--------------------------------
row 0:   10       12       14       16
row 1:   20       22       24       26
row 2:   30       32       34       36

n[0][0] = 10
n[1][2] = 24
n[2][3] = 36
Pattern key: Row 0 starts at 10, Row 1 at 20, Row 2 at 30 — rows are multiples of 10. Within each row, values increase by 2. So the formula is (i+1) × 10 + j × 2.
problem (f)
(f)

Problem (f) — 3×4 Array with Mixed Values and Zeros

Textbook Problem (f)

Define a two-dimensional, 3 × 4 integer array called n. Assign the following values to the array elements:

Required values — problem (f)

col 0
col 1
col 2
col 3
row 0
10
12
14
0
row 1
20
22
0
0
row 2
30
0
0
0
Amber = actual values  ·  Gray = 0 (zeros fill the upper-right triangle)

Pattern analysis — lower-left triangle filled, upper-right = 0

Row 0
10, 12, 14, 0
3 values then 0
Row 1
20, 22, 0, 0
2 values then zeros
Row 2
30, 0, 0, 0
1 value then zeros
Rule: if j < (3 - i) → value = (i+1)*10 + j*2  else → 0
Cols with values: row 0 = cols 0,1,2  |  row 1 = cols 0,1  |  row 2 = col 0 only
problem_f.c
C
#include <stdio.h>

int main() {

    /* ── Method 1: Direct initialisation ──────────────── */
    int n[3][4] = {
        {10, 12, 14, 0},   /* row 0: 3 values, then 0 */
        {20, 22,  0, 0},   /* row 1: 2 values, then 0,0 */
        {30,  0,  0, 0}    /* row 2: 1 value, then 0,0,0 */
    };

    /* ── Method 2: Using if condition in nested loops ─── */
    int m[3][4];
    int i, j;

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 4; j++) {
            if (j < (3 - i))
                m[i][j] = (i + 1) * 10 + j * 2;
            else
                m[i][j] = 0;
        }
    }
    /* Row 0: j<3 → cols 0,1,2 get values; col 3 → 0 */
    /* Row 1: j<2 → cols 0,1 get values; cols 2,3 → 0  */
    /* Row 2: j<1 → col 0 gets value; cols 1,2,3 → 0   */

    /* ── Print the array ──────────────────────────────── */
    printf("Array n (Problem f):\n");
    printf("%-8s %-8s %-8s %-8s\n",
           "col 0", "col 1", "col 2", "col 3");
    printf("--------------------------------\n");

    for (i = 0; i < 3; i++) {
        printf("row %d: ", i);
        for (j = 0; j < 4; j++)
            printf("%-8d", n[i][j]);
        printf("\n");
    }

    /* Verify */
    printf("\nn[0][2] = %d  (expected 14)\n", n[0][2]);
    printf("n[0][3] = %d  (expected 0)\n",  n[0][3]);
    printf("n[1][1] = %d  (expected 22)\n", n[1][1]);
    printf("n[2][0] = %d  (expected 30)\n", n[2][0]);

    return 0;
}
terminal
output
Array n (Problem f):
col 0    col 1    col 2    col 3
--------------------------------
row 0:   10       12       14       0
row 1:   20       22       0        0
row 2:   30       0        0        0

n[0][2] = 14  (expected 14)
n[0][3] = 0   (expected 0)
n[1][1] = 22  (expected 22)
n[2][0] = 30  (expected 30)
Key insight — lower triangle pattern: Row i has values only in columns 0 to (2-i). When j reaches (3-i), zeroes start. This is the lower-left triangular pattern — only elements where j < 3-i have values.
problem (g)
(g)

Problem (g) — 3×4 Array with Diagonal Values and Zeros

Textbook Problem (g)

Define a two-dimensional, 3 × 4 integer array called n. Assign the following values to the array elements:

Required values — problem (g)

col 0
col 1
col 2
col 3
row 0
10
0
0
0
row 1
0
20
0
0
row 2
0
0
30
0
Teal = diagonal values (10, 20, 30)  ·  Gray = 0  ·  Column 3 is always 0 (3×4 not square)

Pattern analysis — main diagonal only, everything else = 0

n[0][0]
row=0, col=0 → same index
10
n[1][1]
row=1, col=1 → same index
20
n[2][2]
row=2, col=2 → same index
30
n[i][j] where i≠j
off-diagonal
0
Formula: n[i][j] = (i==j) ? (i+1)*10 : 0
Diagonal values: 10, 20, 30 = (row+1) × 10

This is a diagonal matrix pattern. Only cells where row == column have non-zero values. The values on the diagonal are 10, 20, 30 — multiples of 10. Since the array is 3×4 (not square), column 3 has no diagonal partner and is always 0.

problem_g.c
C
#include <stdio.h>

int main() {

    /* ── Method 1: Direct initialisation ──────────────── */
    int n[3][4] = {
        {10, 0,  0,  0},   /* row 0: only [0][0]=10 */
        { 0, 20, 0,  0},   /* row 1: only [1][1]=20 */
        { 0,  0, 30, 0}    /* row 2: only [2][2]=30 */
    };

    /* ── Method 2: Using i==j condition ───────────────── */
    int m[3][4];
    int i, j;

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 4; j++) {
            if (i == j)
                m[i][j] = (i + 1) * 10;   /* diagonal: 10, 20, 30 */
            else
                m[i][j] = 0;               /* off-diagonal: 0 */
        }
    }

    /* ── Print the array ──────────────────────────────── */
    printf("Array n (Problem g):\n");
    printf("%-8s %-8s %-8s %-8s\n",
           "col 0", "col 1", "col 2", "col 3");
    printf("--------------------------------\n");

    for (i = 0; i < 3; i++) {
        printf("row %d: ", i);
        for (j = 0; j < 4; j++)
            printf("%-8d", n[i][j]);
        printf("\n");
    }

    /* Sum of diagonal elements */
    int diagSum = 0;
    for (i = 0; i < 3; i++) diagSum += n[i][i];
    printf("\nDiagonal sum = %d  (10+20+30)\n", diagSum);

    /* Verify specific cells */
    printf("n[0][0] = %d  (expected 10)\n", n[0][0]);
    printf("n[1][1] = %d  (expected 20)\n", n[1][1]);
    printf("n[0][1] = %d  (expected 0)\n",  n[0][1]);
    printf("n[2][3] = %d  (expected 0)\n",  n[2][3]);

    return 0;
}
terminal
output
Array n (Problem g):
col 0    col 1    col 2    col 3
--------------------------------
row 0:   10       0        0        0
row 1:   0        20       0        0
row 2:   0        0        30       0

Diagonal sum = 60  (10+20+30)
n[0][0] = 10  (expected 10)
n[1][1] = 20  (expected 20)
n[0][1] = 0   (expected 0)
n[2][3] = 0   (expected 0)
Diagonal pattern: The condition i == j identifies diagonal elements. Value = (i+1) × 10. Column 3 never appears on the diagonal (no row 3 in a 3×4 array), so it is always 0.
all three compared

All Three Problems — Side by Side Comparison

Each problem is a 3×4 integer array named n. The difference is only in the condition used to assign values:

  • Problem (e)Every cell gets a value using (i+1)*10 + j*2. No condition needed.
  • Problem (f) — Only cells where j < 3-i get values. Rest are 0. Lower-left triangle.
  • Problem (g) — Only cells where i == j get values. Rest are 0. Diagonal only.
all_three_combined.c
C — all three in one program
#include <stdio.h>

void printMatrix(int n[][4], char *label) {
    printf("\n%s:\n", label);
    for(int i=0;i<3;i++){
        for(int j=0;j<4;j++) printf("%5d",n[i][j]);
        printf("\n");
    }
}

int main() {
    int e[3][4], f[3][4], g[3][4];
    int i, j;

    for(i=0;i<3;i++) {
        for(j=0;j<4;j++) {

            /* Problem (e): every cell = (i+1)*10 + j*2 */
            e[i][j] = (i+1)*10 + j*2;

            /* Problem (f): lower-left triangle */
            f[i][j] = (j < 3-i) ? (i+1)*10 + j*2 : 0;

            /* Problem (g): diagonal only */
            g[i][j] = (i==j) ? (i+1)*10 : 0;
        }
    }

    printMatrix(e, "Problem (e) - All values");
    printMatrix(f, "Problem (f) - Lower triangle");
    printMatrix(g, "Problem (g) - Diagonal only");

    return 0;
}
terminal
output
Problem (e) - All values:
   10   12   14   16
   20   22   24   26
   30   32   34   36

Problem (f) - Lower triangle:
   10   12   14    0
   20   22    0    0
   30    0    0    0

Problem (g) - Diagonal only:
   10    0    0    0
    0   20    0    0
    0    0   30    0
Clean one-liner formula: All three use the same base value (i+1)*10 + j*2 — the only difference is the condition that decides whether each cell gets that value or 0. This is the elegant C way to write it using the ternary operator condition ? value : 0.

Checklist

  • I can declare a 3×4 int array using int n[3][4]
  • I can initialise it at declaration using nested braces { {row0}, {row1}, {row2} }
  • Problem (e) — I found the pattern: (i+1)*10 + j*2
  • Problem (f) — I understand the lower-left triangle: values when j < 3-i, else 0
  • Problem (g) — I understand the diagonal: values when i==j, else 0
  • I can print a 2D array using nested for loops with printf
  • I can verify individual elements using n[row][col]