Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab12........ //main.cpp #include<iostream> #include \"Scores.h\" #include<cstdl

ID: 3720424 • Letter: L

Question

Lab12........ //main.cpp #include<iostream> #include "Scores.h" #include<cstdlib> using namespace std; int main() {
//declaration of Scores array of 2 scores Scores scr[2]; int i, score1, score2, score3; cout << "Student Details" << endl; for (int i = 0; i<2; ++i) { cout << "Enter quiz scores for Student " << (i + 1) << endl; cout << "Enter score1" << endl; cin >> score1; cout << "Enter score2" << endl; cin >> score2; cout << "Enter score3" << endl; cin >> score3;
//calling set() of individual scores scr[i].setScore1(score1); scr[i].setScore2(score2); scr[i].setScore3(score3); } cout << "STUDENT SCORE DETAILS" << endl; for (int i = 0; i<2; ++i) { cout << "Student " << (i + 1) << " score details are: " << endl; cout << "Score1: " << scr[i].getScore1() << endl; cout << "Score2: " << scr[i].getScore2() << endl; cout << "Score3: " << scr[i].getScore3() << endl; } //calculate average for (int i = 0; i<2; ++i) cout << "Student " << (i + 1) << " Average is " << scr[i].average() << endl; system("pause"); return 0; } //Scores.cpp #include "Scores.h"
//constructor for initializing score values Scores::Scores(int score1, int score2, int score3) { (*this).score1 = score1; (*this).score2 = score2; (*this).score3 = score3; }
//set and get() for individual scores void Scores::setScore1(int score1) { (*this).score1 = score1; } int Scores::getScore1() { return (*this).score1; } void Scores::setScore2(int score2) { (*this).score2 = score2; } int Scores::getScore2() { return (*this).score2; } void Scores::setScore3(int score3) { (*this).score3 = score3; } int Scores::getScore3() { return (*this).score3; }
//calculate average double Scores::average() { double avg; avg = (double)(getScore1() + getScore2() + getScore3()) / (double)3; return avg; } //Scores.h class Scores { //declaration of three private member variables int score1, score2, score3; public: Scores() {} Scores(int, int, int); //get and set functions void setScore1(int); int getScore1(); void setScore2(int); int getScore2(); void setScore3(int); int getScore3(); //declaration of average() double average(); };

Explanation / Answer

Hi,
You have got most of the code correct, except for the sorting of the scores part, let me help you with that
you can use the std::sort function to sort an aray of objects as follows

bool fun(Scores a, Scores b)

{

return (a.score1< b.score1) && (a.score2< b.score2) && (a.score3< b.score3) ;

}

and then we can use this with the following line
std::sort(scr, scr+2, fun);  
and also include #include<stdlib.h>, therefore your overall code for main.cpp would be

#include<iostream>
#include<stdlib.h>

#include "Scores.h"

#include<cstdlib>

using namespace std;

bool fun(Scores a, Scores b)

{

return (a.score1< b.score1) && (a.score2< b.score2) && (a.score3< b.score3) ;

}

int main()

{

//declaration of Scores array of 2 scores

Scores scr[2];

int i, score1, score2, score3;

cout << "Student Details" << endl;

for (int i = 0; i<2; ++i)

{

cout << "Enter quiz scores for Student " << (i + 1) << endl;

cout << "Enter score1" << endl;

cin >> score1;

cout << "Enter score2" << endl;

cin >> score2;

cout << "Enter score3" << endl;

cin >> score3;

//calling set() of individual scores

scr[i].setScore1(score1);

scr[i].setScore2(score2);

scr[i].setScore3(score3);

}
std::sort(scr, scr+2, fun); // sorting the scores before printing

cout << "STUDENT SCORE DETAILS" << endl;

for (int i = 0; i<2; ++i)

{

cout << "Student " << (i + 1) << " score details are: " << endl;

cout << "Score1: " << scr[i].getScore1() << endl;

cout << "Score2: " << scr[i].getScore2() << endl;

cout << "Score3: " << scr[i].getScore3() << endl;

}

//calculate average

for (int i = 0; i<2; ++i)

cout << "Student " << (i + 1) << " Average is " << scr[i].average() << endl;

system("pause");

return 0;

}

Thumbs up if this was helpful, otherwise let me know in comments