Exercise 2.4

Introduce a static local vector to hold the elements of your Pentagonal series. This function returns a const pointer to the vector. It accepts a position by which to grow the vector if the vector is not that size as yet. Implement a second function that, given a position, returns the element at that position. Write a main() function to exercise these functions.

 #include <vector> #include <iostream> using namespace std; inline bool check_validity( int pos ) { return ( pos <= 0 || pos > 64 ) ? false : true; } bool pentagonal_elem( int pos, int &elem ) { if ( ! check_validity( pos )){ cout << "Sorry. Invalid position: " << pos << endl; elem = 0; return false; } const vector<int> *pent = pentagonal_series( pos ); elem = (*pent)[pos-1]; ...

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.