// FILE: 95a4.cpp // PURPOSE: Test implementation of solutions to 1995 Exam A, Question 4 // OTHER FILES: apvector.h, apvector.cpp //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: All the files must be in directory in which you compile // 2. COMMAND LINE: g++ (or gxx) 95a4.cpp /*-------------------------------------------------------------------- Consider an abstract data type that represents a finite, nonempty sequence of integers. Each such sequence has a designated "current position." These sequences are represented by variables of type Sequence. For example, variable S of type Sequence, might represent the sequence. 3, 26, 18, 11 18 -- where the underline indicates that the current position is the second position and that the integer 26 is at that position. Sequences may have an "invalid" current position. A sequence with an invalid current position is said to be "ended." Assume that the following public member functions have been defined for the class Sequence. More precise specifications are given before part (a). IsEnded returns true if the current position is invalid; otherwise, returns false. SetToFirst sets the current position to be the first position CurrentValue returns the integer value at the current position. It is an error to make this call when the sequence is ended. Advance It is an error to call Advance when the sequence is ended. If the current position is the last position, then the current position is changed to be invalid Otherwise, the current position is changed to the position following the one that was current when Advance was called. The following example demonstrates the use of some of these operations in the definition of a function SumSeq that returns the sum of the integers in sequence S. int SumSeq(Sequence s) { // postcondition: returns sum of all entries in s int sum = 0; s.SetToFirst(); while (! s.IsEnded()) { sum += s.CurrentValue(); s.Advance(); } return sum; } part A Write the body of function Length whose header is given below. Length returns the number of integers in its sequence parameter. Note that Length is NOT a member function. int Length(Sequence s) // precondition: s contains m elements // postcondition: returns m part B Write the function LenOfIncreasing, whose header is given below. LenOfIncreasing returns the length of the longest (strictly) increasing segment in s beginning with, and including, the integer at its current position. Examples: (the current position is indicated with an *) Sequence s Increasing Segment LenOfIncreasing(s) 3 6 11 6 5* 6 8 9 11 5 3 5 6 8 9 11 5 7 -1 2 5 8 6 6 7 7* 30 7 30 2 1 7 6 3 5* 9 9 9 10 10 4 5 9 2 3 5 8 9 12* 4 6 9 15 12 1 Complete function LenOfIncreasing below the following header. int LenOfIncreasing(Sequence s) // precondition: s represents n_1, n_2, ... , n_m; // current position of s is c; 1 <= c <= m // postcondition: returns the largest integer r between 1 and // m (inclusive) such that n_c < n_{c+1} < ... < n_{c+r-1} ----------------------------------------------------------*/ #include #include #include #include "apvector.h" template class apvector; class Sequence { public: Sequence(); Sequence(int a[], int aLen, int curPos); bool IsEnded(); // precondition: current position is c // postcondition: returns true if c is invalid; // otherwise, returns false int CurrentValue(); // precondition: current position is c, which is valid // postcondition: returns value at position c void Advance(); // precondition: current position is c, sequence has m elements // postcondition: if 1 <= c < m, then current position = c+1 // if c == m, then current position is invalid void SetToFirst(); // postcondition: current position is first position void print(); private: int privCurPos; apvector privSequence; }; //----------------Part A--------------------------- int length(Sequence s) { // may only use IsEnded, SetToFirst, CurrentValue, and Advance return -77; }//*** length //-------------Part B----------------------------------- int LenOfIncreasing(Sequence s) { // may only use IsEnded, SetToFirst, CurrentValue, and Advance return -66; }//*** LenOfIncreasing int a1[] = {3, 6, 11, 6, 5, 6, 8, 9, 11, 5, 3}; Sequence s1(a1, sizeof(a1)/sizeof(int), 5); int a2[] = {7, -1, 2, 5, 8, 6, 6, 7, 7, 30}; Sequence s2(a2, sizeof(a2)/sizeof(int), 9); int a3[] = {1, 7, 6, 3, 5, 9, 9, 9, 10, 10, 4}; Sequence s3(a3, sizeof(a3)/sizeof(int), 5); int a4[] = {3, 5, 8, 9, 12, 4, 6, 9, 15}; Sequence s4(a4, sizeof(a4)/sizeof(int), 5); void Driver(void); void cls(); int main() { string title("1995 Exam A: Question 4"); cls(); cout << setw(39 + title.length()/2) << title.c_str() << "\n\n"; Driver(); cout << "Press ..."; cin.get(); cout << endl << endl; return 0; } void Driver(void) { s1.print(); cout << "length(s1) = " << length(s1) << endl << "LenOfIncreasing(s1) = " << LenOfIncreasing(s1) << "\n\n"; s2.print(); cout << "length(s2) = " << length(s2) << endl << "LenOfIncreasing(s2) = " << LenOfIncreasing(s2) << "\n\n"; s3.print(); cout << "length(s3) = " << length(s3) << endl << "LenOfIncreasing(s3) = " << LenOfIncreasing(s3) << "\n\n"; s4.print(); cout << "length(s4) = " << length(s4) << endl << "LenOfIncreasing(s4) = " << LenOfIncreasing(s4) << "\n\n"; }// Driver Sequence::Sequence() { SetToFirst(); } Sequence::Sequence(int a[], int aLen, int curPos): privCurPos(curPos) { privSequence.resize(aLen); for (int c = 0; c < aLen; c++) privSequence[c] = a[c]; } bool Sequence::IsEnded() { return (privCurPos > (privSequence.length()) ); } int Sequence::CurrentValue() { assert( !IsEnded() ); return privSequence[privCurPos -1]; } void Sequence::Advance() { assert( !IsEnded() ); privCurPos += 1; } void Sequence::SetToFirst() { privCurPos = 1; } void Sequence::print(void) { cout << "Sequence: "; if ( privSequence.length() == 0 ) cout << "Empty list.\n"; else { for (int c = 0; c < privSequence.length(); c++) { cout << privSequence[c]; cout << ((c + 1 == privCurPos)? "* " : " "); } } cout << endl; }// print void cls(void) { for (int k = 1; k <= 55; k++) cout << '\n'; }