Solutions for those exercises.

Multiple Choices

  1. What is the correct way of creating a 2-dimensional rectangular array of int with 5 rows and 2 columns named myMatrix?

    • int[][] myMatrix = new int[5][2];
    • int[][] myMatrix = new int[2][5];
    • int[,] myMatrix = new int[2, 5];
    • int[,] myMatrix = new int[5, 2];
  2. Consider the following code:

    int[,] grades = {{10, 20}, {30, 40}};
    Console.WriteLine(grades[1,0]);

    What will it display?

    • Nothing
    • 10
    • 20
    • grades
    • 30
    • grades[1,0]
    • 40

Exercises

  1. Write a statement that creates a 2-dimensional rectangular array of int with 5 rows and 3 columns.

  2. Write a statement that creates a 2-dimensional jagged array of int with 2 rows. The first row should contain an array containing 1, the second row should contain an array containing 2, 3.

  3. Write a declaration for a 2-dimensional rectangular array of int containing the following data:

       
    102030
    405060
    708090
  4. Write a declaration for a 2-dimensional ragged array of int containing the following data:

       
    1020
    40
    708090
  5. Suppose we have a 2-dimensional rectangular array named temp that has been declared and initialized. How can we know the number of rows in this array?

  6. Suppose we have a 2-dimensional rectangular array named temp that has been declared and initialized. How can we know the number of elements in this array?

  7. Write a program that display “Every row contains its own index” if the 2-dimensional rectangular array of int matrix is such that its first row contains the value 0, its second row contains the value 1, etc.

  8. Write a program that display the average of each row of a 2-dimensional jagged array of int jArray.

  9. Write a program that display the sum of the values on the diagonal of a 2-dimensional rectangular array of int jArray.

Problem: Toward a Crossword Puzzle Solver

The goal of this problem is to work toward the creation of a program that solve crossword puzzles. We will reason in the simple case where the “word” is actually simply a pair of number (so, “1, 2” or “8, 101”).

In the following, assume given two int variables first and second, as well as a 2-dimensional rectangular array values.

  1. Write a program that display “pair found” if first and second occur next to each other in the same row.
  2. Edit your program so that “pair found” is displayed also if second occurs before first in the same row.
  3. Edit your program so that “pair found” is displayed also if first occurs “above” second (that is, if they are next to each other in the same column),
  4. Edit your program so that “pair found” is displayed also if second occurs “above” first,
  5. Edit your program so that “pair found” is displayed also if first and second occur diagonally,
  6. Edit your program so that “pair found” is displayed also if first and first occur anti-diagonally.

Test your program thoroughly, possibly bundling it in a static class to ease testing and debugging.