Assignment No.9

  1. (Adapted from Textbook Chapter 7 Exercise 1) Write a method called lastIndexOf that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. The method shold return -1 if the value is not found. For example, in the array [74, 85, 102, 99, 101, 85, 56], the last index of the value 85 is 5.
    Hint: The method is supposes to return the position of the last occurrence of the number with that position initialized to -1. Then it goes through the array using a for-loop and then whenever the target value is found, update the position it remembers. This way, the position will be updated to the last occurrence.

  2. (Adapted from Textbook Chapter 7 Exercise 2) Write a method called range that returns the range of values in an array of integers. The range is defined as 1 more than the difference between the maximum and minimum values in the array. For example, if an array called llist contains the values [36, 12, 25, 19, 46, 31, 22] the call of range(llist) should return 35 (46 - 12 + 1). You may assume that the array has at least one element.

Go back to the assignment list page.