//============================================================================= // VectorIter.cpp // Written by: Unknown, // // Last Modified: Fall 2020 // // Description: This sample demonstrates how to use STL's vector container // to store a sequence of integer values. // // You may also find the use of std::vector::iterator // // Also note the use of typedef vector< int > myVectorType; // This tends to save typing and may allow // type changes to be isolated to one line of code change. // // // Tasks: find the TODOs and do them // // //============================================================================= #include #include using namespace std; // ------------------------------------------------------------------------ // TODO: // First create a new type, which represents a vector container of ints // This reduces code clutter and helps in maintaining code // ------------------------------------------------------------------------ typedef ?????? myVectorType; // ------------------------------------------------------------------------ // ProtoTypes of global functions // ------------------------------------------------------------------------ // TODO: create a prototype declaration of printContents as defined (far) below ?????????? void ContinueAsFibonacci(myVectorType *vFibPtr, int N); // ------------------------------------------------------------------------ // main function - it all begins here // ------------------------------------------------------------------------ int main( void ) { //--------------------------------------------------------------------- // TODO: Create the initial vector, myFib ??????? // TODO: Initialize the first 2 numbers in our vector to be 0 and 1 // Use the vector function push_back ????? ????? // TODO: // Call the ContinueAsFibonacci function to push the next 4 // numbers of the sequence onto myFib... // Notice we must pass a pointer to myFib... so set that up too ????? ContinueAsFibonacci(myFibPtr, 4); // Print out the contents for inspection printContents( myFib ); // TODO: // Repeat the above so you get the next 5 numbers in the sequence ?????? printContents( myFib ); return 0; // zero indicates program success to the operating system } // end main() //------------------------------------------------------------------------- // printContents //------------------------------------------------------------------------- void printContents( myVectorType v ) { //--------------------------------------------------------------------- // Using iterators, // which point to the beginning and ending of the vector, // loop through the vector and print out its contents // TODO: use the vector functions begin and end to initialize the below correctly myVectorType::iterator it_cur = ????? myVectorType::iterator it_end = ????? cout << "Contents of the vector: \n "; // TODO: loop until it_cur equals it_end while ( ?????? ) { cout << *it_cur << " " ; // TODO: increment the it_cur iterator ????? } cout << endl; } //------------------------------------------------------------------------- // ContinueAsFibonacci // // Assume the last 2 values of vFibPtr properly start the Fibonacci sequence // at the place the user wants to begin // So we really only care about the last 2 elements in the vector // // Using iterators such as those returned by begin and end, // and the vector function push_back // push the next N numbers in // the Fibonacci sequence into vector vFib // // TODO: implement this function // //------------------------------------------------------------------------- void ContinueAsFibonacci(myVectorType *vFibPtr, int N) { // TODO: // Do some simple error checks ????? { return; } // if vFibPtr is null return ????? { return; } // if n is less than or equal 0 return ????? { return; } // if vFibPtr's size is < 2 return // TODO: loop from 0 to N-1 ????? { int value = 0; myVectorType::iterator it_end = vFibPtr->end(); // this is 1 past last value // TODO: properly adjust it_end, and use it to calculate // the next Fibonacci number... store that in the variable named value ????? value = ????? ????? value += ????? // TODO: Using vector function push_back // push value onto the vector pointed to by vFibPtr ?????? } } //------------------------------------------------------------------------- //------------------------------------------------------------------------- // end file