You are given a list of students’ names and their test scores. *Write a program
ID: 668291 • Letter: Y
Question
You are given a list of students’ names and their test scores. *Write a program that does the following*: ( Must be C++ language)
a. Calculates the average test scores.
b. Determines and prints the names of all the students whose test scores are below the average test score.
c. Determines the highest test score.
d. Prints the names of all the students whose test scores are the same as the highest test score.
STUDENT NAMES AND TEST SCORES
Walker Noelle 75
Hunt Lawrence 87
Watt Aubrey 96
Richardson Shelby 82
Wallace Matthew 66
Oliver Shane 92
Cena John 96
Patrick Matthew 83
Smith Damon 54
Marie Rachel 55
Fink Emily 85
Richardson Abby 88
Hellary Danielle 96
Explanation / Answer
#include <iostream>
#include <math>
#include <fstream>
#include <string>
double calcAverage (int, int, int); //Prototype for the function that calculates the average of the scores
int findHighest (int, int, int); //Prototype for the function that finds the highest score
int findLowest (int, int, int); //Prototype for the function that finds the lowest score
//using namespace std;
int main()
{
string name; //Variable for the name of the students
int first, second, third; //Variables for the test scores
ifstream fileIn; //File stream object for input file
ofstream fileOut; //File stream object for output file
fileIn.open("input.txt"); //This is the file that will be opened and inputted
fileOut.open("output.txt"); //This is the file that will be created and outputted
if (fileIn) //If the open operation succeeded this condition will be satisfied
{
while(fileIn >> name >> first >> second >> third) //Loop that continues execution and inputs values as long as they are available in the file
{
//Three function are called in the following statement (explained below)
fileOut << name << " - Average is: " << calcAverage(first, second, third) << "; Highest is: " << findHighest(first, second, third) << "; Lowest is: " << findLowest(first, second, third) << endl; //Creates output file with average, highest score, and lowest score
}
cout << "Your file has been made." << endl; //This is the standard output the user will immediately see after the output file has been successfully made.
fileIn.close(); //Input file is closed, and no further changes are made
fileOut.close(); //Output file is closed, and no further changes are made
}
else cout << "There was an error when opening your file." << endl; //If the input file hasn't been found, this statement is printed in standard output
}
double calcAverage(int first, int second, int third) //This function calculates the average of the three scores for each student
{
return ((first + second + third) / 3.0); //Returns the average of the three scores when called for in the main function
}
int findHighest(int first, int second, int third) //This function finds the highest of the three scores for each student
{
if(first >= second && first >= third) //If the first score is greater than or equal to both the second and third score this condition is satisfied
return first; //The first score is returned when this function is called for in the main function
else if (second >= first && second >= third) //If the second score is greater than or equal to both the first and third score this conditon is satisfied
return second; //The second score is returned when this function is called for in the main function
else return third; //If neither the first nor the second score are returned, the third score is returned into the main function when this function is called for
}
int findLowest(int first, int second, int third) //This function finds the lowest of the three scores for each student
{
if (first <= second && first <= third) //If the first score is less than or equal to both the second and third score this condition is satisfied
return first; //The first score is returned when this function is called for in the main function
else if (second <= first && second <= third) //If the second score is less than or equal to both the first and third score this condition is satisfied
return second; //The second score is returned when this function is called for in the main function
else return third; //If neither the first nor the second score are returned, the third score is returned into the main function when this function is called for
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.