Practice: 2D arrays, programs and challenges
Tom's 40-question set on two-dimensional arrays: 12 fundamentals, 18 complete programs (sums, maximum, diagonals, transpose, matrix operations, searching) and 10 algorithm challenges. Every answer has the idea and a complete C17 program that compiles in Visual Studio.
Questions 1-12 check the basics in one line each. Questions 13-30 are complete programs: write them in Visual Studio, run them, and test them. Questions 31-40 are challenges for students who want more. Every answer is hidden until you open it; try first.
All programs read their input with scanf and start with #define _CRT_SECURE_NO_WARNINGS for Visual Studio. They follow Tom's reference solutions: if (scanf(...) != 1) return 1; stops the program when the input is not a number.
Fundamentals (1-12)
1. Valid indexes
For int a[3][4];, give the valid row indexes, the valid column indexes and the number of elements.
Try it yourself first, then open the solution step by step.
2. Read one element
int a[2][3] = {{2,4,6},{1,3,5}}; What is a[1][2]?
Try it yourself first, then open the solution step by step.
3. Partial initialization
int a[2][3] = {{7}}; What are a[0][0] and a[1][2]?
Try it yourself first, then open the solution step by step.
4. Row-by-row output
Nested loops print {{1,2},{3,4}} with the row index in the outer loop and the column index in the inner loop. What is printed?
Try it yourself first, then open the solution step by step.
5. Sum all cells
Find the sum of {{3,-1,2},{5,0,4}}. Show the row sums.
Try it yourself first, then open the solution step by step.
6. Main diagonal
For {{1,2,3},{4,5,6},{7,8,9}}, give the condition for the main diagonal and its sum.
Try it yourself first, then open the solution step by step.
7. Secondary diagonal
For the same matrix, give the condition for the secondary diagonal and its sum.
Try it yourself first, then open the solution step by step.
8. Fix an out-of-bounds loop
i runs over the rows of int a[3][4];. Correct for (int i = 0; i <= 3; i++) and explain the problem.
Try it yourself first, then open the solution step by step.
9. A function parameter
Declare a function print_grid that receives a 2D int array with 4 columns and the number of rows.
Try it yourself first, then open the solution step by step.
10. What comes next in memory?
In int a[2][4];, which element is stored right after a[0][3]?
Try it yourself first, then open the solution step by step.
11. A zero-filled matrix
Write one declaration of a 3 × 4 int matrix named a with all elements 0.
Try it yourself first, then open the solution step by step.
12. Transpose sizes
A has 2 rows and 3 columns. What is the size of its transpose B? Write B[j][i] in terms of A.
Try it yourself first, then open the solution step by step.
Programs: basic and intermediate (13-30)
Write a complete program for each question and test it with three cases: a typical one, a boundary one and a special one (for example all zeros, negative numbers, or a tie). Assume the input is valid unless the question says otherwise.
13. Read and print a matrix
Read 12 integers into a 3 × 4 matrix (row by row) and print its three rows on separate lines, the four numbers of a row separated by spaces.
Example input:
1 2 3 4
5 6 7 8
9 10 11 12Expected output:
1 2 3 4
5 6 7 8
9 10 11 12Try it yourself first, then open the solution step by step.
14. Sum every row
Read a 4 × 3 matrix and print the sum of each row on a separate line.
Example input:
1 2 3
4 5 6
7 8 9
10 11 12Expected output:
6
15
24
33Try it yourself first, then open the solution step by step.
15. Sum every column
Read a 3 × 4 matrix and print the four column sums on one line, separated by spaces.
Example input:
1 2 3 4
5 6 7 8
9 10 11 12Expected output:
15 18 21 24Try it yourself first, then open the solution step by step.
16. Minimum and maximum
Read a 3 × 3 matrix and print the minimum and then the maximum. Negative values are allowed.
Example input:
4 -2 7
0 9 -5
3 8 1Expected output:
-5 9Try it yourself first, then open the solution step by step.
17. Count above a threshold
Read a 4 × 5 matrix and then a number T. Print how many elements are strictly greater than T.
Example input:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
15Expected output:
5Try it yourself first, then open the solution step by step.
18. Average and even count
Read a 3 × 4 matrix (values from -1000 to 1000). Print the average with three decimals and the number of even elements.
Example input:
1 2 3 4
5 6 7 8
9 10 11 12Expected output:
6.500 6Try it yourself first, then open the solution step by step.
19. Row with the largest sum
Read a 4 × 4 matrix. Print the index of the row with the largest sum and that sum. If rows tie, print the smallest index.
Example input:
1 2 3 4
9 9 9 9
5 5 5 5
9 9 9 9Expected output:
1 36Try it yourself first, then open the solution step by step.
20. Diagonal sums
Read a 4 × 4 matrix. Print the sum of the main diagonal and the sum of the secondary diagonal, in that order.
Example input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16Expected output:
34 34Try it yourself first, then open the solution step by step.
21. Transpose a rectangle
Read a 2 × 3 matrix and print its 3 × 2 transpose: three lines with two numbers each.
Example input:
1 2 3
4 5 6Expected output:
1 4
2 5
3 6Try it yourself first, then open the solution step by step.
22. Add two matrices
Read two 3 × 3 matrices A and B (9 numbers for A, then 9 for B). Print A + B as three rows.
Example input:
1 2 3
4 5 6
7 8 9
9 8 7
6 5 4
3 2 1Expected output:
10 10 10
10 10 10
10 10 10Try it yourself first, then open the solution step by step.
23. Multiply two matrices
Read A, a 2 × 3 matrix, and B, a 3 × 2 matrix. Print their 2 × 2 product.
Example input:
1 2 3
4 5 6
7 8
9 10
11 12Expected output:
58 64
139 154Try it yourself first, then open the solution step by step.
24. Check symmetry
Read a 4 × 4 matrix. Print YES if a[i][j] == a[j][i] for all cells, otherwise NO.
Example input:
1 2 3 4
2 5 6 7
3 6 8 9
4 7 9 10Expected output:
YESTry it yourself first, then open the solution step by step.
25. Check an identity matrix
Read a 4 × 4 matrix. Print YES if the main diagonal is all 1 and every other cell is 0, otherwise NO.
Example input:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1Expected output:
YESTry it yourself first, then open the solution step by step.
26. First matching position
Read a 3 × 4 matrix and then a target value. Print the row and column of the first cell (in row-by-row order) that holds the target, or -1 -1 if it is not there.
Example input:
5 3 8 1
7 3 9 2
4 6 3 0
3Expected output:
0 1Try it yourself first, then open the solution step by step.
27. Strict local peaks
Read a 5 × 5 matrix. Count the interior cells that are strictly greater than all eight neighbors. Cells on the edge cannot be peaks.
Example input:
1 1 1 1 1
1 5 1 1 1
1 1 1 7 1
1 1 1 1 1
1 1 1 1 1Expected output:
2Try it yourself first, then open the solution step by step.
28. Sum of the border
Read a 4 × 5 matrix. Add up every cell on the border exactly once.
Example input:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20Expected output:
147Try it yourself first, then open the solution step by step.
29. Column with the largest sum
Read a 3 × 4 matrix. Print the index of the column with the largest sum and that sum. Ties: the smallest index.
Example input:
1 2 3 4
5 6 7 8
9 10 11 12Expected output:
3 24Try it yourself first, then open the solution step by step.
30. Normalize every row
Read a 3 × 4 matrix (values from -1000 to 1000). In each row, subtract the minimum of that row from every element, and print the result.
Example input:
4 2 7 5
-1 3 0 2
10 10 10 10Expected output:
2 0 5 3
0 4 1 3
0 0 0 0Try it yourself first, then open the solution step by step.
Algorithm challenges (31-40)
These go beyond the exam. Each one needs an idea first, then careful handling of the edge cases. For each one, also say how the time and the memory grow with the size of the matrix (the complexity).
31. Rotate a square matrix 90°
Read N (1 to 20) and an N × N matrix. Rotate it 90 degrees clockwise in place, without a second N × N matrix, and print it.
Example input:
2
1 2
3 4Expected output:
3 1
4 2Try it yourself first, then open the solution step by step.
32. Spiral order
Read R and C (1 to 20) and an R × C matrix. Print its elements clockwise in a spiral, starting from the top-left cell.
Example input:
3 4
1 2 3 4
5 6 7 8
9 10 11 12Expected output:
1 2 3 4 8 12 11 10 9 5 6 7Try it yourself first, then open the solution step by step.
33. Set matrix zeroes
Read R and C (1 to 20) and an R × C matrix. If a cell was 0 at the start, set its whole row and its whole column to 0. Use O(1) extra space.
Example input:
3 3
1 2 3
4 0 6
7 8 9Expected output:
1 0 3
0 0 0
7 0 9Try it yourself first, then open the solution step by step.
34. Search a sorted matrix
Read R and C (1 to 100), an R × C matrix whose rows and columns are nondecreasing, and a target. Print YES or NO. Search in O(R + C).
Example input:
3 3
1 4 7
2 5 8
3 6 9
6Expected output:
YESTry it yourself first, then open the solution step by step.
35. Number of islands
Read R and C (1 to 30) and an R × C grid of 0 and 1. Count the islands: groups of 1-cells connected up, down, left or right.
Example input:
3 4
1 1 0 0
0 1 0 1
0 0 0 1Expected output:
2Try it yourself first, then open the solution step by step.
36. Largest square of ones
Read R and C (1 to 100) and a matrix of 0 and 1. Print the area of the largest square made only of 1s. Use O(C) extra memory.
Example input:
3 4
1 1 0 1
1 1 1 0
0 1 1 0Expected output:
4Try it yourself first, then open the solution step by step.
37. Rectangle sums
Read R and C (1 to 50), the matrix, then Q (1 to 100) and Q rectangles r1 c1 r2 c2 (corners included). Print the sum of each rectangle, using a prefix-sum table.
Example input:
3 3
1 2 3
4 5 6
7 8 9
2
0 0 1 1
1 1 2 2Expected output:
12
28Try it yourself first, then open the solution step by step.
38. One step of the Game of Life
Read R and C (1 to 20) and a grid of 0 (dead) and 1 (alive). Compute one generation, all cells at once: a live cell stays alive with 2 or 3 live neighbors; a dead cell comes alive with exactly 3. Cells outside the grid are dead.
Example input:
3 3
0 1 0
0 1 0
0 1 0Expected output:
0 0 0
1 1 1
0 0 0Try it yourself first, then open the solution step by step.
39. Minimum path sum
Read R and C (1 to 100) and costs from 0 to 100. Start at the top-left cell and move only right or down to the bottom-right cell. Print the smallest possible sum of the costs on the path, both ends included.
Example input:
3 3
1 3 1
1 5 1
4 2 1Expected output:
7Try it yourself first, then open the solution step by step.
40. Longest increasing path
Read R and C (1 to 30) and a grid of integers. Moving up, down, left or right, and only to a strictly larger value, find the length of the longest path.
Example input:
3 3
9 9 4
6 6 8
2 1 1Expected output:
4Try it yourself first, then open the solution step by step.