# C++ Primer Plus Chapter 7 Exercise 2

Exercise 2 is a good lesson in functions. You will be forced to use a function for every requirement of the program. In addition, you will have to be inventive with how you terminate the program early and still calculate a running average. See my source below:
2. Write a program that asks the user to enter up to 10 golf scores, which are to be stored
in an array. You should provide a means for the user to terminate input prior to entering
10 scores. The program should display all the scores on one line and report the average
score. Handle input, display, and the average calculation with three separate array-
processing functions.

```#include <iostream>

const int maxSize = 10;

// Prototypes
int input(int scores[], int arSize);
double avg(int scores[], int arSize);
void display(int scores[], int arSize);

int main()
{
int scores[maxSize];
int arSize = input(scores, maxSize);
display(scores, arSize);
std::cout << "Average score: " << avg(scores, arSize);
return 0;
}

int input(int scores[], int arSize)
{
double score;
int count = 0;
std::cout << "Enter up to " << maxSize << " scores (press -1 to terminate)\n"; // Loop up to 10 times
for(int i = 0; i < maxSize; i++) // Loop until maxSize
{
std::cout << "Enter Score #" << (i + 1) << ": ";
std::cin >> score;
if(score > 0)
{
scores[i] = score; // assign score to score[i]
count++;
}
else
break;

}
return count; // return something sice fucntion is not void.

}

double avg(int scores[], int arSize)
{
double total = 0;
for(int i = 0; i < arSize; i++) // Loop as many times as was actually inputted
{
total += scores[i];
}
}

void display(int scores[], int arSize)
{
std::cout << "Scores: ";
for(int i = 0; i < arSize; i++) // Loop as many times as was actually inputted
{
std::cout << scores[i] << " "; // Print to one line
}
}
```

# C++ Primer Plus Chapter 7 Exercise 1

Chapter 7 kicks off with a fairly painless reintroduction into functions. We computer harmonic mean for a number pair. There are probably a few ways to do this, but they should all be very similar. Take in two numbers, store them if you like as I did, check if they are zero’s, and if not compute the harmonic mean by way of a function. Check out my source below:

1. Write a program that repeatedly asks the user to enter pairs of numbers until at least one
of the pair is 0. For each pair, the program should use a function to calculate the har-
monic mean of the numbers. The function should return the answer to main(), which
should report the result. The harmonic mean of the numbers is the inverse of the aver-
age of the inverses and can be calculated as follows:
harmonic mean = 2.0 × x × y / (x + y)

```#include <iostream>

using namespace std;

float Hmean(int x, int y);

int main()
{
int myArray[2];

cout << "Please enter pairs of numbers: " << endl;
cin >> myArray[0] >> myArray[1];

if(myArray[0] == 0 || myArray[1] == 0)
{
cout << "You have entered a pair matching zero" << endl;
cout << "Exiting..." << endl;
}
else
{
cout << "The Harmonic mean of " << myArray[0] << " and "
<< myArray[1] << " is " << Hmean(myArray[0],myArray[1]);
}
return 0;
}

float Hmean(int x, int y)
{
float calc = 2.0 * x * y / (x + y);

return calc;
}
```