Lab 6: CPS100e: Sequences and Iterators

Change into your cps100e directory and create a lab6 subdirectory. Then change into this and copy files by typing what's below (don't forget the trailing dot).

      cp ~ola/cps100e/lab6/* .

If you type "ls" you should see the following files: Makefile, testseq.cc, sequence.cc, seqiterator.cc, data, data.large, sequence.h, and seqiterator.h.

Introduction

In this lab you'll use two classes that interact with each other, DigitSeq and DigitSeqIterator. The main function for this assignment is a tester for the functions you will write.

The class DigitSeq represents a sequence of digits. You can create a sequence by inserting digits into the sequence at the beginning of the sequence (using Prepend) or at the end of the sequence (using Append).

The class DigitSeqIterator is bound to a particular sequence and allows one to iterate over the sequence via the functions First() and Next().

DO NOT USE VECTORS OR ARRAYS IN THIS ASSIGNMENT!

Part 1 - Print

Compile and run the program testseq. It doesn't do much right now as this program has code missing for five of its functions. You should first implement the function Print to list out a sequence with a specified number of digits per line.

To print the sequence, you'll need to use a DigitSeqIterator to move through the sequence. Declare such an iterator and then use the function First and Next to move over the digits.

Shown below is an example of printing a sequence with 5 digits per line.

> testseq
Enter filename: data

Sequence is:
3 5 4 6 3
7

Compile and run your program before moving on to the next part. Also test your program on the data.large file.

Part 2 - Creating a large Random Sequence

Complete the function RandSeq to create a random digit sequence of a specified size. Each digit should be from 0 to 9 and can be generated using a dice.

For example, calling RandSeq(random, 20) followed by Print(random,5) might result in:

9 2 3 0 3
1 6 4 2 5
7 8 9 0 5
3 1 6 8 8 

Compile and run your program before moving on to the next part.

Part 3 - Number of Occurences

Complete the function NumOccur to return the number of times a particular digit appears in a sequence. Do not use an Array or Vector for any of this assignment. Instead use a DigitSeqIterator to move through the digits in the sequence.

When this function is applied to the sequence example in Part 1 for each of the digits 0-9, the number of occurences are shown below.

k   Occurrences
0   0
1   0
2   0
3   2
4   1
5   1
6   1
7   1
8   0
9   0

Compile and run your program before moving on to the next part.

Part 4 - Digit Occuring Most Often

Complete the function MostOften to return the digit that appears most often in the sequence. In the example in Part 1, the digit appearing most often is 3.

Compile and run your program before moving on to the next part.

Part 5 - Expand

Expand creates a new sequence that is an expanded version of another sequence. Each digit N from the old sequence appears N consecutive times in the new sequence. The digit 0 should disappear, occuring 0 times. If there are two consecutive 3's they would each appear 3 times.

Below is the sequence from Part 1 expanded, and printed 10 digits per line.

Sequence is:
3 3 3 5 5 5 5 5 4 4
4 4 6 6 6 6 6 6 3 3
3 7 7 7 7 7 7 7

Submission

To turn in programs: When your modified program works, you can submit it, along with a README describing how long it took you and what your impressions of the lab are. Type (where N is your section number: 1, 2, or 3):

      submit100e lab6secN README testseq.cc

The README file should include your name, how long you worked on the program, and anyone you received significant help from. You should also include any comments you have about the lab.

You should receive a message telling you that the programs were submitted correctly.