//============================================================================= // 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 vector< int > myVectorType; // ------------------------------------------------------------------------ // ProtoTypes of global functions // ------------------------------------------------------------------------ // TODO: create a prototype declaration of printContents as defined (far) below void printContents( myVectorType v ); void ContinueAsFibonacci(myVectorType *vFibPtr, int N); // ------------------------------------------------------------------------ // main function - it all begins here // ------------------------------------------------------------------------ int main( void ) { //--------------------------------------------------------------------- // TODO: Create the initial vector myVectorType myFib; // TODO: Initialize the first 2 numbers in our vector to be 0 and 1 // Use the vector function push_back myFib.push_back( 0 ); myFib.push_back( 1 ); // 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 myVectorType* myFibPtr = &myFib; 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 ContinueAsFibonacci(myFibPtr, 5); 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 = v.begin(); myVectorType::iterator it_end = v.end(); cout << "Contents of the vector: \n "; // TODO: loop until it_cur equals it_end while (it_cur != it_end) { cout << *it_cur << " " ; // TODO: increment the it_cur iterator ++it_cur; } 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 if (vFibPtr == NULL) { return; } // if vFibPtr i null return if (N <= 0) { return; } // if n is less than or equal 0 return if (vFibPtr->size() < 2) { return; } // if vFibPtr's size is < 2 return // TODO: loop from 0 to N-1 for (int i=0; i < N; i++) { 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 it_end--; value = (*it_end); it_end--; value += (*it_end); // TODO: Using vector function push_back // push value onto the vector pointed to by vFibPtr vFibPtr->push_back(value); } } //------------------------------------------------------------------------- //------------------------------------------------------------------------- // end file