Java

TOPIC 45 – PROCESSING/SEARCHING ARRAYS

 

LESSON WORK

 

 

GROUP WORK

 

QUESTION #1

Create a function named output that gets an int array as parameter and outputs each element to screen.

 

Follow helpful these steps:

  1. Create the function prototype (header).
  2. Think about the data that will be returned.  Will it be a value from the array?  Will it be an index?  Will it be a calculated value?  Might you need an escape value such as -1?
  3. Do you need to loop over the entire array?  Can you only loop until you find what you are looking for?
  4. Write down the template to loop over the array. 
  5. Add the details to complete the function.

 

 

QUESTION #2

Create a function named total that gets a double array and returns the total of all element values.

 

 

QUESTION #3

Create a function named findFirst that gets an int array and an integer as parameters and returns the index of the first occurrence of the integer in the array. If the integer value is not in the array, -1 should be returned.

 

 

 

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.