Solutions for those exercises.
Multiple Choices
-
What is the correct way of creating a 2-dimensional rectangular array of
int
with 5 rows and 2 columns namedmyMatrix
?-
int[][] myMatrix = new int[5][2];
-
int[][] myMatrix = new int[2][5];
-
int[,] myMatrix = new int[2, 5];
-
int[,] myMatrix = new int[5, 2];
-
-
Consider the following code:
What will it display?
- Nothing
- 10
- 20
- grades
- 30
- grades[1,0]
- 40
Exercises
-
Write a statement that creates a 2-dimensional rectangular array of
int
with 5 rows and 3 columns. -
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. -
Write a declaration for a 2-dimensional rectangular array of
int
containing the following data:10 20 30 40 50 60 70 80 90 -
Write a declaration for a 2-dimensional ragged array of
int
containing the following data:10 20 40 70 80 90 -
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? -
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? -
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. -
Write a program that display the average of each row of a 2-dimensional jagged array of
int
jArray
. -
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
.
- Write a program that display “pair found” if
first
andsecond
occur next to each other in the same row. - Edit your program so that “pair found” is displayed also if
second
occurs beforefirst
in the same row. - 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), - Edit your program so that “pair found” is displayed also if
second
occurs “above”first
, - Edit your program so that “pair found” is displayed also if
first
andsecond
occur diagonally, - Edit your program so that “pair found” is displayed also if
first
andfirst
occur anti-diagonally.
Test your program thoroughly, possibly bundling it in a static
class
to ease testing and debugging.