Project 9

due at midnight on   +60

The goal of this assignment is to practice simple algorithms on vectors, and to implement them using functions. Start with the prototypes and the main function provided below.

#include <iostream>
#include <vector>
using namespace std;

void   print_vector    (vector<int> v);
int    find_minimum    (vector<int> v);
double compute_average (vector<int> v);
int    how_many_evens  (vector<int> v);

int main()
{
    vector<int> v;
    v.push_back( 3); v.push_back( 5); v.push_back( 8);
    v.push_back( 2); v.push_back(10); v.push_back(-3);
    v.push_back( 7); v.push_back( 6); v.push_back( 4);
    v.push_back( 5);            // TODO: better initializer?

    print_vector(v);
    cout << "The minimum is " << find_minimum(v) << "\n";
    cout << "The average is " << compute_average(v) << "\n";
    cout << "There are " << how_many_evens(v)
         << " even numbers.\n";
    return 0;
}

Then you will write definitions for the four functions.

  • print_vector shows every element in the vector, along with its index (as shown below). We already wrote this function in class on 11/20, so check the notes.

  • find_minimum will return the smallest integer in the vector. (It might be negative!)

  • compute_average adds up all the integers in the vector and divides by the size, then returns that as a double value.

  • how_many_evens counts the number of integers in the vector that are divisible by two.

The expected output in this program is as follows. You may want to change around the values initially in the vector to test other possibilities.

v[0] is 3
v[1] is 5
v[2] is 8
v[3] is 2
v[4] is 10
v[5] is -3
v[6] is 7
v[7] is 6
v[8] is 4
v[9] is 5
The minimum is -3
The average is 4.7
There are 5 even numbers.