Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey, I need help with this easy C++ code. These are very short, thank you in advance. I will upvote. Exercise 2A) YOU MUST IMPLEMENT

Hey, I need help with this "easy" C++ code. These are very short, thank you in advance. I will upvote.

Exercise 2A) YOU MUST IMPLEMENT IT USING AN ENHANCED FOR LOOP (For-each)

-In this exercise, we are going to find the maximum GPA that is on the student system.

//Test

Student s1(0, "Bienve", MALE, 3.0); Student s2(1, "Jose Juan", MALE, 2.8); Student s3(2, "Ana", FEMALE, 3.5); vector testVector1; testVector1.push_back(s1); testVector1.push_back(s2); testVector1.push_back(s3); cout << "Maximum GPA: " << Student::maxStudentGPA(testVector1) << endl;

//Result

Maximum GPA: 3.5 

Exercise 2B) YOU MUST IMPLEMENT IT WITH A REGULAR FOR LOOP

In this exercise, we are going to find the lowest GPA on the system.

//

Student s1(0, "Bienve", MALE, 3.0); Student s2(1, "Jose Juan", MALE, 2.8); Student s3(2, "Ana", FEMALE, 3.5); vector testVector1; testVector1.push_back(s1); testVector1.push_back(s2); testVector1.push_back(s3); cout << "Minimum GPA: " << Student::minStudentGPA(testVector1) << endl;

//result

Minimum GPA: 2.8

Exercise 2C

YOU MUST IMPLEMENT IT USING A WHILE LOOP

In this exercise, we are going to find the average GPA of N Student Records.

//test

Student s1(0, "Bienve", MALE, 3.0); Student s2(1, "Jose Juan", MALE, 2.8); Student s3(2, "Ana", FEMALE, 3.5); vector testVector1; testVector1.push_back(s1); testVector1.push_back(s2); testVector1.push_back(s3); cout << "Average GPA of N Students: " << Student::averageGPA(testVector1, 2) << endl;

//results

Average GPA of N Students: 2.9

CODE STARTS HERE

Three src

Main.cpp, Student.cpp and Student.h

//main.cpp

#include "Student.h"

using namespace std;

int main() {

Student s1(0, "Bienve", MALE, 3.0);

Student s2(1, "Jose Juan", MALE, 2.8);

Student s3(2, "Ana", FEMALE, 3.5);

Student s4(3, "Ariel", FEMALE,"CIIC", 4.0);

Student s5(4, "Mulan", FEMALE, "ICOM", 3.56);

Student s6(5, "Aladdin", MALE, "CIIC", 3.1);

vector testVector1;

testVector1.push_back(s1);

testVector1.push_back(s2);

testVector1.push_back(s3);

vector testVector2;

testVector2.push_back(s4);

testVector2.push_back(s5);

testVector2.push_back(s6);

cout << "---------TESTS---------" << endl;

cout << " ----Exercise #2A----" << endl;

cout << "Maximum GPA: " << Student::maxStudentGPA(testVector1) << endl;

cout << " ----Exercise #2B----" << endl;

cout << "Minimum GPA: " << Student::minStudentGPA(testVector1) << endl;

cout << " ----Exercise #2C----" << endl;

cout << "Average GPA of N Students: " << Student::averageGPA(testVector1, 2) << endl;

}

//Student.cpp

#include "Student.h"

using namespace std;

/*

* EXERCISE: #2A

*

* IMPLEMENT USING AN ENHANCED FOR LOOP (ForEach).

*

* Returns the value of the Student's GPA

* that has the maximum GPA on the list

*

*/

double Student::maxStudentGPA(vector& v)

{

//YOUR CODE HERE

return -99.9; //DUMMY RETURN

}

/*

* EXERCISE: #2B

*

* IMPLEMENT USING A REGULAR FOR LOOP.

*

* Returns the value of the Student's GPA

* that has the minimum GPA of the list

*

*/

double Student::minStudentGPA(vector& v)

{

//YOUR CODE HERE

return -99.9; //DUMMY RETURN

}

/*

* Exercise #2C

*

* IMPLEMENT USING A WHILE LOOP

*

* For the first N students, calculate the average gpa

*

* Formula: average = sum / N

* Assume N is greater than 0

*/

double Student::averageGPA(vector &v, int N){

//YOUR CODE HERE

return -99.9; //DUMMY RETURN

}

}

//Student.h

#include

#include

#include

using namespace std;

enum Gender{

MALE,

FEMALE

};

class Student{

private:

long id; // Unique ID

string name; // Name of student

Gender gender; // Gender of student

string courseCode; // Course code (CIIC or ICOM)

double gpa; // GPA of student

public:

Student(long id, const string &name, Gender gender, double gpa){

this->id = id;

this->name = name;

this->gender = gender;

this->courseCode = "";

this->gpa = gpa;

}

Student(long id, const string &name, Gender gender, string courseCode, double gpa){

this->id = id;

this->name = name;

this->gender = gender;

this->courseCode = courseCode;

this->gpa = gpa;

}

Student(){}

static string toString(Student& s){

string genderLetter = (s.gender == MALE ? "M" : "F");

return string("{" + to_string(s.id) + "," + s.name + "," + genderLetter + "," + to_string(s.gpa) + "}");

}

static string toString(vector& v){

string result = "{";

for (Student s : v) {

result += toString(s) + ",";

}

result += "}";

return result;

}

static string toString(vector& v){

string result = "{";

for (long id : v) {

result += to_string(id) + ",";

}

result += "}";

return result;

}

// Getters

long getID(){return id;}

string getName(){return name;}

Gender getGender(){return gender;}

double getGPA(){return gpa;}

string getCourseCode(){return courseCode;}

//Setters

void setName(string name){this->name = name;}

void setGender(Gender gender){this->gender = gender;}

void setGPA(double gpa){this->gpa = gpa;}

void setCourseCode(string code){this->courseCode = code;}

// EXERCISES

static double maxStudentGPA(vector& v);

static double minStudentGPA(vector& v);

static double averageGPA(vector &v, int N);

};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions