Published by Patrick Mutisya · 14 days ago
Write pseudocode for 1‑D and 2‑D arrays.
array[i] for 1‑D, array[row][col] for 2‑D.DECLARE scores[5] AS INTEGER
FOR i FROM 0 TO 4
scores[i] ← 0
END FOR
scores[0] ← 85
scores[1] ← 92
scores[2] ← 78
scores[3] ← 90
scores[4] ← 88
PRINT "Score at index 2: ", scores[2]
sum ← 0
FOR i FROM 0 TO 4
sum ← sum + scores[i]
END FOR
PRINT "Total score: ", sum
DECLARE matrix[3][4] AS INTEGER
FOR row FROM 0 TO 2
FOR col FROM 0 TO 3
matrix[row][col] ← 0
END FOR
END FOR
matrix[0][0] ← 1 matrix[0][1] ← 2 matrix[0][2] ← 3 matrix[0][3] ← 4
matrix[1][0] ← 5 matrix[1][1] ← 6 matrix[1][2] ← 7 matrix[1][3] ← 8
matrix[2][0] ← 9 matrix[2][1] ← 10 matrix[2][2] ← 11 matrix[2][3] ← 12
PRINT "Element at (1,2): ", matrix[1][2]
FOR row FROM 0 TO 2
sum ← 0
FOR col FROM 0 TO 3
sum ← sum + matrix[row][col]
END FOR
average ← sum / 4
PRINT "Average of row ", row, ": ", average
END FOR
| Row / Col | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 |
| 1 | 5 | 6 | 7 | 8 |
| 2 | 9 | 10 | 11 | 12 |
For a 1‑D array of length \$n\$, the element at index \$i\$ is denoted \$A[i]\$, where \$0 \le i < n\$.
For a 2‑D array with \$r\$ rows and \$c\$ columns, the element at row \$i\$ and column \$j\$ is denoted \$M[i][j]\$, where \$0 \le i < r\$ and \$0 \le j < c\$.
length - 1.