Assignment 10

due at midnight on   +40

The goal of this assignment is to practice simple algorithms on arrays. Start with the prototypes and the main function below.

const int SIZE = 10;

void print_array(int a[SIZE]);
int find_minimum(int a[SIZE]);
double compute_average(int a[SIZE]);
int how_many_evens(int a[SIZE]);

int main()
{
    int a[SIZE] = { 3, 5, 8, 2, 10, 2, 7, 6, 3, 18 };
    print_array(a);
    cout << "The minimum is " << find_minimum(a) << "\n";
    cout << "The average is " << compute_average(a) << "\n";
    cout << "There are " << how_many_evens(a)
         << " even numbers.\n";
    return 0;
}

Then you will write definitions for the four functions.

  • print_array shows every element in the array, 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 array. (It might be negative!) We did a version of this for maximum in class on 11/20 (arrays1.cpp), but you will have to move it to its own function.

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

  • how_many_evens counts the number of integers in the array 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 array (as well as the SIZE) to test other possibilities.

a[0] is 3
a[1] is 5
a[2] is 8
a[3] is 2
a[4] is 10
a[5] is 2
a[6] is 7
a[7] is 6
a[8] is 3
a[9] is 18
The minimum is 2
The average is 6.4
There are 6 even numbers.