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;
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:
This should help you find the second largest if you sort in descending order, meaning you do this sort(v.rbegin(), v.rend());
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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.
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.
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.
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.
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.
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!
Also, what about the formatting?