INDIVIDUAL WORK
QUESTION #1 (SELF-CHECK)
Consider
the following statement that creates an array:
int[] arr = {2, 9, 0, 3, 9, 7, 2, 3};
Answer the
following questions:
a) What is the length of arr?
b) What is the index of the first
occurrence of the value 9 in arr?
c) What is the index of the last
occurrence of the value 3 in arr?
d) What is the index of the first
occurrence of the value 0 in arr?
How is that different to the index of the last occurrence of 0?
e) What is the maximum value in the
array?
f) What is the index of the maximum
value in the array? If there is more
than one, then we want the first occurrence of that value.
SOLUTIONS
Check your
answers here.
QUESTION #2
(SELF-CHECK)
Attempt to
solve as many of the following as you can.
a) Create a function named count that gets an int
array and an integer as parameters. It returns the number of occurrences of
that integer in the array. Test your
function.
b) Create a function named contains
that gets an int array and an int value
as parameters. It returns true
if there is at least one occurrence of value in the array. Otherwise, it returns false.
c) Create a function named findLast
that gets an int array and an int
value as parameters. It returns the
index of the last occurrence of value in the array. If there is no occurrence of value, then
-1 should be returned.
d) Create a function named max
that gets an int array as parameter and returns the
maximum value in the array.
SOLUTIONS
Check your
answers here.
QUESTION #3
Create a
function named minIndex that gets an int array as parameter and returns the
index of the minimum value in the array.
Test your function.
QUESTION #4
Create a
function named check45 that gets an int array that contains nine elements
in it.
Each
element represents a cell in a Sudoku row.
Since every number from 1 to 9 is required exactly once in a Sudoku
row, one check we can do is see if all numbers add up to 45 (because 1 + 2
+ 3 + … + 9).
This
function simply checks if the numbers add up to 45. If it does, then it returns true. Otherwise, it returns false. Test your function.
|