In Programming Exercise 13 (Chapter 8), you are asked to write a program to calculate students’ average test scores and their grades. Improve this programming exercise by adding a function to sort students’ names so that students’ data is output into ascending order according to their name.
*Need the answer in C++*
#include<iostream>
using namespace std;
struct student{
string name;
char grade;
};
bool studSort(student a, student b){
return a.name<b.name;
}
int main()
{
int n,x;
cout<<"Enter number of students: ";
cin>>n;
cout<<"Enter number of subjects: ";
cin>>x;
struct student s[n];
for(int j=0;j<n;j++){
cout<<"Enter name
of student "<<j+1<<": ";
cin>>s[j].name;
int mark[x], i;
float sum=0,avg;
cout<<"\nEnter
Marks in "<<x<<" subjects :: \n";
for(i=0; i<x;
i++)
{
cout<<"\nEnter Marks["<<i+1<<"] :: ";
cin>>mark[i];
sum=sum+mark[i];
}
avg=sum/x;
if(avg>80)
{
s[j].grade = 'A';
}
else if(avg>60
&& avg<=80)
{
s[j].grade = 'B';
}
else if(avg>40
&& avg<=60)
{
s[j].grade = 'C';
}
else
{
s[j].grade = 'D';
}
}
sort(s,s+n,studSort);
for(int i=0;i<n;i++){
cout<<"Name:
"<<s[i].name<<"; Grade:
"<<s[i].grade<<endl;
}
return 0;
}
Please refer to the screenshot of the code to understand the indentation of the code
Hope this helped. Please do upvote and if there are any queries please ask in comments section.
Get Answers For Free
Most questions answered within 1 hours.