Assignment #3A Solution



Assignment 3A include a programming portion and a written portion. The programming portion must compile and consist of a single file ( hw03A.cpp), and the written portion should consist of a single file (hw03Awritten) in a .pdf format. Be sure to include your name at the beginning of each file! You must hand in both files via NYU Classes.

Programming Part:

1. Add the method erase( Vector<Object>::iterator vItr) to the Vector class. The sig- nature of your method should be:

iterator erase( iterator vItr)

2. Write agenericfunction templatecalledprint if that:

• takes three parameters: two iterators start, end, and a functor pred

start and end have the capabilities of a forward iterator, and refer to a range

[start,end) in a container

pred is a functor that takes an element in the range [start,end) as an arguement and returns a bool

• prints1 all items in the range [start,end) which evaluates to true

• runs in O(n) time where n is the number of items in the range [start,end)

The signature of your generic function template is:

template< class Itr, class UnaryPred >

void print_if( Itr start, Itr end, UnaryPred pred )

3. Create afunctor calledGPA in range where:

• the class has two private member variables of type double: low and high

• the constructor has two parameters of type double: l and h which it uses to initialize the private member variables low and high

• the overloaded operator() takes a single argument of type student and returns true if the student’s gpa is in the range [low, high]. Otherwise the overloaded operator() returns false

4. Test your answer to programming questions 3 and 2 by creating a vector of type student. Use your code from questions 3 and 2 to print out all the students in your vector whose GPA is within [3, 4]. Turn in your testing code.

Written Part

1. For the vector class, and for the following code2 snippet:

vector<int> c { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; vector<int>::iterator itr1 = c.begin()+2; vector<int>::iterator itr2 = c.begin()+4; vector<int>::iterator itr3 = c.begin()+8;

cout << *(c.begin( ) + ( c.end( ) c.begin( ) )/2 ) << endl;


cout << *itr1 << endl; cout << *itr2 << endl; cout << *itr3 << endl;

cout << *(c.begin( ) + ( c.end( ) c.begin( ) )/2 ) << endl;

What is printed? Explain your answer.3

2. In written question 1, which of the iterators were valid after the erase method was called.

Wewillsayaniteratorisstill“valid” ifitreferstothe sameitem asbeforethe method was called.

3. Using big-Oh notation, give the worst case run time for the method erase, which you imple- mented programming problem 1.

4. Finish writing the following function that subtracts one from every item in the range [start,end).

template< class Itr >

void subtractOne( Itr start, Itr end )


// Fill in the code here


Forexample, thefollowingcodesnippet:

vector< int > a = { 144, 524, 230, 8 };

subtractOne( a.begin( ), a.end( ) );

results inthevector a now containing 143, 523, 229, 7.

5. For the Vector class we discussed in class, if we removed the word explicit in front of the constructor, i.e.

template <class Object>

class Vector



Vector( int initSize = 0 ) :

theSize( initSize ), theCapacity( initSize + SPARE_CAPACITY )

{ objects = new Object[ theCapacity ]; }

// rest of class the same as before


What would be printed by the following code snippet?

Vector<int> v(3);

v = 110;

cout << v.capacity();