CS1003/1004 Homework #4
Due by Tuesday, April 6, at 11:00am

There are two parts to this homework assignment: a theory portion, worth 15 points, and a programming portion, worth 10 points. Please be sure to review the submission instructions in advance, and make sure to include a README.

This version is revised as of 3/31. A clarification was made that two procedures can be defined for sorting.

Written (theory) questions

  1. (7 points) You're given an array of integers of length N. Devise an iterative algorithm to discover the most frequently-occurring integer in an array (that is, if the array contained 41, 3, 2, 47, 41, 3, 99, 5, 81, 3, 2, the answer would be 3, since it occurred 3 times), without sorting them. Statistically, this is called the mode of a set of numbers.
    1. (5 points) Express this algorithm as psuedocode, C, or Java. If there are multiple entries with the highest frequency, just pick one of them.
    2. (2 points) What's the running time of this algorithm, and (one sentence) why?
  2. (5 points) The Euclidean algorithm to find the GCD (Greatest Common Divisor) of two numbers (X and Y) is described as follows in Brookshear: As long as the value of neither X nor Y is zero, continue dividing the larger of the values by the smaller and assigning X and Y the values of the divisor and remainder, respectively. (The final value of X is the greatest common divisor.)

    Implement this as a procedure that uses a recursive algorithm in psuedocode, C or Java. Hint: In order for this implementation to be correct, it should not have any loop constructs in it.
  3. (3 points) I'm trying to write a program that takes a list of numbers in an array and slides them over by one so that I can insert a 0 at the front. It's OK with me if the last element is lost in the process. For example, I'd like to transform the array containing the numbers 5, 10, 33, 2, 4 into 0, 5, 10, 33, 2. Assume a is the array, and alength is the number of elements in the array. Here's my attempt at it.
    for(int i=0; i < alength-1; i++) { 
      a[i+1] = a[i]; 
    } 
    a[0] = 0;
    Without typing this code into a computer to check, will this algorithm work? Why or why not? If it doesn't work, can you describe what it does do, and rewrite it so that it works as desired?

Programming assignment: bank account manager modifications

In this portion, we're going to modify the bank account manager software that we wrote for HW#3. If you're not happy with the code you wrote for HW#3, you can obtain a copy of the reference code that Janak wrote; it will be made available on the class website on Thursday night (to account for late days). Alternatively, you can email Janak to request a copy if you already submitted HW#3 in its entirety.

Note that the following steps are a little less-well defined than the procedures in HW#3. This is deliberate; I'm giving you a little more flexibility to implement these as you see fit.

  1. (6 points) Implement a means to sort the list of accounts in the bank. Allow the user to sort either by name or by balance. In order to do this, write a new procedure or pair of procedures that sorts the names and balances arrays appropriately (either use bubble sort, as described in class, or insertion sort, as described in the book). Finally, add an option to the user interface to let the user sort the accounts as desired. Use the listAccounts procedure to test and make sure your sort works. (Hint: when sorting names, the strcmp function (C) or the .compareTo method (Java) will help you compare two strings and determine their lexicographical order.)
  2. (4 points) Modify the withdraw procedure so that, if there is a zero or negative balance after the withdrawal, the bank account is deleted. In order for this to work, you'll need to "slide" the remaining accounts upwards so that the accounts are still indexed by 0 through nAccounts-1.

(5 points extra credit) Instead of a linear search, have deposit and withdraw employ a binary search. You can implement this either recursively or iteratively; if you do the former, you may find it helpful to implement the binary search as a separate procedure. Make sure to have both deposit and withdraw presort the accounts by name so that the binary search works correctly. If you choose to do this, make sure to clearly state you've done so in your README.