Lab 2 - The Sequence Class


Due Date: Midnight Sunday June 13

  1. Create a lab2 subdirectory in your 2440 directory. Put all of your files for this lab in that subdirectory.
  2. Copy the files sequence1.h, sequence1.o, sequence_test.C, and sequence_exam.C from the directory
        cp /usr/local/apache2/htdocs/u/csgs/sab/2440/Assignments/labs_S_04/lab2/*.[Coh] .
    
    into your lab2 subdirectory.
  3. Compile the sequence_exam program using the command
        % g++ -Wall sequence_exam.C sequence1.o -o sequence_exam
    
    Run the program to see that it passes all of the tests.
  4. Compile the sequence_test program using the command
        % g++ -Wall sequence_test.C sequence1.o -o sequence_test
    
    Run this program to see how the various sequence member functions behave.
  5. Write a program that asks the user to input a list of integers. Store each number in numerical order in a sequence object. Read integers until end-of-file (see hint below). Store only one copy of each number, i.e., if a number is already in the sequence, do not insert it again. The program should then output the sequence of integers which means the numbers will be output in sorted order if you built the sequence correctly.

    Here is a sample run of the program. The user's input is shown in bold.

    Input a list of integers:
    
    112
    67 
    181920
    67 
    -1 
    89 
    1617 
    23 
    89 
    415
    131
    89 
    101
    45 
    45 
    
    
    -1 23 45 67 89 101 112 131 415 1617 181920
    
    For extra credit you may do the following option. If you do not want to do the extra credit, start on the implementation of the sequence class, by copying sequence1.h to sequence1.C and making stubs for each of the member functions, like you did for the statistician class.

Extra Credit Option

For each number in the list keep a count of how many times that number appears in the list. Alter the output to show each number on a line by itself with the count beside it.

Hints/Reminders

  1. Before storing the numbers in sorted order, write an easier version of the program that just reads in the numbers, storing them into a sequence and then printing them out. Once this works, go back and figure out how to store the numbers in order.
  2. The simplest way to read in a list of integers until the end of file is with a loop similar to the following:
        1 int num;
        2 
        3 while (cin >> num)
        4 {
        5     // Process the number
        6 }
    
    In a Unix environment, end-of-file is typed from the keyboard as CTRL-D.
  3. You can use the start, is_item, advance, and current sequence member functions to loop through the items in a sequence.
         1 sequence list;
         2 
         3 // code to build list
         4 
         5 // loop through items in the sequence - each time through
         6 // the loop list.current gives the next item in the sequence
         7 
         8 for (list.start(); list.is_item(); list.advance())
         9 {
        10     // access current item with list.current()
        11 }
    
  4. Instead of having to type in a list of numbers every time you can redirect the input to come from a file. For example, suppose you put some numbers into a file named nums and your program is called order. You can make your program read from the file instead of from the keyboard using the following command:
        % order < nums
    
    This will make any cin statement read from the file instead of the keyboard.

Click here for the post-lab assignment.