Code:#include <iostream> #include <iomanip> using namespace std; int main() { //declare variables short numberOfTests = 0; short testScores = 0; short scoreGrade = 0; short sum = 0; short testNumber = 1; double averageScore = 0.0; do //begin loop { cout << "Enter the number of tests: "; cin >> numberOfTests; if (numberOfTests >= 1 && numberOfTests <= 100) cout << "You entered " << numberOfTests << " as the number of tests. " << endl; else cout << "The number must be between 1 and 100 (inclusive). "; //end if } while (numberOfTests < 1 || numberOfTests > 100); testScores = numberOfTests; while (numberOfTests > 0) { cout << "Enter the score for test #" << testNumber << ": " << endl; cin >> scoreGrade; if (scoreGrade >= 0 && scoreGrade <= 100) { //update accumulator and counter sum += scoreGrade; testNumber += 1; numberOfTests -= 1; } cout << "You must enter a valid number between 0 and 100 (inclusive) as a score. "; } //end while averageScore = sum / testScores; cout << "There were a total of " << testScores << " test scores. " << endl; cout << "The average of all the test scores is: " << averageScore << endl; cin.get(); return 0; } //end of main function




Reply With Quote