45 דק׳

תרגול: מערכים דו־ממדיים, 30 שאלות קצרות

30 השאלות הקצרות של תום על מערכים דו־ממדיים: גדלים ואינדקסים, לולאות מקוננות, סכומים, אלכסונים, גבולות, אתחול, שחלוף ורשתות חיישנים. כל תשובה מוסתרת עד שפותחים אותה.

באנגלית בלבד

השיעור הזה כתוב באנגלית בלבד.

Answer each question on paper first, then open the answer. Indexes start at 0 everywhere. The questions follow the three lessons of the unit: rows, columns and nested loops, totals, maximum and counting and diagonals, transpose and functions.

Sizes, indexes and loops

1. Rows and columns

How many rows and how many columns does int a[3][5]; have?

Answer

נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

    2. Number of elements

    How many int elements does int a[4][6]; store?

    Answer

    נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

      3. Write the expression

      Write the expression for row 2, column 3 of the matrix a.

      Answer

      נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

        4. The last column

        What is the index of the last valid column in int a[2][7];?

        Answer

        נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

          5. What is printed?

          int a[2][2] = {{1, 2}, {3, 4}};
          printf("%d", a[1][0]);
          Answer

          נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

            6. Loop headers

            Write the two nested for headers that visit every cell of int a[3][4];.

            Answer

            נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

              Sums and diagonals

              7. Sum of all values

              For int a[2][3] = {{1,2,3},{4,5,6}};, find the sum of all values.

              Answer

              נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                8. Main diagonal

                For int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};, find the sum of the main diagonal.

                Answer

                נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                  9. Secondary diagonal

                  For the same 3 × 3 matrix, find the sum of the secondary diagonal.

                  Answer

                  נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                    10. Main diagonal condition

                    Write the condition that is true when the cell (i, j) is on the main diagonal.

                    Answer

                    נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                      11. Secondary diagonal condition

                      Write the condition for the secondary diagonal of an N × N matrix.

                      Answer

                      נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                        12. Cells on a diagonal

                        How many cells of a 5 × 5 matrix are on at least one of the two diagonals? Count the centre once.

                        Answer

                        נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                          13. Sum of a row

                          What is the sum of row 1 of {{2,4,6},{1,3,5}}?

                          Answer

                          נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                            14. Sum of a column

                            What is the sum of column 0 of {{2,4,6},{1,3,5}}?

                            Answer

                            נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                              15. Where is 8?

                              What are the coordinates (row, column) of the value 8 in {{1,2,3},{4,5,6},{7,8,9}}?

                              Answer

                              נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                Bounds and traversal

                                16. Out of bounds

                                int a[3][4]; What is the problem with a[3][0]?

                                Answer

                                נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                  17. Add 1 to a cell

                                  Complete the statement that adds 1 to the cell a[i][j].

                                  Answer

                                  נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                    18. Number of visits

                                    How many cells does a full traversal of an N-by-M matrix visit?

                                    Answer

                                    נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                      19. Complexity of a full sum

                                      What is the time complexity of summing every cell of an N-by-M matrix?

                                      Answer

                                      נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                        20. The fastest index

                                        When you scan a C matrix row by row, which loop index changes fastest?

                                        Answer

                                        נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                          Initialization, maximum, counting, transpose

                                          21. All zeros

                                          Write a declaration that sets all elements of int a[2][3] to zero.

                                          Answer

                                          נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                            22. The largest value

                                            What is the largest value in {{-4,7},{2,5}}?

                                            Answer

                                            נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                              23. Even numbers

                                              How many even numbers are in {{1,2,3},{4,5,6}}?

                                              Answer

                                              נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                                24. Transpose

                                                What is the transpose of {{1,2,3},{4,5,6}}?

                                                Answer

                                                נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                                  25. Average

                                                  What is the average of the readings {{20,22},{24,26}}?

                                                  Answer

                                                  נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                                    Sensor grids

                                                    26. Percentage above 80 °C

                                                    A 4 × 5 sensor grid has 3 readings above 80 °C. What percentage of the readings is above 80 °C?

                                                    Answer

                                                    נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                                      27. Count above 70

                                                      In a 3 × 4 temperature grid, how do you count the values strictly above 70?

                                                      Answer

                                                      נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                                        28. The lower-right corner

                                                        In a 10 × 10 grid, which cell is the lower-right corner?

                                                        Answer

                                                        נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                                          29. Sum of column j

                                                          All rows have the same length. How do you compute the sum of column j?

                                                          Answer

                                                          נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                                                            30. A hot spot

                                                            Describe a check that raises an alert when any reading of a 3 × 3 grid exceeds a threshold T.

                                                            Answer

                                                            נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.