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. If the bonus becomes HArd

Hey, I need help with this "easy" C++ code. These are very short, thank you in advance. I will upvote. If the bonus becomes "HArd" just ignore it because I am pretty sure I wont need to use it for future reference

Exercise 5

IMPLEMENT IT USING ANY LOOP

In this exercise, we are going to remove a Student record from the system.

Complete the method void Student::removeByID(vector &v, long ID): Removes the first occurrence of the specified Student ID

//Test

Student s4(3, "Ariel", FEMALE,"CIIC", 4.0); Student s5(4, "Mulan", FEMALE, "ICOM", 3.1); Student s6(5, "Aladin", MALE, "CIIC", 3.1); vector testVector2; testVector2.push_back(s4); testVector2.push_back(s5); testVector2.push_back(s6); cout << "Before removing ID: " << Student::toString(testVector2) << endl; Student::removeByID(testVector2, 5); cout << "After removing: " << Student::toString(testVector2) << endl;

//Result

Before removing ID: {{3,Ariel,F,4.000000},{4,Mulan,F,3.100000},{5,Aladin,M,3.100000},} After removing: {{3,Ariel,F,4.000000},{4,Mulan,F,3.100000},}

Exercise 6

DO NOT USE WHILE LOOPS

In this exercise, we are going to update a Student record of the system.

Complete the method void Student::updateStudent(vector &v, Student s): Find the Student record that matches the given Student and update its data

//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); Student temp(2, "Mariela", FEMALE, 2.3); cout << "Before Updating: " << Student::toString(testVector1) << endl; Student::updateStudent(testVector1, temp); cout << "After Updating: " << Student::toString(testVector1) << endl;

//result

Before Updating: {{0,Bienve,M,3.000000},{1,Jose Juan,M,2.800000},{2,Ana,F,3.500000},} After Updating: {{0,Bienve,M,3.000000},{1,Jose Juan,M,2.800000},{2,Mariela,F,2.300000},}

BONUS

IMPLEMENT WITH NESTED LOOPS

In this exercise, we are going to store the Students that have the same name on the system.

Complete the method vector Student::repeatedStudentNames(vector v): Returns a vector cointaining the Students that have the same name.

//test

Student s4(3, "Ariel", FEMALE,"CIIC", 4.0); Student s5(4, "Ariel", FEMALE, "ICOM", 3.1); Student s6(5, "Aladin", MALE, "CIIC", 3.1); vector testVector2; testVector2.push_back(s4); testVector2.push_back(s5); testVector2.push_back(s6); vector resultB = Student::repeatedStudentNames(testVector2); cout << "Students with same name: " << Student::toString(resultB) << endl;

//result

Students with same name: {{3,Ariel,F,4.000000},{4,Ariel,F,3.100000},}

CODE STARTS HERE

Three(3) src

main.cpp, Student.cpp, 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 #5----" << endl;

cout << "Before removing ID: " << Student::toString(testVector2) << endl;

Student::removeByID(testVector2, 5);

cout << "After removing: " << Student::toString(testVector2) << endl;

cout << " ----Exercise #6----" << endl;

Student temp1(6, "Mariela", FEMALE, 2.3);

cout << "Before Updating: " << Student::toString(testVector1) << endl;

Student::updateStudent(testVector1, temp1);

cout << "After Updating: " << Student::toString(testVector1) << endl;

cout << " -------BONUS-------" << endl;

vector temp2 = Student::repeatedStudentNames(testVector2);

cout << "Students with same name: " << Student::toString(temp2) << endl;

}

//Student.cpp

#include "Student.h"

using namespace std;

}

/*

* EXERCISE: #5

*

* IMPLEMENT WITH ANY LOOP

*

* Removes the first occurrence of the specified Student ID,

* if it is present. If not present, then list is unchanged.

*

* HINT: Verify the methods erase() and begin() of the vector

*/

void Student::removeByID(vector &v, long ID){

//YOUR CODE HERE

}

/*

* EXERCISE #6

*

* DO NOT USE WHILE LOOPS

*

* Find the Student record that matches the given Student

* and update its data. If the Student is not present, add it to the list.

*

* Remember that each Student has an unique identifier

*/

void Student::updateStudent(vector &v, Student s){

//YOUR CODE HERE

}

/*

* BONUS

*

* IMPLEMENT WITH NESTED LOOPS USING ANY LOOP.

*

* Returns a vector cointaining two Students that has the same name.

* If there is no repeated names, the vector stays empty.

*

* HINT: Use the compare method of the string library

*/

vector Student::repeatedStudentNames(vector& v){

//YOUR CODE HERE

return v;

}

// 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 void updateStudent(vector &v, Student s);

static vector findStudents(vector& v, float gpa);

static vector repeatedStudentNames(vector& v); //BONUS

};

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

Students also viewed these Databases questions