122: CS2 --- Assignment 2

Due Monday, April 24

Problem Description: A sequence is an ordered-list of values. Thus we can talk about the i th element of a sequence, where i can vary between 1 and the number of elements in the sequence. Also, a value may occur more than once within a sequence. For example, [ 10 21 5 17 21 3 21 ] is a sequence of seven elements, where the second, fifth and seventh elements all have the same value of 21. For our purposes, the values within a sequence are constrained to be positive integers. A header file for the class Sequence has been defined for you. The header file for this class is sequence.h.

An example of a client program to use for testing is given here in hw02.cpp . For handing in purposes include (in addition to the implementation for the class Sequence described below), the client program Prog2.cpp. This program should utilizes the class RandomInt from Chapter 9 (you can download RandInt.h and RandInt.cpp right here) to generate a random sequence of integers of a given size from a given interval. The user should be prompted for the size of the sequence and its interval range. Your program will then compute the sequence with duplicate elements removed. You should output both the orginal sequence generated and the one with duplicates removed.

You are to provide the implementation code for the class Sequence. The class Sequence has a single constructor that acts as the default constructor. This constructor creates an empty sequence of size 0. In addition, the class Sequence has the following member functions that you will implement.

- void Set(int i, int v)
Sets the i th element of a sequence to v. For example, if T represents the sequence [6 21 ], then the invocation T.set(2, 9) would update T s representation to be [6 9 ]. Note, if there is no i th element, the program terminates.

- int Peek(int i)
Returns the value of the i th element of the sequence. For example, if T represents the sequence [6 21], then the invocation T.peek(1) returns the value 6. Note: if there is no i th element, the program terminates.

- int Size()
Returns the number of elements in the sequence. For example, if T represents the sequence [6 21], then the invocation T.size() returns 2.

- void push_back(int v)
Adds the value v to the end of the sequence. For example, if T represents the sequence [6 21], then the invocation T.push_back(54) would update T s representation to be [6 2154 ].

- void Erase(int i)
Removes the i th member of the sequence and move all members to the right of the i th member to the left one position, reducing the size of the sequence by one. For example, if T represents the sequence [6 2154 ], then the invocation T.erase(1) would update T s representation to be [21 54 ]. Note: if there is no i th element, the program terminates.

You are also to include the following auxilliary functions for operating on sequece objects.

- Overload the insertion operator for Sequence objects. Thus, if T is an object representing the sequence [ 6 21 54 ], the statement cout << T << endl; Would produce as its output. [ 6 21 54 ]

- void ExtractTo(Sequence &S)
Prompts the user for values to append to the sequence S. Function ExtractTo() should stop appending values once the user enters a 0 (the 0 should not be part of the sequence).

-int Find(const Sequence &S, int v)
Returns the position of the last occurrence of the value v in the sequence S. If S does not contain the value v, the function returns 0.

int Max(const Sequence &S)
Returns the value of the largest element in the sequence S. If S is an empty sequence, the function instead displays an error message and terminates the program immediately. The stdlib function exit() should prove useful here.

- void RemoveDuplicates(Sequence &S)
Duplicates of earlier occurring values are removed from the list. The leftmost (smallest valued index) occurrence of a repeated element should be the one left in the sequence. Thus, in the sequence [11 28 11 29 6 21 6 9], the third element should be removed as its value is a duplicate of the value of the first element. Similarly, the seventh element should be removed as its value is a duplicate of the value of the fifth element. Thus, the resulting sequence should be [11 28 29 6 21 9]. Hint: the following process is suggested for RemoveDuplicates()

 
for i in the interval 1 to S.size() do 
   determine the value v which is
    the i th element of S 
   determine the location j which is the last
     location in S that contains the value v 
   while j and i are different
       repeatedly erase the occurrence of v from S at position j 
       and then update j so that it is again the last location in S 
       that contains the value v 
end for
That's all folks.