In: Computer Science
IN C++
As in previous labs, RESIST THE URGE TO CODE! Determine what data will be necessary in order to complete this task. Then design the steps necessary to process that data. Do you need if statements? Do you need loops? Do you need if statements within your loops?
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 for loop.
Test Data:
Ben Simmons 70
Carson Wentz 80
Joel Embiid 90
Bryce Harper 75
Yes we need if contdition to find the maximum marks scored by the student
Yes we need for loop to read the n students names and their marks
yes we need a if condtion inside the loop
when the student marks are entered we compare them with max marks
so we need if inside the loop
Code:
#include <iostream>
using namespace std;
int main()
{
int n,max=0,i;
float sum=0;/*Declaring variables*/
cout<<"Enter no of students:";
cin>>n;/*Reading no of students*/
int marks[n];
string name[n];/*Declaring 2 array one for marks other for
names*/
for(i=0;i<n;i++)
{/*This loop iterates 0 to n-1*/
cout<<"Enter the name of student:";
cin.get();
getline(cin,name[i]);/*Reading students name*/
cout<<"Enter student marks:";
cin>>marks[i];/*Reading students marks*/
sum+=marks[i];/*Total of the all students*/
if(marks[i]>marks[max])
{
/*If student marks is greater than the marks[max ]
then we assign the max= current student index*/
max=i;
}
}
cout<<"Heighest Score: "<<marks[max]<<"\nStudent
Name:"<<name[max]<<endl;
/*Printing the highest score and student name*/
cout<<"Average
Score:"<<sum/n<<endl;/*Average*/
cout<<"Difference between Heighest and Average:
"<<marks[max]-sum/n<<endl;/*Difference between Heighest
and Average*/
return 0;
}
Output:
Indentation: