תרגול: מערכים דו־ממדיים, תוכניות ואתגרים
40 השאלות של תום על מערכים דו־ממדיים: 12 שאלות יסוד, 18 תוכניות שלמות (סכומים, מקסימום, אלכסונים, שחלוף, פעולות על מטריצות, חיפוש) ו־10 אתגרים אלגוריתמיים. לכל תשובה רעיון ותוכנית C17 שלמה שעוברת הידור ב־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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
2. Read one element
int a[2][3] = {{2,4,6},{1,3,5}}; What is a[1][2]?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
3. Partial initialization
int a[2][3] = {{7}}; What are a[0][0] and a[1][2]?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
5. Sum all cells
Find the sum of {{3,-1,2},{5,0,4}}. Show the row sums.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
6. Main diagonal
For {{1,2,3},{4,5,6},{7,8,9}}, give the condition for the main diagonal and its sum.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
7. Secondary diagonal
For the same matrix, give the condition for the secondary diagonal and its sum.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
9. A function parameter
Declare a function print_grid that receives a 2D int array with 4 columns and the number of rows.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
10. What comes next in memory?
In int a[2][4];, which element is stored right after a[0][3]?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
11. A zero-filled matrix
Write one declaration of a 3 × 4 int matrix named a with all elements 0.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 12נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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
33נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 24נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 9נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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:
5נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 6נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 36נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 34נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 6נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 10נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 154נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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:
YESנסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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:
YESנסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 1נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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:
2נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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:
147נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 24נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 0נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 2נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 7נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 9נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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:
YESנסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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:
2נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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:
4נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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
28נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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 0נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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:
7נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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:
4נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.