In: Computer Science
IN C++
Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop.
Sample Output
Please enter the number of students:
4
Enter the student name:
Ben Simmons
Enter the score:
70
Enter the student name:
Carson Wentz
Enter the score:
80
Enter the student name:
Joel Embiid
Enter the score:
90
Enter the student name:
Bryce Harper
Enter the score:
75
Joel Embiid
score is 90
the average is 78.75
the difference is 11.25
Press any key to continue . . .
Explanation:
Here is the code which asks for the number of students from the user and then each student's score and name is asked and sum is taken for the scores and the highest score is also detected.
Then, average, highest score are printed and then difference of both is also printed.
Code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Please enter the number of students: ";
cin>>n;
string name_highest = "";
int highest = -1;
int sum = 0;
int i=0;
while (i < n) {
string s;
int score;
getline(cin, s);
cout<<"Enter the student name: ";
getline(cin, s);
cout<<"Enter the score: ";
cin>>score;
sum+= score;
if(highest<score)
{
name_highest = s;
highest = score;
}
i++;
}
cout<<name_highest<<endl;
cout<<"score is "<<highest<<endl;
float avg = (float)sum/(float)n;
cout<<"the average is "<<avg<<endl;
cout<<"the difference is
"<<(highest-avg)<<endl;
return 0;
}
output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!