Computer Science 3300
Fall 2005
Section 001
Programming Assignment 1

Assigned: Sep 6, 2005
Due: Sep 13, 2005, 11:59pm.

This is an exercise in using C++. The program should be short and easy. You can expect programming assignments for this course to be considerably more involved than this warmup exercise.


Preliminaries

Given any positive integer n, the hailstone sequence starting at n is obtained as follows. You write a sequence of numbers, one after another. Start by writing n. If n is even, then the next number is n/2. If n is odd, then the next number is 3n + 1. Continue in this way until you write the number 1.

For example, if you start at 7, then the next number is 22 (3 times 7 plus 1). The next number after 22 is 11. The hailstone sequence starting at 7 is (7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1), and it contains 17 numbers.


The assignment

Write a C++ program that reads a number n from the user (after giving a suitable prompt) and then tells the user two things: (1) the length of the hailstone sequence that starts with n, and (2) the largest number in the hailstone sequence that starts with n. The output needs to be sensible and easy to read, not just two numbers. For example, a session with this program might look like this. Parts in black are printed by the program. Parts in blue are typed by the user.

  What number shall I start with?  7
  The length of the hailstone sequence starting at 7 is 17.
  The largest number in the sequence is 52.


Testing your program

Compile your program and test it using g++ under Unix, as follows.

  1. Log into one of the computers in Austin 208. At the login prompt, select options/session, and select Gnome. Type your computer science id, then return, then your password. When you type your password, nothing will be shown in the window. Your password is being read, though.

  2. Create a terminal by right-clicking on the background, and selecting terminal

  3. You can see your files by typing command ls. At the end of each command, type the return key. Your files are the same as the My Documents folder in Windows, which called your home directory in Unix.

  4. Create a new folder (or directory) for your CSCI 3300 assignments by issuing command

        mkdir 3300
    Then change to that directory using command
        cd 3300
    To check your directory, type
        dirs
    Note that ~ stands for your home directory, and directories are separated by /, not \.

  5. Create your program using a text editor. You can get a simple text editor by selecting Applications at the top of the screen and selecting Accessories/Text Editor. When you save your file, be sure to save it in the 3300 directory.

  6. Suppose that your program is called assn1.cpp. Compile your program by running command

        g++ -Wall assn1.cpp
    If you get any errors, then modify your program using the text editor. If you just get another prompt, without any error messages, then your program contains no compile errors. That does not guarantee that it works, though.

  7. Once your program compiles correctly, try running it. First, list your files using ls. You should see one called a.out. That is the executable version of your program. (Unix does not use the .exe convention that Windows uses for executable programs.) Use command

        ./a.out
    to run your program. Does it work? If your program gets stuck in an infinite loop, type control-C (The control and C keys at the same time) to stop your program.

  8. I will give you instructions on how to turn in your program shortly.

  9. To log out, click on Actions at the top of the screen, and select logout.


Using a debugger

If your program loops forever, you will probably want to use a debugger to find out what is wrong. There is a debugger called gdb that works as follows.

  1. First, recompile your program using option -g, as follows.

        g++ -Wall -g assn1.cpp

  2. Now run a.out using gdb as follows.

        gdb ./a.out
    The debugger will greet you and give you a prompt. Type
        run
    to run your program.
  3. You can interrupt your program using a control-C. When you do, you return to the debugger. It will show you the line that the program is currently running. If you type

        bt
    it will show you the functions that are waiting, with the main program at the bottom. Each function called the one above it.

  4. You can print the value of a variable. For example, to find out what n is, type

        print n

  5. You can move to other functions. The functions in the stack are numbered from 0 when you do bt. To go to the next-higher-numbered function, use command

        up
    To go to the next-lower-numbered function, type
        down
    You can print the variables in the current function, whichever one you have selected.


Other debugging tips

A good way to debug a program is to turn on the lights. That means show things that the program normally would not show. For this program, a good idea is to show the numbers in the hailstone sequence as you compute them, to see that you are doing the right thing.

After your program is working, with the lights turned on, you will want to get rid of the debug prints, since they are not supposed to be there in the final product. Beginning students usualy just delete them. Unfortunately, if you encounter a problem later, and want them back, you need to type them in again. There is a way to keep them in the program, so that you can switch them on or off. Surround each debug print (or section of debugging code) as in the following example.

#    ifdef DEBUG
       printf("The next number is %i\n", n);
#    endif
Now, at the top of your program, write
#define DEBUG
to switch on debugging. Just comment this out to turn off debugging. For example, write
/*#define DEBUG*/
Then any parts between #ifdef DEBUG ... #endif will be ignored by the compiler. You may turn in a program with debug prints in it, as long as they are surrounded by #ifdef DEBUG ... #endif.

Important note. The # signs must be at the beginning of a line, and #ifdef DEBUG must be on its own line, as is #endif.


More documentation on Unix

You can find additional documention on using Unix here. The version of Unix that the computers in Austin 208 are running is called Solaris.


A word on some differences between Unix and Windows

Whether you log into Unix or Windows in the Austin computer labs, you will see the same files.

You cannot run Windows executable files in Unix, or Unix executable files in Windows. They are different machine languages and different file formats. So do not try to create a.out on Unix and then run it on a Windows machine.

Unix and Windows have different conventions for how to end a line. Unix uses one character, called the linefeed character (Ascii code 10, abbreviated LF). Windows uses a sequence of two characters, the carriage-return character (Ascii code 13, abbreviated CR) followed by the linefeed character. One effect of this difference is that, if you look at a Unix text file using Notepad on Windows, it will all be one line, with rectangles standing for the linefeed characters. Notepad is looking for the CR-LF combination to end a line. By the same token, if you look at a Windows text file in Unix, you might see extra CR characters, one at the end of each line.

A CR is also a control-M, and a LF is also control-J. So a CR might look like a control-M, often written ^M, when you see it.

Note. The emacs text editor, available for both Windows and Unix, can edit files in either format, regardless of the kind of machine that it runs on. You can edit Unix files using the Windows version of emacs, for example. Emacs is described in the Solaris documentation. It is available from www.gnu.org. (Gnu tools will work in Unix or Windows, not just in the Gnu operating system.)