Extreme Programming

Largest Integer

Complete the function LargestInt, that reads a data file (whose name is passed in as a parameter) and returns the value of the largest integer in the file.  You can assume that the file contains only non-negative integer values separated by spaces.

You should make three data files to test your program (test1, test2, and test3). After writing, saving, compiling, and testing your function, answer the following questions (in your README file):

  1. What value did you use initialize the variable that keeps track of the current largest value in the file? Why?
  2. Would you change your value if you were guaranteed that there was always at least one number in the data file?  Why or why not?
  3. Would you change your value if you did not know the range of the numbers in the data file (i.e., if the data file contained a list of any integer value)?  Why or why not?

Alphabetically First Word

Complete the function SmallestWord, that reads a data file (whose name is passed in as a parameter) and returns the value of the alphabetically first word in the file.  You can assume that the file contains only alphabetic strings separated by spaces and that it contains at least one word.  Note that you can compare two strings alphabetically using the less than operator, "<".  In other words, one string is considered less than another one if it occurs before it in standard dictionary order.

You should make three data files to test your program (test4, test5, and test6). After writing, saving, compiling, and testing your function, answer the following questions (in your README file):

  1. What value did you use initialize the variable that keeps track of the current smallest value in the file? Why?
  2. Did it help to assume there was at least one data value in the file?  Why or why not?
  3. Is there a largest possible value a string can hold? If so, what is it and does it make sense to use such a value here? Why or why not?

The Hailstone Sequence

The hailstone sequence, sometimes called the 3n+1 sequence, is defined by a function f(n):
f(n) = n / 2     if n is even
     = 3n + 1    otherwise, if n is odd
We can use the value computed by f(n) as successive arguments of f(n) as shown below, the successive values of n form the hailstone sequence (It is called a hailstone sequence because the numbers go up and down mimicking the process that forms hail).
while (n != 1)
{
   n = f(n);
}
Although it is conjectured that this loop always terminates, no one has been able to prove it.  However, it has been verified by computer for an enormous range of numbers.  Several sequences are shown below with the initial value of n on the left.
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
8 4 2 1
9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Complete the function, Hailstone, that returns the number of steps in the hailstone sequence that starts with a given number (i.e., for the first three examples shown above, the function should return 17, 4, and 20, respectively)..

Longest Hailstone

Complete the function, LongestHailstone, that reads a data file of numbers and returns the one that yields the longest hailstone sequence.  You can assume that the file contains only positive integer values separated by spaces.  To do this, you should use the function you wrote in the previous section to determine how long each sequence is.

You should make three data files to test your program (test7, test8, and test9). After writing, saving, compiling, and testing your function, answer the following questions (in your README file):

  1. What value did you use initialize the variable that keeps track of the current longest sequence? Why?
  2. What makes this function different from the first two parts of the lab?
  3. Did you find this part harder than the earlier parts of the lab?  Why or why not?