help with vector

Hello all and thanks for the help ahead of time I'm in need of a little help. I need to edit the program below to only bring back the highest GPA with all the other information attached. I think its pretty simple to get the highest GPA by just having the fillVector() hold this little GPA > x then x = GPA (if i am wrong feel free to correct me), but idk how to attach all other information with that any hints would be very much appreciated( I suspect there is a way to save the point in the vector rather than just the information saved in gpa, but have no clue as how to do it).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include<vector>
using namespace std;



class student
{
	private:
		string studentName;
		double studentGpa;
		int studentRank, studentYear;
	
	public:
		void setName(string name)
		{
			studentName = name;
		}
		void setGpa(double gpa)
		{
			studentGpa = gpa;
		}
		void setRank(int rank)
		{
			studentRank = rank;
		}
		void setYear(int year)
		{
			studentYear = year;
		}
		string getName() const
		{
			return studentName;
		}
		double getGpa() const
		{
			return studentGpa;
		}
		int getRank() const
		{
			return studentRank;
		}
		int getYear() const
		{
			return studentYear;
		}
};

void fillVector(vector<student>&);
void printVector(const vector<student>&);

int main()
{	
	vector<student> allStudents;
	
	fillVector(allStudents);
	printVector(allStudents);
	
	return 0;
}

void fillVector(vector<student> & newAllStudents)
{
	string name;
	double gpa;
	int year, rank;
	
	cout << "How many students are there: " << endl;
	int numStudents;
	cin >> numStudents;
	
	for (int i = 0; i < numStudents; i++)
	{
		cout << "Enter student name: " << endl;
		cin >> name;
		cout << "Enter student GPA: " << endl;
		cin >> gpa;
                    if (gpa > x)
                    {
                        gpa = x;
                    }
		cout << "Enter student year: " << endl;
		cin >> year;
		cout << "Enter student rank: " << endl;
		cin >> rank;
		
		student newStudent;
		newStudent.setName(name);
		newStudent.setGpa(gpa);
		newStudent.setYear(year);
		newStudent.setRank(rank);
		newAllStudents.push_back(newStudent);
	}
	
}

void printVector(const vector<student> & newAllStudents)
{	 
	unsigned int size = newAllStudents.size();
			
	cout << "The studens who qualify for the scholarship are: " << endl;
	
	for (unsigned int i = 0; i < size; i++)	
	{
		if (newAllStudents[i].getGpa() >= 3.0)
		{
			cout << "Student name: " << newAllStudents[i].getName() << endl;
			cout << "Student GPA: " << newAllStudents[i].getGpa() << endl;
			cout << "Student year and rank: " << newAllStudents[i].getYear() << "(" << newAllStudents[i].getRank() << ")" << endl << endl;
		}
	}
}
There are many ways such a problem can be solved.

One of the simplest ways to find the highest gpa is to have a separate function that will do the following:

1) Initialize a gpa_comparison field to 0.

2) In a loop, do the following:
a) Check if the current student's gpa is higher than the gpa_comparison field.
b) If not, consider the next student.
c) If it is, save the gpa into the gpa_comparison field.

3) At the end of the previous loop, you now have saved the highest gpa. In another loop, iterate through the vector, and for each student with this gpa, print all the attributes associated with that student.

Of course, this algorithm is inefficient, since it requires multiple iterations to scroll through the data.

An alternative is to use a multiset<> container and populate it, instead of a vector. Then you will get an automatic sort gpa-wise, since a multiset<> is an ordered container.
so something like this?

1
2
3
4
5
6
7
8
9
10
11
12
void printHighestGpa()
{
double highestGpa = 0;

for (unsigned int i = 0; i < size; i++)
{
     if (newAllStudenst[i].getGpa() > highestGpa)
     {
      newAllStudent[i].getGpa() = highestGpa;      /* then i get stumped I know i will return the highestGpa but what about name and year and rank? Do I just set up the other info in the if loop as well?*/
      }
}
}
sorry i know i didn't put anything to print it but im more curious about the information I can print it pretty easiely
well this is what I got but I ended up with it returning the whole vector any ideas what i did wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void printHighestGpa(const vector<student> & newAllStudents)
{	 
	unsigned int size = newAllStudents.size();
	double highestGpa = 0;
	string winnerName;
	int winnerYear, winnerRank;
			
	cout << "The student(s) who won the scholarship is: " << endl;
	
	for (unsigned int i = 0; i < size; i++)	
	{
		if (newAllStudents[i].getGpa() > highestGpa)
		{
			highestGpa = newAllStudents[i].getGpa();
			winnerName = newAllStudents[i].getName();
			winnerYear = newAllStudents[i].getYear();
			winnerRank = newAllStudents[i].getRank();
			
			cout << "Student name: " << winnerName << endl;
			cout << "Student GPA: " << highestGpa << endl;
			cout << "Student year and rank: " << winnerYear << "(" << winnerRank << ")" << endl << endl;
		}
	}
}
Last edited on
also ive been researching this question off site and ran into *min_element and *max_element would this be an option for this program? Only reason I ask is because i see everyone using arrays with this but no vectors.
Last edited on
1
2
3
cout << "Student name: " << winnerName << endl;
			cout << "Student GPA: " << highestGpa << endl;
			cout << "Student year and rank: " << winnerYear << "(" << winnerRank << ")" << endl << endl;


I see you output the results EVERY time that you find a new highest value. Perhaps you should wait until you've gone right through the vector and THEN output just once.
Last edited on
Thanks Repeater thats exactly the hint i needed I moved the cout statements outside the for loop and it works great again thanks so very much to all that helped me!
Completed Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include<vector>
using namespace std;



class student
{
	private:
		string studentName;
		double studentGpa;
		int studentRank, studentYear;
	
	public:
		void setName(string name)
		{
			studentName = name;
		}
		void setGpa(double gpa)
		{
			studentGpa = gpa;
		}
		void setRank(int rank)
		{
			studentRank = rank;
		}
		void setYear(int year)
		{
			studentYear = year;
		}
		string getName() const
		{
			return studentName;
		}
		double getGpa() const
		{
			return studentGpa;
		}
		int getRank() const
		{
			return studentRank;
		}
		int getYear() const
		{
			return studentYear;
		}
};

void fillVector(vector<student>&);
void printHighestGpa(const vector<student>&);

int main()
{	
	vector<student> allStudents;
	
	fillVector(allStudents);
	printHighestGpa(allStudents);
	
	return 0;
}

void fillVector(vector<student> & newAllStudents)
{
	string name;
	double gpa;
	int year, rank;
	
	cout << "How many students are there: " << endl;
	int numStudents;
	cin >> numStudents;
	
	for (int i = 0; i < numStudents; i++)
	{
		cout << "Enter student name: " << endl;
		cin >> name;
		cout << "Enter student GPA: " << endl;
		cin >> gpa;
		cout << "Enter student year: " << endl;
		cin >> year;
		cout << "Enter student rank: " << endl;
		cin >> rank;
		
		student newStudent;
		newStudent.setName(name);
		newStudent.setGpa(gpa);
		newStudent.setYear(year);
		newStudent.setRank(rank);
		newAllStudents.push_back(newStudent);
	}
	
}

void printHighestGpa(const vector<student> & newAllStudents)
{	 
	unsigned int size = newAllStudents.size();
	double highestGpa = 0;
	string winnerName;
	int winnerYear, winnerRank;
			
	cout << "The student(s) who won the scholarship is: " << endl;
	
	for (unsigned int i = 0; i < size; i++)	
	{
		if (newAllStudents[i].getGpa() > highestGpa)
		{
			highestGpa = newAllStudents[i].getGpa();
			winnerName = newAllStudents[i].getName();
			winnerYear = newAllStudents[i].getYear();
			winnerRank = newAllStudents[i].getRank();
		}

	}
	cout << "Student name: " << winnerName << endl;
	cout << "Student GPA: " << highestGpa << endl;
	cout << "Student year and rank: " << winnerYear << "(" << winnerRank << ")" << endl << endl;
}
Actually, there is a serious bug in your logic and it won't work correctly at all.

You are trying to obtain the highest Gpa and print it (along with the other values) in a single iteration. If your I/P data 's 1st student contains a gpa of 4.5 and 2nd student a gpa of 4.6, both will be printed.

You must have 2 iterations:
The 1st iteration will return the highest gpa.
In the 2nd iteration, compare this returned value with the gpa of each student. If it matches, print it out.

Here's the corrected program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include<vector>

using namespace std;


class student
{
	private:
		string studentName;
		double studentGpa;
		int studentRank, studentYear;
	
	public:
		void setName(string name)
		{
			studentName = name;
		}
		void setGpa(double gpa)
		{
			studentGpa = gpa;
		}
		void setRank(int rank)
		{
			studentRank = rank;
		}
		void setYear(int year)
		{
			studentYear = year;
		}
		string getName() const
		{
			return studentName;
		}
		double getGpa() const
		{
			return studentGpa;
		}
		int getRank() const
		{
			return studentRank;
		}
		int getYear() const
		{
			return studentYear;
		}
};


void fillVector(vector<student>&);
double getHighestGpa(const vector<student>&);
void printHighestGpa(const vector<student>&, const double& hiGpa);


int main()
{	
	vector<student> allStudents;
	
	fillVector(allStudents);
	
	double hiGpa = getHighestGpa(allStudents);
	
	printHighestGpa(allStudents, hiGpa);
	
	return 0;
}


void fillVector(vector<student> & newAllStudents)
{
	string name;
	double gpa;
	int year, rank;
	
	cout << "How many students are there: " << endl;
	int numStudents;
	cin >> numStudents;
	
	for (int i = 0; i < numStudents; i++)
	{
		cout << "Enter student name: " << endl;
		cin >> name;
		cout << "Enter student GPA: " << endl;
		cin >> gpa;
		cout << "Enter student year: " << endl;
		cin >> year;
		cout << "Enter student rank: " << endl;
		cin >> rank;
		
		student newStudent;
		newStudent.setName(name);
		newStudent.setGpa(gpa);
		newStudent.setYear(year);
		newStudent.setRank(rank);
		newAllStudents.push_back(newStudent);
	}
	
}


double getHighestGpa(const vector<student>& newAllStudents)
{	 
	unsigned int size = newAllStudents.size();
	double highestGpa = 0;

	for (unsigned int i = 0; i < size; i++)	
	{
		if (newAllStudents[i].getGpa() > highestGpa)
		{
			highestGpa = newAllStudents[i].getGpa();
		}

	}

    return highestGpa;
}	
	

void printHighestGpa(const vector<student>& newAllStudents,
                     const double& hiGpa)
{	 
	unsigned int size = newAllStudents.size();
	double highestGpa = 0;

	cout << "The student(s) who won the scholarship is/are: " << endl;
	
	for (unsigned int i = 0; i < size; i++)	
	{
		if (newAllStudents[i].getGpa() == hiGpa)
		{
           cout << "Student name: " << newAllStudents[i].getName() << endl;
	       cout << "Student GPA: " << hiGpa << endl;
	       cout << "Student year and rank: " 
	            << newAllStudents[i].getYear() << "(" 
	            << newAllStudents[i].getRank() << ")" 
	            << endl << endl;
	    }
	}
}


I've compiled it successfully but not tested it; but it should work:
https://ideone.com/E8zXfn

Ill run the program i have created again and see if it does this with 3.x and 2.xas a student shouldnt have a gpa over 4.0. If I was writing this program for a purpose I would include error checking to be sure the user only inputs a number between 0.1 and 4.0 but this is just a school project and the teacher isnt expecting it as the time constraints we are under.
Last edited on
I ran my newest code here with the parameters you set up but I cannot replicate and get the student who won the scholarship = the one with a gpa of 4.6 only not the 4.5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include<vector>
using namespace std;



class student
{
	private:
		string studentName;
		double studentGpa;
		int studentRank, studentYear;
	
	public:
		void setName(string name)
		{
			studentName = name;
		}
		void setGpa(double gpa)
		{
			studentGpa = gpa;
		}
		void setRank(int rank)
		{
			studentRank = rank;
		}
		void setYear(int year)
		{
			studentYear = year;
		}
		string getName() const
		{
			return studentName;
		}
		double getGpa() const
		{
			return studentGpa;
		}
		int getRank() const
		{
			return studentRank;
		}
		int getYear() const
		{
			return studentYear;
		}
};

void fillVector(vector<student>&);
void printHighestGpa(const vector<student>&);

int main()
{	
	vector<student> allStudents;
	
	fillVector(allStudents);
	printHighestGpa(allStudents);
	
	return 0;
}

void fillVector(vector<student> & newAllStudents)
{
	string name;
	double gpa;
	int year, rank;
	
	cout << "How many students are there: " << endl;
	int numStudents;
	cin >> numStudents;
	
	for (int i = 0; i < numStudents; i++)
	{
		cout << "Enter student name: " << endl;
		cin >> name;
		cout << "Enter student GPA: " << endl;
		cin >> gpa;
		cout << "Enter student year: " << endl;
		cin >> year;
		cout << "Enter student rank: " << endl;
		cin >> rank;
		
		student newStudent;
		newStudent.setName(name);
		newStudent.setGpa(gpa);
		newStudent.setYear(year);
		newStudent.setRank(rank);
		newAllStudents.push_back(newStudent);
	}
	
}

void printHighestGpa(const vector<student> & newAllStudents)
{	 
	unsigned int size = newAllStudents.size();
	double highestGpa = 0;
	string winnerName;
	int winnerYear, winnerRank;
			
	cout << "The student(s) who won the scholarship is: " << endl;
	
	for (unsigned int i = 0; i < size; i++)	
	{
		if (newAllStudents[i].getGpa() > highestGpa)
		{
			highestGpa = newAllStudents[i].getGpa();
			winnerName = newAllStudents[i].getName();
			winnerYear = newAllStudents[i].getYear();
			winnerRank = newAllStudents[i].getRank();
		}

	}
	cout << "Student name: " << winnerName << endl;
	cout << "Student GPA: " << highestGpa << endl;
	cout << "Student year and rank: " << winnerYear << "(" << winnerRank << ")" << endl << endl;
}
Last edited on
Can you display the I/P and O/P ?
can you specify what you are saying as i cant understand sorry
Topic archived. No new replies allowed.