Problem (e) — 3×4 Array with Multiples Pattern
Define a two-dimensional, 3 × 4 integer array called n. Assign the following values to the array elements:
Required values — problem (e)
Pattern analysis — find the formula before writing code
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
#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; }
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
(i+1) × 10 + j × 2.Problem (f) — 3×4 Array with Mixed Values and Zeros
Define a two-dimensional, 3 × 4 integer array called n. Assign the following values to the array elements:
Required values — problem (f)
Pattern analysis — lower-left triangle filled, upper-right = 0
Cols with values: row 0 = cols 0,1,2 | row 1 = cols 0,1 | row 2 = col 0 only
#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; }
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)
j < 3-i have values.Problem (g) — 3×4 Array with Diagonal Values and Zeros
Define a two-dimensional, 3 × 4 integer array called n. Assign the following values to the array elements:
Required values — problem (g)
Pattern analysis — main diagonal only, everything else = 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.
#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; }
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)
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 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-iget values. Rest are 0. Lower-left triangle. - Problem (g) — Only cells where
i == jget values. Rest are 0. Diagonal only.
#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; }
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
(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]