Pages: 123
Thanks a bunch Zeroes, that was really helpful! one more thing though how do I print the second largest number?
I think I need to use syntax similar to something like this
numbers.end()-1;
 sort descending Order obtained by using a simple insertion algorithm to classify moving and print largest 2nd element.

Hmm. Looks like you may not be allowed to use std::sort()?
http://en.wikipedia.org/wiki/Insertion_sort

Otherwise, to sort in descending order, use `sort(v.rbegin(), v.rend());` instead.

Edit: I'm a bit confused now. Do you need to write your own sorting function?
Last edited on
There is no mention of function, so I'm assuming no.
Well then, after you sorted in descending order, the second largest element is the second element in the vector, which is `v[1]` (or if you prefer, `v.at(1)`).

Edit: oops... the above isn't true if the first element repeats itself, like:
 `90 90 90 80 66 3 2 1`
Last edited on
 ``12`` ``````cout<<"==\n"; cout<

Used this
it prints second lowest number
so I'm assuming I need to write numbers.at(9);?
 so I'm assuming I need to write numbers.at(9);?

That will print the 10th element of the vector. What do you want to do?
closed account (3qX21hU5)
This should help you find the second largest if you sort in descending order, meaning you do this `sort(v.rbegin(), v.rend());`

 ``123456789101112131415161718`` `````` int second; // Holds the second largest number int index = 1; // Used to iterate through the vector while (true) { // If the second element is the same as the first // it isn't the second largest number so we go to the // next element, and so on untill it is not egual to the element // before it. if (v[index] == v[index-1]) index++; else { second = v[index]; break; } } cout << "Second Largest: " << second << endl;``````

We do this because we could have something likes this for our numbers

10 - First Element
10 - Second Element
10 - Third Element
9 - Fourth Element - This one will be the second largest
4
3
1

So if we just told the program to print the second element in the vector it would print '10' which as we can see is not the second highest number.
Last edited on
Zeroes I simply added numbers.at(8) as suggested by catfish and it works out fine, what is the advantage of using that code instead of this?
Ha ha, competing with Zereo for first to reply!

Firstly:
 ``12`` ``````sort(numbers.begin(), numbers.end()); // sorts numbers in ascending order sort(numbers.rbegin(), numbers.rend()); // sorts numbers in descending order ``````

Secondly:
What if some of the numbers repeat? In particular, what if the largest repeats? That would mean your "second largest" would actually wrongly be the original largest.

At this point I suggest writing a function to display your numbers vector.
Last edited on
closed account (3qX21hU5)
Because you are not getting the second highest number all the time. Check out my example above that I edited in.

If there are multiple 10's and 10 is the highest number you will not be printing the second largest number.

EDIT: Lol damn you Catfish hes my noob to help ;p (Joking am0n ;p)
Last edited on
I'm a little confused now sorry, Catfish I used the code you gave me but how do I display the result(second highest number)?
Haha yeah, I'm a noob, anyway here's the code
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include #include #include #include using namespace std; int main() { vector numbers; srand(time(NULL)); for (int i = 0; i != 10; ++i) { int randomInt = rand() % 20 + -10; cout << randomInt << endl; numbers.push_back(randomInt); } cout << "=====================================\n"; sort(numbers.begin(), numbers.end()); for (int i =0; i != numbers.size(); ++i) cout << numbers[i] << endl; cout<<"==\n"; sort(numbers.begin(), numbers.end()); system("pause"); return 0; ; }``````

Would this work?
closed account (3qX21hU5)
Nope check out our posts before to see why. You were almost there with the code you had before except for one thing which is what happens if the highest number gets repeated twice, or more? Basically delete everything after line 23 and then post the code I pasted in there where you just deleted everything. Except change everything with v[] to numbers [] obviously

Make sure you just don't copy and paste and call it good though. Make sure you study it and see what it is doing and understand why you need to do it. Catfish explains why you need to do it well in his recent post.

P.S Like your formatting a lot better now great improvement on that.
Last edited on
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include #include #include #include #include using namespace std; int main() { vector numbers; srand(time(NULL)); for (int i = 0; i != 10; ++i) { int randomInt = rand() % 20 + -10; cout << randomInt << endl; numbers.push_back(randomInt); } cout << "=====================================\n"; sort(numbers.begin(), numbers.end()); for (int i =0; i != numbers.size(); ++i) cout << numbers[i] << endl; cout<<"==\n"; int second; int index = 8; while (true) { if (numbers[index] == numbers[index-1]) index++; else { second = numbers[index]; break; } } cout << "Second Largest: " << second << endl; system("pause"); return 0; ; }``````
closed account (3qX21hU5)
Looks good to me except you need to change `numbers[index-1]` to `numbers[index+1]` and `index++;` to `--index;` and your formatting a bit at the end.

I would also recommend you keep running the program again and again to keep checking the results to see if there are any bugs. Otherwise you did good and made some major improvements great job.

Last edited on
Darn, I'm getting the same error(I tried with duplicate numbers and it's printing out the 8th number not the actual second one)
closed account (3qX21hU5)
That is because you had the formula wrong I edited my previous post with the corrections since you change my original one to 8 instead of 1 which means you have to reverse everything.

Anyways finally time to get off work so won't be able to reply for awhile. If you do run into anymore troubles just post it here and i'm sure someone else would be more then willing to help.
Last edited on
WOW Zereo,catfish3 I can't thank you enough! This is great, thank you, you just saved me around \$400 + 6 months of C++ coursed with crappy teacher!
Last edited on
You need to sort in descending order. You do that by:

`sort(numbers.rbegin(), numbers.rend());`

Then you can use Zereo's original code and your program should work fine.

Edit: oops, too late this time.
Last edited on
Here is how the final code looks like
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include #include #include #include #include using namespace std; int main() { vector numbers; srand(time(NULL)); for (int i = 0; i != 10; ++i) { int randomInt = rand() % 20 + -10; cout << randomInt << endl; numbers.push_back(randomInt); } cout << "=====================================\n"; sort(numbers.begin(), numbers.end()); for (int i =0; i != numbers.size(); ++i) cout << numbers[i] << endl; cout<<"==\n"; int second; int index = 8; while (true) { if (numbers[index] == numbers[index+1]) index--; else { second = numbers[index]; break; } } cout << "Second Largest: " << second << endl; system("pause"); return 0; ; }``````

I think it's okay
Pages: 123