i have made a program with a vector that is a instance of a class student with three variables. i have stored the information and calculated the class average. what I want to do is get the person with the highest test score and output "this student has the highest score with". This is where im lost and need some help to go in the right direction.

here is my code

#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Student {
	public:
		virtual void print();
		Student(string,string,double);
		Student();
		virtual ~Student();
		double score;
	protected:
		string firstName;
		string lastName;
		//double score;
};

void Student::print() {
	std::cout<<firstName<<"  "<<lastName<<"  "<<score<<std::endl;
}


Student::Student(string first,string last,double x) {
	firstName = first;
	lastName = last;
	score = x;
}
Student::~Student() {

	firstName = "";
	lastName = "";
	score = 0;
}
int main() {

	vector <Student*> list;
	double average;
	double total;

	list.push_back(new Student("Louis","Radske",90));
	list.push_back(new Student("Bill","Collins",88));
	list.push_back(new Student("Ted","Blood",78));
	list.push_back(new Student("Tim","Sails",98));
	list.push_back(new Student("Ed","Goodson",85));

	for(int z=0;z<list.size();z++)
	{
        total+=list[z]->score;
	}

    average = total/list.size();
    std::cout<<"The class average is  "<<average<<std::endl;

    //std::cout<<"MAX " << *max_element(list.begin(),list.end())<<std::endl;
    for(int x=0;x<list.size();x++)
    {
        list[x]->print();
    }
    //list.front()->print();
	return(0);
}

thanks.

inside the loop that starts on line 50 keep track of the student who has the highest score. Do this by saving the value of the i loop counter into another variable. For example:

int highest = 0;
for(int z=0;z<list.size();z++)
{
   total+=list[z]->score;
   if( list[z]->score > list[highest]->score)
         highest = z;
}

Edited 6 Years Ago by Ancient Dragon: n/a

The Problem with the above code is that

std::cout<<"MAX " << *max_element(list.begin(),list.end())<<std::endl;

in this line, the function max_element doesn't know how to compare between 2 elements of type Student.
If we send it an extra parameter

std::cout<<"MAX " << *max_element(list.begin(),list.end(),my_comp_func)<<std::endl;

and if

bool my_comp_func(Student,Student);

Is written. I think the code would work.. However I'm kinda unsure about it.

it would be as below. The code would have been a bit easier if you had not created a vector of pointers . vector<Student> list;

bool my_comp_func(Student*& s1,Student*& s2)
{
    return s1->score < s2->score;
}

int main() {

	vector <Student*> list;
	double average = 0.0;
	double total = 0.0;

	list.push_back(new Student("Louis","Radske",90));
	list.push_back(new Student("Bill","Collins",88));
	list.push_back(new Student("Ted","Blood",78));
	list.push_back(new Student("Tim","Sails",98));
	list.push_back(new Student("Ed","Goodson",85));

	for(size_t z=0;z<list.size();z++)
	{
        total+=list[z]->score;
	}

    average = total/list.size();
    std::cout<<"The class average is  "<<average<<std::endl;
    vector <Student*>::iterator it = max_element(list.begin(),list.end(),my_comp_func );
    cout << "MAX: " << (*it)->score << '\n';
    for(size_t x=0;x<list.size();x++)
    {
        list[x]->print();
    }
    //list.front()->print();
	return(0);
}

Edited 6 Years Ago by Ancient Dragon: n/a

this is what i got. it works but i want to try the compare function. thanks to all

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Student {
	public:
		virtual void print();
		Student(string,string,double);
		Student();
		virtual ~Student();
		double score;
	protected:
		string firstName;
		string lastName;
		//double score;
};

void Student::print() {
	std::cout<<firstName<<"  "<<lastName<<"  "<<score<<std::endl;
}


Student::Student(string first,string last,double x) {
	firstName = first;
	lastName = last;
	score = x;
}
Student::~Student() {

	firstName = "";
	lastName = "";
	score = 0;
}
int main() {

	vector <Student*> list;
	//double high = list[0]->score;
	double average;
	double total;
    double high = 0;
    list.push_back(new Student("Louis","Radske",90));
	list.push_back(new Student("Bill","Collins",88));
	list.push_back(new Student("Ted","Blood",78));
	list.push_back(new Student("Tim","Sails",98));
	list.push_back(new Student("Ed","Goodson",85));

	for(int z=0;z<list.size();z++)
	{
        total+=list[z]->score;

        if(list[z]->score > high)
           high = list[z]->score;
	}

    average = total/list.size();
    std::cout<<"The class average is  "<<average<<std::endl;
    std::cout<<"The highest test score "<<high<<std::endl;

    for (int z=0;z<list.size();z++)
    {
        if (list[z]->score == high)
        {
            std::cout <<"Student with high score is: ";
            list[z]->print();
        }
    }

    //std::cout<<"MAX " << *max_element(list.begin(),list.end())<<std::endl;
    for(int x=0;x<list.size();x++)
    {
        list[x]->print();
    }
    //list.front()->print();
	return(0);
}
This article has been dead for over six months. Start a new discussion instead.