IC210 Lab 7

Pre-lab homework. Read the whole lab, then turn in a flowchart (either neatly handdrawn or using the RAPTOR tool) for each of:

Your flowcharts are to focus on the conceptually difficult parts of the programís control flow logic. Reminder: All pre-lab homework is due at the beginning of the lab period, and late pre-lab homework will earn a grade of 0.

Functions and Recursion

This lab will consist of several independent problems. Show each solution to the instructor when you complete it.

  1. Suppose we want programs that are able to input and output time in hh:mm:ss format. Our programs will probably calculate things in seconds, but the user would prefer to see something like 02:00:23 (i.e. 2 hours, 0 minutes and 23 seconds) instead of 7223 seconds. Write a function writeTime that prints out a given number of seconds in the proper format to either the screen or a file. The following code fragments give examples of how I'd like to use it:

    Example Code FragmentWrites
    int s = 7223;
    02:00:23 to the screen.
    int s = 7223;
    ofstream fout("test.txt");
    02:00:23 to test.txt

    Test your function with a program that reads in a number of seconds from the user and either the word "screen" or a file name, and prints the time in hh:mm:ss format to either the screen or a file with the given name.

  2. As this class progresses, we'll be doing a lot of computing with "points", i.e. with xy values. We'd like to read these from the user or from files in the usual ordered pair notation, i.e. (x,y). It sure would be nice to have a function readPoint that would do it for us. We should be able to use readPoint in the following ways:

    Example Code FragmentInputOutput
    double x=0, y=0;
    cout << "Enter point: ";
    cout << "x = " << x << ", ";
    cout << "y = " << y << endl;
    (3.5,-0.1) from user x = 3.5, y = -0.1
    double x=0, y=0;
    ifstream fin("data.txt");
    cout << "x = " << x << ", ";
    cout << "y = " << y << endl;
    (0.5,-4.4) from data.txt x = 0.5, y = -4.4

    Notice how readPoint modifies the two double values passed to it. Define function readPoint and test it thoroughly.

  3. Write a recursive function, printbin(n) that prints the binary representation of n. You may assume that n >= 0. A sample run of your program would be as follows:
    Enter non-negative integer: 37
    In binary that's 100101

  4. Write a recursive function writereverse(istream& IN,ostream& OUT) that reads in strings from the input stream IN (i.e. either typed by the user or from a file) terminated with the word "end" and prints the strings to the stream OUT out in reverse order. For example, if the call from main looks like writereverse(cin,cout) and the user types
    I am sam end
    then writereverse should print out
    sam am I


If you finish the above during lab time, try to solve the following:

  1. An arithmetic sequence is a sequence of the form:
    a, a + b, a + 2b, a + 3b, ...
    Write a program that reads integers a and b from the user along with positive integer n, and prints out the first n terms of the arithmetic sequence defined by a and b, followed by "...". The trick is you may not use loops! Thus, you need to use recursion. Here is a sample run (user input in red):
    Enter a, b and n separated by spaces: 2 5 10
    2, 7, 12, 17, 22, 27, 32, 37, 42, 47, ...
  2. Challenge Problem
    Write a program that reads a positive integer n from the user and prints out all n-digit binary numbers, one per line. Hint 1: write a function with prototype listbins(string front, int n); which, assuming that n is not negative, will print out all the different strings you can get by tacking an n-bit binary number onto the back of the string front. So, for example, listbins("hello",2) should produce the following output:

    If you get listbins working you're done, since listbins("",n) prints out all n-bit binary numbers. This illustrates an important technique. Your first thought is probably "I'd like a function listbins(int n); that prints out all n-bit binary numbers." However, we need to throw in an extra argument which, from the perspective of a "user" of the function is unnecessary, but which from the implementer's perspective we need so we can pass partial results along to the next recursive call. In essence, the more general, flexible and powerful function listbins(front,n) is easier to write than the more specific function listbins(n).

    Hint 2: Remember that + concatenates strings or strings and characters. So if s = "do" and t = "ne" then s + t is the string "done", and s + 't' is the string "dot".