Write a struct called Student that has the following variables and methods – be
ID: 3703502 • Letter: W
Question
Write a struct called Student that has the following variables and methods – be sure to use the variable names provided:
strings firstName, lastName,
int midtem and final,
and a vector of int called homework.
There will be an overloaded constructor.
An empty constructor and
a constructor that takes first and last names, the midterm and the final.
The data is as follows:
The first name followed by last name (always given first)
The name is followed by the list of grades. The list of grades will contain grades of the following types;
hw –indicates a homework grade to be pushed on the vector
midterm – there will be 1 midterm grade entered
final – there will be one final grade entered
These grades are not in any particular order!! Do not assume the midterm grade is first, etc.
Notation for accessing a vector in a class.
Student mySt; // has a vector of homework
mySt.homework[i]; //notice the index goes after the name of the vector
mySt.homework.size(); // size goes after the name of the vector.
Explanation / Answer
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
struct student
{
string firstName, lastName;
int midtem ,final;
vector<int>homework;
// default constructor
student();
// parametrized constructor
student(string,string,int,int);
};
// default constructor
student::student()
{
firstName="";
lastName="";
midtem =0;
final=0;
}
// parametrized constructor
student::student(string a,string b,int c ,int d)
{
firstName=a;
lastName=b;
midtem =c;
final=d;
}
int main()
{
string a,b;
int mid,final;
cout<<"Enter first name : ";
cin>>a;
cout<<"Enter last name : ";
cin>>b;
cout<<"Enter midterm marks : ";
cin>>mid;
cout<<"Enter final marks : ";
cin>>final;
//create object of structure
student myStudent(a,b,mid,final);
int i,n,score;
double grade,homeworkAverage,sum=0;
cout<<"Enter Number of home work assignments : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter score of "<<i+1<<" assignment : ";
cin>>score;
myStudent.homework.push_back(score);
}
// to calculate sum of homework assignment
for(i=0;i<n;i++)
sum+=myStudent.homework[i];
homeworkAverage=sum/n;
// To find grade
grade=.2 *homeworkAverage + .3 *myStudent.midtem + .5 *myStudent.final;
cout << "midtem marks = " << fixed << showpoint << setprecision(2) << myStudent.midtem << endl;
cout << "Final marks = " << fixed << showpoint << setprecision(2) << myStudent.final << endl;
cout << "Home assignement marks = " << fixed << showpoint << setprecision(2) << sum << endl;
cout << "grade = " << fixed << showpoint << setprecision(2) << grade << endl;
return 0;
} // end main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.