Question

In: Computer Science

Consider the following program. // This program averages 3 test scores. It repeats as // many...

Consider the following program.

 
 
 
// This program averages 3 test scores. It repeats as
// many times as the user wishes.
#include <iostream>
using namespace std;

int main()
{
int score1, score2, score3; // Three scores
double average; // Average score
char again; // To hold Y or N input

do
{
// Get three scores.
cout << "Enter 3 scores and I will average them: ";
cin >> score1 >> score2 >> score3;

// Calculate and display the average.
average = (score1 + score2 + score3) / 3;
cout << "The average is " << average << ".\n";

// Does the user want to average another set?
cout << "Do you want to average another set? (Y/N) ";
cin >> again;
} while (again == 'Y' && again == 'y');
return 0;
}
 
 

The purpose of the program is to average three test scores and to keep prompting for them until the user does not enter Y or y at the bottom of the loop. Test the program thoroughly, and fix any unexpected behavior. Add a section of code that makes sure the user inputs a number between 0 and 100 for a test score and prompts the user for a new number if they enter a number outside the range of 0-100. For full credit you must do the following:

  • Fix the errors, add code to make sure that the input numbers are between 0 and 100 then submit the working code (only the .cpp file).
  • Add comments to the code to explain the purpose of any change(s).
  • Add you name, date, e-mail and a revision comment at the top of this code plus indicate what numbers you tested the program with as a comment at the end of the code.

Solutions

Expert Solution

Hi.

The code is modified as asked.

Please add your name,date,email as comment at the top of the program as asked in the question.

2 versions of the code have been added(one using functions and one without using functions).

Either of them can be used.

Thank you.

Code Version-1(using functions):



/*There is an error in the given code for exiting loop condition.I have mentioned the reason in the comment at the loop.I have also modified the code so that only valid inputs are taken from the user(0-100) */
#include <iostream>
using namespace std;


//This function makes sure that the user enters scores between 0-100 only
double takeInput(){

    double score; //to store score
    cin>>score;  //takes score from user
    while(!(score>=0 && score <=100)){              //check if the entered number is not between 1 and 100
        cout<<"Please enter score between 0 and 100"<<endl;    //asks user to enter a valid number
        cin>>score;   //takes score from user
    }
    return score;    //returns entered score
}

int main()
{
     double score1, score2, score3; // Three scores(modified from int to double as scores can be in decimals)
     double average; // Average score
     char again; // To hold Y or N input

     do
     {
       // Get three scores.
       cout << "Enter 3 scores and I will average them: ";
       score1=takeInput();          //calls takeInput function to take a valid score1
       score2=takeInput();          //calls takeInput function to take a valid score2
       score3=takeInput();          //calls takeInput function to take a valid score3

       // Calculate and display the average.
       average = (score1 + score2 + score3) / 3;
       cout << "The average is " << average << ".\n";

       // Does the user want to average another set?
       cout << "Do you want to average another set? (Y/N) ";
       cin >> again;
      } while (again == 'Y' || again == 'y');     //modified '&&' to '||' as we need to check if user enters 'Y' OR 'y' , but previously it checked if user entered 'Y' AND 'y' which is not possible
   return 0;
}

/*I have tested the code with the following values.
  score1=100
  The program asked me to enter a valid number between 0 and 100.So I entered
  score1=20
  
  score2=300
  The program asked me to enter a valid number between 0 and 100.So I entered
  score2=30
  
  score3=-200
  The program asked me to enter a valid number between 0 and 100.So I entered
  score3=40
  
  Then the program printed 'The average is 30'
  
 */

Code Version-2(without using functions):


/*There is an error in the given code for exiting loop condition.I have mentioned the reason in the comment at the loop.I have also modified the code so that only valid inputs are taken from the user(0-100) */
#include <iostream>
using namespace std;



int main()
{
     double score1, score2, score3; // Three scores(modified from int to double as scores can be in decimals)
     double average; // Average score
     char again; // To hold Y or N input

     do
     {
       // Get three scores.
       cout << "Enter 3 scores and I will average them: ";
       
       cin>>score1;  //takes score from user
       while(!(score1>=0 && score1 <=100)){              //check if the entered number is not between 1 and 100
        cout<<"Please enter score between 0 and 100"<<endl;    //asks user to enter a valid number
        cin>>score1;   //takes score from user
       }
       
       cin>>score2;  //takes score from user
       while(!(score2>=0 && score2 <=100)){              //check if the entered number is not between 1 and 100
        cout<<"Please enter score between 0 and 100"<<endl;    //asks user to enter a valid number
        cin>>score2;   //takes score from user
       }
       
       cin>>score3;  //takes score from user
       while(!(score3>=0 && score3 <=100)){              //check if the entered number is not between 1 and 100
        cout<<"Please enter score between 0 and 100"<<endl;    //asks user to enter a valid number
        cin>>score3;   //takes score from user
       }
       // Calculate and display the average.
       average = (score1 + score2 + score3) / 3;
       cout << "The average is " << average << ".\n";

       // Does the user want to average another set?
       cout << "Do you want to average another set? (Y/N) ";
       cin >> again;
      } while (again == 'Y' || again == 'y');     //modified '&&' to '||' as we need to check if user enters 'Y' OR 'y' , but previously it checked if user entered 'Y' AND 'y' which is not possible
   return 0;
}

/*I have tested the code with the following values.
  score1=100
  The program asked me to enter a valid number between 0 and 100.So I entered
  score1=20
  
  score2=300
  The program asked me to enter a valid number between 0 and 100.So I entered
  score2=30
  
  score3=-200
  The program asked me to enter a valid number between 0 and 100.So I entered
  score3=40
  
  Then the program printed 'The average is 30'
  
  */
  

Sample Output:


Related Solutions

Create a C++ program that will ask the user for how many test scores will be...
Create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. (no fstream) The data needs to include the student’s first name, student number test score the fields should be displayed with a total width of 15. The prompt should be printed with a header in the file explaining what each is: ex. First Name student number Test Score 1) mike 6456464   98 2) phill...
C++ program that will ask the user for how many test scores will be entered. Setup...
C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. The data will include the student’s first name and midterm score Print out the completed test scores to a file (midTermScores.txt) . Print out list of students names and grades in the print out, a letter grade should replace numeric score using standard grading (a = 90 – 100, b=80-90, c=70-80, d=60-70, f=below 60)
Create a program that calculates the average of 3 test scores. Make use of an array...
Create a program that calculates the average of 3 test scores. Make use of an array to store the integer scores. const int size = 3; int testScores[size]; Send this array to a function that actually calculates and returns the average. 1. Tell the user what the program does. 2. Prompt the user to enter the integer scores. ( Use a for loop to do this. ) 3. Create and implement a function with prototype: double average( int a[], int...
a) How many different license plates are possible if repeats are allowed from each contains 3...
a) How many different license plates are possible if repeats are allowed from each contains 3 letters (out of the first 15 letters of the alphabet) followed by 2 digits (0~9)? b) How many of these license plates contain different letters and different digits?
For the following 4 questions, consider the following 10 scores on a test: 45 45 60...
For the following 4 questions, consider the following 10 scores on a test: 45 45 60 65 75 80 85 90 90 100 A) Find the standard deviation of the data (to the nearest hundredth). B) Calculate the 25th percentile of the data.
Write a program that reads students’ names followed by their test scores. The program should output...
Write a program that reads students’ names followed by their test scores. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score. Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, testScore of type int (testScore is between 0 and...
Write a program that asks the user to enter five test scores. The program should display...
Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: calcAverage: This method should accept five test scores as arguments and return the average of the scores. determineGrade: This method should accept a test score as an argument and return a letter grade for the score, based on the following grading scale: Score Letter Grade 90-100...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program should display a letter grade for each score and the average test score. Hint: Declare local variables under main() program Prompts the user to enter 5 test scores Define a function to calculate the average score: this should accept 5 test scores as argument and return the avg Define a function to determine the letter grade: this should accept a test score as argument...
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
Instructions Write a Java program that asks the user t enter five test scores. The program...
Instructions Write a Java program that asks the user t enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: * calcAverage -- This method should accept five test scores as arguments and return the average of the scores. * determineGrade -- This method should accept a test score as an argument and return a letter grade for the score, based on the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT