// Attached: HW_2a
// ===========================================================
// File: HW_2a
// ===========================================================
// Programmer: Elaine Torrez
// Class: CMPR 121
// ===========================================================
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
// Function prototypes
void getScores(double scores[], int size);
void showMenu();
char getChoice();
void displayResult(char choice, double scores[], int size);
void clearScreen();
int main()
{
const int SIZE = 5;
double testScores[SIZE];
char choice;
getScores(testScores, SIZE);
clearScreen();
showMenu();
choice = getChoice();
displayResult(choice, testScores, SIZE);
return 0;
}
// ===========================================================
void getScores(double scores[], int size)
{
cout << "Enter 5 test scores:\n";
for (int i = 0; i < size; i++)
{
cin >> scores[i];
}
}
// ===========================================================
void showMenu()
{
cout << "A.) Calculate the average of the test scores.\n";
cout << "B.) Display all test scores\n";
}
// ===========================================================
char getChoice()
{
char choice;
cout << "Enter your choice: ";
cin >> choice;
return choice;
}
// ===========================================================
void displayResult(char choice, double scores[], int size)
{
clearScreen();
switch (choice)
{
case 'A':
case 'a':
{
double sum = 0;
for (int i = 0; i < size; i++)
sum += scores[i];
double average = sum / size;
cout << fixed << setprecision(2);
cout << "The average is " << average << endl;
break;
}
case 'B':
case 'b':
{
cout << "Test scores:\n";
cout << fixed << setprecision(2);
for (int i = 0; i < size; i++)
cout << scores[i] << endl;
break;
}
default:
cout << "Invalid entry!" << endl;
}
cout << "Press any key to continue . . .";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();
}
// ===========================================================
void clearScreen()
{
system("cls"); // Windows
// system("clear"); // Mac/Linux
}