45 min

Practice: 2D arrays, 30 short questions

Tom's 30 short questions on two-dimensional arrays: sizes and indexes, nested loops, sums, diagonals, bounds, initialization, transpose and sensor grids. Every answer is hidden until you open it.

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

Try it yourself first, then open the solution step by step.

    2. Number of elements

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

    Answer

    Try it yourself first, then open the solution step by step.

      3. Write the expression

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

      Answer

      Try it yourself first, then open the solution step by step.

        4. The last column

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

        Answer

        Try it yourself first, then open the solution step by step.

          5. What is printed?

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

          Try it yourself first, then open the solution step by step.

            6. Loop headers

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

            Answer

            Try it yourself first, then open the solution step by step.

              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

              Try it yourself first, then open the solution step by step.

                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

                Try it yourself first, then open the solution step by step.

                  9. Secondary diagonal

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

                  Answer

                  Try it yourself first, then open the solution step by step.

                    10. Main diagonal condition

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

                    Answer

                    Try it yourself first, then open the solution step by step.

                      11. Secondary diagonal condition

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

                      Answer

                      Try it yourself first, then open the solution step by step.

                        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

                        Try it yourself first, then open the solution step by step.

                          13. Sum of a row

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

                          Answer

                          Try it yourself first, then open the solution step by step.

                            14. Sum of a column

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

                            Answer

                            Try it yourself first, then open the solution step by step.

                              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

                              Try it yourself first, then open the solution step by step.

                                Bounds and traversal

                                16. Out of bounds

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

                                Answer

                                Try it yourself first, then open the solution step by step.

                                  17. Add 1 to a cell

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

                                  Answer

                                  Try it yourself first, then open the solution step by step.

                                    18. Number of visits

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

                                    Answer

                                    Try it yourself first, then open the solution step by step.

                                      19. Complexity of a full sum

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

                                      Answer

                                      Try it yourself first, then open the solution step by step.

                                        20. The fastest index

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

                                        Answer

                                        Try it yourself first, then open the solution step by step.

                                          Initialization, maximum, counting, transpose

                                          21. All zeros

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

                                          Answer

                                          Try it yourself first, then open the solution step by step.

                                            22. The largest value

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

                                            Answer

                                            Try it yourself first, then open the solution step by step.

                                              23. Even numbers

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

                                              Answer

                                              Try it yourself first, then open the solution step by step.

                                                24. Transpose

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

                                                Answer

                                                Try it yourself first, then open the solution step by step.

                                                  25. Average

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

                                                  Answer

                                                  Try it yourself first, then open the solution step by step.

                                                    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

                                                    Try it yourself first, then open the solution step by step.

                                                      27. Count above 70

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

                                                      Answer

                                                      Try it yourself first, then open the solution step by step.

                                                        28. The lower-right corner

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

                                                        Answer

                                                        Try it yourself first, then open the solution step by step.

                                                          29. Sum of column j

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

                                                          Answer

                                                          Try it yourself first, then open the solution step by step.

                                                            30. A hot spot

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

                                                            Answer

                                                            Try it yourself first, then open the solution step by step.