Jump to content
  • Advertisement
Sign in to follow this  
TommySauder

Vector Trouble

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm doing accelerated C++ exercises, and it asks to display the quarterly-largest numbers in a vector... So I use the sort() function to arrange the vetor from largest to smallest, and to grab the quarterly-largest numbers... only problem it seems that sort() isn't working... here's the source
// Program to print of the quartile of largest numbers
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <ios>

using namespace std;

int main()
{
	// ask user for name
	cout << "Please enter your first name: ";
	string name;
	cin >> name;
	cout << "Hello " << name << "!" << endl;

	vector<int> integers;
	int input;

	cout << "Please enter 12 numbers, followed by end-of-file: ";

	while (cin >> input)
		integers.push_back(input);

	// test to see if the user entered the appropriate amount of numbers

	if (integers.size() < 12) {
		cout << endl << "Please run the program again, 12 integers were not entered. " << endl;

		return 1;
	}

	// rearrage vector from largest to smallest

	sort(integers.begin(), integers.end());

	// hold the quarters in 3 different integers

	int largest;
	int large;
	int small;

	largest = integers[0];
	large = integers[4];
	small = integers[8];

	// print out the values

	cout << "The largest value you entered was: " << largest << endl;
	cout << "The next quarter-largest value you entered was: " << large << endl;
	cout << "The last quarter-largest value you entered was: " << small << endl;

	return 0;
}

I'm a bit confused as to why this isn't working...

Share this post


Link to post
Share on other sites
Advertisement
sort(integers.begin(), integers.end());

Will sort from smallest to largest.

If you want to sort from largest to smallest, you will need:

sort(integers.begin(), integers.end(), greater<int>());

std::greater can be found in the <functional> header.

Passing a third parameter to std::sort is how you specify what sorting criterion should be used. By default, it is std::less, which results in values being sorted from least to greatest: v[0] < v[1] < v[2] ...). If you use std::greater instead, then they will be sorted from greatest to least v[0] > v[1] > v[2] ...

Share this post


Link to post
Share on other sites
Ah, thank you... I guess I went about writting the program wrong because the book hasn't tought us anything about the <functional> header...

I guess I should go back to the drawing boards to find the largest values

any suggestions?

Share this post


Link to post
Share on other sites
Quote:
Original post by TommySauder
any suggestions?


Think about where the smallest, largest, etc, values end up with the current sorting order.

Share this post


Link to post
Share on other sites
I'm thinking of using the max() function defined in <algorithm>...

Since max() only has 2 params. eg: max (e, a), how would it be possible to find the max between 4 of the integers held in the vector?

Share this post


Link to post
Share on other sites
It would be at the end rather than the beginning, but I was just wondering if there's a standard template function that would determine that for me?

I'm really stubborn when getting code to be the most practical and precise.

Share this post


Link to post
Share on other sites
<algorithm> also contain max_element which works on a range. But it is unnecessary if you sort the values, which will be needed anyway to find the quartiles, so using max_element is just doing extra work for nothing. It's just a question of figuring out where to look in the vector.

Edit - there you go [smile]. You can get the last element of a container with the back() member functions (integers.back())

Share this post


Link to post
Share on other sites
Thanks Fruny for your help!

On a side note: I can't believe how fast Accelerated C++ delves into pretty advanced topics right off the bat, compared to Sams Teachyourself C++ in 21 days which has the slowest approach to advanced topics.

I had bought Accelerated C++ and Sams Teachyourself C++ in 21 days about 1 1/2 years ago, skimmed through the Accelerated C++ book and almost had a heart attack, because it claimed to be a beginners book.. so I went on to read the Sams book and got frustrated a bit more than halfway through because I was getting extremely confused.

I guess I'm just trying to say that Accelerated c++ seems to be one hell of a great book (even though I just finished chapter 3).

Share this post


Link to post
Share on other sites
Quote:
Original post by TommySauder
I guess I'm just trying to say that Accelerated c++ seems to be one hell of a great book (even though I just finished chapter 3).


The good thing about Accelerated C++ is it teaches you C++ the right way from the very beginning.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!