C Program flow 1 Input data 2 Checkvalidate data 3 Calculate
C++
Program flow: 1. Input data 2. Check/validate data 3. Calculate final grade 4. Output data Requirements: 1. The number of students to enter will be 10. 2. Using a struct, you will keep track of a student\'s: a. first name b. last name c. student ID d. 4 exam grades (each out of 100 points) e. final letter grade. 3. For each student, if the user enters invalid data he will be allowed to enter it again. 4. Steps 3 and 4 of the Program Flow cannot execute if your data checking detected an error. 5. Letter grade calculation will use the standard grading scale. a. 90% - 100% = A b. 80% - 89% = B c. 70% - 79% = C d. 60% - 69% = D e. Below 60% = F 6. The output will consist of: a. A list of students who passed (C or higher) b. A list of students who did not pass (D or lower) c. The person(s) with the highest grade (points, not letter) Restrictions: 1. main() will only consist of variable declarations, functions, and small amounts of logic, if necessary. The majority of the logic will reside in the core functions. 2. In the student struct, the exam grades must be stored as an array, not 4 separate variables. 3. For Program Flow 2: use a function 4. For Requirement 3: use a try/catch and a loop Example output ------------- PASS ---- Aberdeen, Joe A Blake, Kevin A Garry, Glen C Jenakovski, Mary B FAIL ---- Zoolander, Derek F HIGHEST GRADE ---- Blake, Kevin A Extra Credit: 1. Allow the user to specify the number of students to be entered 2. Allow spaces in the first or last name 3. Protect against bad data, in addition to that which is required in Requirement 3 4. Use a recursive function for Requirement 6c
Solution
#include<iostream>
#include<exception>
using namespace std;
typedef struct student
{
string fname;
string lname;
string sid;
int marks[4];
string grade;
};
int getPercentage(struct student s)
{
return (s.marks[0]+s.marks[1]+s.marks[2]+s.marks[3])/4;
}
string getGrade(struct student s)
{
string grade=\"\";
int gr=getPercentage(s);
if(gr>=90)
grade=\"A\";
else if(gr>=80 && gr<90)
grade=\"B\";
else if(gr>=70 && gr<80)
grade=\"C\";
else if(gr>=60 && gr<70)
grade=\"D\";
else if(gr<60)
grade=\"F\";
return grade;
}
void validInput(struct student *s,int i)
{
int b[4];
cout<<\"Enter the first name of student \"<<(i+1)<<\": \";
cin>>s->fname;
cout<<\"Enter the last name of student \"<<(i+1)<<\": \";
cin>>s->lname;
cout<<\"Enter the id of student \"<<(i+1)<<\": \";
cin>>s->sid;
label:cout<<\"Enter the marks in suject 1 of student \"<<(i+1)<<\": \";
cin>>b[0];
if(b[0]>=0 && b[0]<=100)
s->marks[0]=b[0];
else{
cout<<\"Enter the marks between 0 to 100\ \";
goto label;}
label2:cout<<\"Enter the marks in suject 2 of student \"<<(i+1)<<\": \";
cin>>b[1];
if(b[1]>=0 && b[1]<=100)
s->marks[1]=b[1];
else{
cout<<\"Enter the marks between 0 to 100\ \";
goto label2;}
label3: cout<<\"Enter the marks in suject 3 of student \"<<(i+1)<<\": \";
cin>>b[2];
if(b[2]>=0 && b[2]<=100)
s->marks[2]=b[2];
else{
cout<<\"Enter the marks between 0 to 100\ \";
goto label3;}
label4:cout<<\"Enter the marks in suject 4 of student \"<<(i+1)<<\": \";
cin>>b[3];
if(b[3]>=0 && b[3]<=100)
s->marks[3]=b[3];
else{
cout<<\"Enter the marks between 0 to 100\ \";
goto label4;}
s->grade = getGrade(*s);
}
int main()
{
struct student students[10];
int highest = 0;
int a;
try{
for(int i=0;i<10;i++)
{
validInput(&students[i],i);
if(highest<getPercentage(students[i]))
{
highest = getPercentage(students[i]);
a=i;
}
}
cout<<\"\ ------------- PASS ----\ \";
for(int i =0;i<10;i++)
{
if(students[i].grade == \"A\" || students[i].grade == \"B\" || students[i].grade == \"C\")
cout<<students[i].lname<<\", \"<<students[i].fname<<\" \"<<students[i].grade<<\"\ \";
}
cout<<\"\ ------------- FAIL ----\ \";
for(int i =0;i<10;i++)
{
if(students[i].grade == \"D\" || students[i].grade == \"E\" || students[i].grade == \"F\")
cout<<students[i].lname<<\", \"<<students[i].fname<<\" \"<<students[i].grade<<\"\ \";
}
cout<<\"\ ------------- HIGHEST GRADE----\ \";
{
cout<<students[a].lname<<\", \"<<students[a].fname<<\" \"<<students[a].grade<<\"\ \";
}
}catch(exception& e)
{
cout<<\"\ Error\";
}
}


