My program, as it currently sits below, accomplishes everything that the problem asks of me:
(Write a program that has a function that prompts for three values. The function will then return the average of those values to main. The returned value will then be displayed to the screen).
Also, I wanted to make the program so that you could keep entering new values, which I did.
However, I know that my code is very explicit, and I am programming "with a hammer". My main function is crowded because I have repeated a block of code in order to get the program to be repeatable.
Can someone please lead me in the right direction with regard to making my code more efficient?
#include <iostream>
usingnamespace std;
double mean(double x, double y, double z);
int main()
{
double first, second, third;
char choice;
cout << " Please enter three different values: " << endl;
cin >> first >> second >> third;
cout << " The average of your three numbers is; \t" << mean(first, second, third) << endl;
cout << "\n Would you like to play again? " << endl;
cin >> choice;
cout << endl;
while (choice == 'y' || choice == 'Y')
{
cout << "\n Please enter three different values: " << endl;
cin >> first >> second >> third;
mean(first, second, third);
cout << " The average of your three numbers is; \t" << mean(first, second, third) << endl;
cout << "\n Would you like to play again? " << endl;
cin >> choice;
cout << endl;
}
return 0;
}
double mean(double x, double y, double z)
{
double average = (x + y + z) / 3.0;
return average;
}