# Exercise 8: Loops and Arrays

CS 116

Nov. 4, 2002

Try this:

1. Take a look at the file `Loop1.java` from the web page. Try it out. Be sure you try entering both positive and negative values for the loop termination.

2. Change the code so that the applet counts down from the entered value to 0.

3. Change the code so that it behaves the same way but uses a `for` loop rather than a `while` loop.

4. Here is an interesting mathematical function: suppose you have a value `x`. If `x` is even, the function returns `x/2`. If `x` is odd, the function returns `3x+1`. If we iteratively apply this function to numbers larger than 1, mathematicians believe the sequence of values eventually returns to 1. Write a little program (you can use `Loop1.java` as a starting point) in which the user enters a starting number and then we loop and print out the values as long as `x` is not equal to 1.

5. Take a look at the file `Array1.java` from the web page. Try it out. Be sure to try search for values that are in the array and values that are not.

6. Change the code so the the applet prints ALL the locations that the target value is found. You can print to the console if you wish.

7. Change the code so that you add a second array with indices from 0 to 9. Use that second array to count the frequency of the values in the original array. For example, the value 3 appears twice in the original array so your array at index 3 would have the value 2.

Print the frequencies after they are all computed to the console.

Thomas P. Kelliher
Wed Oct 30 16:59:36 EST 2002
Tom Kelliher