7 Replies - 507 Views - Last Post: 04 October 2010 - 01:10 PM Rate Topic: -----

#1 lradske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-April 09

vector problem

Posted 04 October 2010 - 09:01 AM

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);
}



Is This A Good Question/Topic? 0
  • +

Replies To: vector problem

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: vector problem

Posted 04 October 2010 - 09:05 AM

Why this:

    vector <Student*> list;


and not this:

    vector <Student> list;


Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: vector problem

Posted 04 October 2010 - 09:07 AM

I would imagine this would involve going through the vector, as you do in other instances, and checking the score? Maybe with a temporary variable to hold the current highest score and an if statement? I mean, if you wrote the rest of this, I can't imagine this is a great leap.
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: vector problem

Posted 04 October 2010 - 09:14 AM

Another general things: why <cstring> is included? I couldn't even compile this, because you deal with std::string, therefore, you should include <string>.
Secondly, why is the destructor virtual, and print function virtual?
And lastly, you are creating some nice leaks here:
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));


as you forgot (or you didn't know) to free the memory after dealing with "list".
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: vector problem

Posted 04 October 2010 - 09:18 AM

I am going to guess that the reason for the virtual functions in this case is that eventually polymorphism will come into play, so that is in preparation for that. If you have a class that may serve as a base class, its destructor should of course be virtual.

EDIT: Reference
Reference (FAQ)

This post has been edited by JackOfAllTrades: 04 October 2010 - 09:19 AM

Was This Post Helpful? 0
  • +
  • -

#6 Banfa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 109
  • Joined: 07-June 10

Re: vector problem

Posted 04 October 2010 - 09:30 AM

You could just provide you own compare function and them call std::max_element
Was This Post Helpful? 1
  • +
  • -

#7 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: vector problem

Posted 04 October 2010 - 09:54 AM

You have a signed/unsigned mismatch. This can lead to an infinite loop, and your compiler should warn you about this.

Quote

for(int z=0;z<list.size();z++)



Please ensure the count control variable is the same type as the type returned by list.size().

if you use a signed int to increment until >= unsigned int

the signed int may not satisfy the range requirement and will overflow, turning negative and leading to an infinite loop.

Because the range max of unsigned int is greater than signed int.

This post has been edited by taylorc8: 04 October 2010 - 09:56 AM

Was This Post Helpful? 0
  • +
  • -

#8 lradske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-April 09

Re: vector problem

Posted 04 October 2010 - 01:10 PM

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);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1