Answer this question using c++. Here is my code from the program 2 mentioned in
ID: 3746035 • Letter: A
Question
Answer this question using c++. Here is my code from the program 2 mentioned in the question.
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string student_name;
string lettergrade;
float exam_1grade;
float exam_2grade;
float GPA;
void getGrade();
private:
void calcGPA();
};
void setStudentName(Student& c, string name)
{
c.student_name = name;
}
void setGrade1(Student& c, float grade1)
{
c.exam_1grade = grade1;
}
void setGrade2(Student& c, float grade2)
{
c.exam_2grade = grade2;
}
void calcGPA(Student& c)
{
c.GPA = (c.exam_1grade + c.exam_2grade) / 2;
}
void getGrade(Student &c)
{
if (c.GPA <= 100 && c.GPA >= 90)
c.lettergrade = "A";
else if (c.GPA < 90 && c.GPA >= 80)
c.lettergrade = "B";
else if (c.GPA < 80 && c.GPA >= 70)
c.lettergrade = "C";
else if (c.GPA < 70 && c.GPA >= 60)
c.lettergrade = "D";
else if (c.GPA < 60 && c.GPA >= 0)
c.lettergrade = "F";
}
void output(Student& c)
{
cout << "Student: " << c.student_name << endl;
cout << "First Exam Grade: " << c.exam_1grade << endl;
cout << "Second Exam Grade: " << c.exam_2grade << endl;
cout << "Grade: " << c.lettergrade << endl;
cout << "--------------------------" << endl;
}
int main(){
Student c;
setStudentName(c,"Amy");
setGrade1(c,95);
setGrade2(c,90);
calcGPA(c);
getGrade(c);
output(c);
return 0;
}
Explanation / Answer
I have completed the code and it is working perfectely fine. Please upvote my answer.
#include <iostream>
#include <string>
using namespace std;
int ind=0;
class Student {
public:
string student_name;
string lettergrade;
float exam_1grade;
float exam_2grade;
float GPA;
void getGrade();
private:
void calcGPA();
};
void setStudentName(Student& c, string name)
{
c.student_name = name;
}
void setGrade1(Student& c, float grade1)
{
c.exam_1grade = grade1;
}
void setGrade2(Student& c, float grade2)
{
c.exam_2grade = grade2;
}
void calcGPA(Student& c)
{
c.GPA = (c.exam_1grade + c.exam_2grade) / 2;
}
void getGrade(Student &c)
{
if (c.GPA <= 100 && c.GPA >= 90)
c.lettergrade = "A";
else if (c.GPA < 90 && c.GPA >= 80)
c.lettergrade = "B";
else if (c.GPA < 80 && c.GPA >= 70)
c.lettergrade = "C";
else if (c.GPA < 70 && c.GPA >= 60)
c.lettergrade = "D";
else if (c.GPA < 60 && c.GPA >= 0)
c.lettergrade = "F";
}
void output_stu(Student& c)
{
cout << "Student: " << c.student_name << endl;
cout << "First Exam Grade: " << c.exam_1grade << endl;
cout << "Second Exam Grade: " << c.exam_2grade << endl;
cout << "Grade: " << c.lettergrade << endl;
cout << "--------------------------" << endl;
}
void addStudent(Student stu_arr[10],string name,int grade1,int grade2)
{
Student c;
setStudentName(c,name);
setGrade1(c,grade1);
setGrade2(c,grade2);
calcGPA(c);
getGrade(c);
stu_arr[ind]=c;
ind++;
}
void output(Student stu_arr[10])
{
int size=ind;
for(int i=0;i<=size;i++)
{
output_stu(stu_arr[i]);
}
}
int main(){
//the driver function
Student stu_arr[10];
addStudent(stu_arr,"Amy",95,90);
addStudent(stu_arr,"Bob",74,63);
addStudent(stu_arr,"Charlie",86,80);
addStudent(stu_arr,"Daisy",75,99);
addStudent(stu_arr,"Edward",24,66);
output(stu_arr);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.