C++ CODE DUE DATE: 10724717 TT:59PM ASSIGNMENT BACKGROUND This group assignment
ID: 3597328 • Letter: C
Question
C++ CODE
DUE DATE: 10724717 TT:59PM ASSIGNMENT BACKGROUND This group assignment is designed to test your ability to use classes, friend function(s), assignment operators, and dynamic allocation. Please note, you will need to use all of the following to receive full credit. Failure to do so will result in point deductions. ASSIGNMENT DESCRIPTI ON You and your group are tasked to generate a list of students each with a random grade. From there, depending on the user's input, you are to divide the students into groups and calculate the group's average grades. TEST CASES You are to ask the user how many students they would like to enter. You should test for 6 students at first, then try 10 students. Lastly try a total of 12 students. You should ask the user how many people are in each group. Valid answers are 2, 3, and 4 people per group. You should include some error checking incase user inputs anything other than 2-4. If you have not already noticed, having 6 or 10 students and if the user tries to divide them into groups of 4, would result in an error since they cannot be split evenly. Therefore you should make sure to use create error check to check for if such a case arises. Print out an error STRUCTURE You should create a class called Student. In this class, you should have the following Private member variables: int studentiD and intstudentGrade Public member functions: constructor with 1 parameter to store studentID and to generate a random studentGrade. You can use rand) to generate a grade from 1-100. - - - A friend function print Student temp) to print out the student's ID and grade -int assignment operator (const Student& next) where you add 2 student object's grades together . Another int assignment operator const int& first, const Student& net). This will be explained in the next portion. You should create the friend function outside of the class. (THIS MEANS THE LOGIC IS NOT IN STUDENT.CPP or in STUDENT.H). You should still initialize the friend function inside Student class so that you can access its member functions. You should create 3 functions outside of the class to add up the respective objects. A function called avgOf2 with 2 parameters (Student a, Student b) to add up both student's grades - o You will only need to use 1 operator+ function forthis A function called avgOf3 with 3 parameters (Student a, Student b, Student c) to add up all three student grades. - You will need to use both operator+ functions forthis o A function called avgOf4 with 4 parameters (Student a, Student b, Student c, Student d) to add up all four student grades. - o You will need to use both operator+ functions forthisExplanation / Answer
Following Are the program : --------------------- >>>>>>>>>>
#include<iostream>
using namespace std;
class Student{
int studentId;
int studentGrade;
public:
Student(){
studentId = 0;
studentGrade = 0;
}
Student(int i){
studentId = i;
studentGrade = (rand() % 100)+1;
}
friend void print(Student st);
int operator+(const Student &a){
return this->studentGrade + a.studentGrade;
}
int opera(int a,const Student &b){
return a+b.studentGrade;
}
};
int avgOf2(Student a,Student b){
return (a+b)/2;
}
int avgOf3(Student a,Student b,Student c){
int f = a+b;
return (b.opera(f,c))/3;
}
int avgOf4(Student a, Student b, Student c, Student d){
int f = a+b;
f = a.opera(f,c);
return (a.opera(f,d))/4;
}
void print(Student s){
cout<<" Student "<<s.studentId<<" : Grade "<<s.studentGrade;
}
int main(){
int ns;
int gs;
cout<<"------------------WELCOME TO STDENT AVERAGE---------------";
error:
cout<<" Enter The No. of student ";
cin>>ns;
if(ns == 6 || ns == 10 || ns == 12 ){
goto cont;
}
else{
cout<<" Enter The Value 6 , 10 or 12";
goto error;
}
cont:
Student student[ns];
error1:
cout<<" Enter The Group Size ";
cin>>gs;
if(gs == 2 || gs == 3 || gs == 4 ){
if(ns == 10){
if(gs == 3 || gs == 4){
cout<<" CANNOT SPLIT GROUP EVENLY";
goto end;
}
}
if(ns == 6){
if(gs == 4){
cout<<" CANNOT SPLIT GROUP EVENLY";
goto end;
}
}
goto cont1;
}
else{
cout<<" Enter The Value 2 , 3 or 4";
goto error1;
}
cont1:
for(int i=1;i<=ns;i++){
student[i] = Student(i);
}
int grade;
for(int i = 1; i<=ns;){
if(gs == 2){
grade = avgOf2(student[i],student[i+1]);
print(student[i]);
print(student[i+1]);
cout<<" Group Average = "<<grade<<" ";
i = i+2;
}
if(gs == 3){
grade = avgOf3(student[i],student[i+1],student[i+2]);
print(student[i]);
print(student[i+1]);
print(student[i+2]);
cout<<" Group Average = "<<grade<<" ";
i = i+3;
}
if(gs == 4){
grade = avgOf4(student[i],student[i+1],student[i+2],student[i+3]);
print(student[i]);
print(student[i+1]);
print(student[i+2]);
print(student[i+3]);
cout<<" Group Average = "<<grade<<" ";
i = i+4;
}
}
end:
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.