I'm going through the Programming Principles and Practice book to teach myself c++ and while doing one of the drills I came across a slight problem. The task is to write a code that will loop one number into a vector and tell the user whether that is the largest number yet, smallest number yet, or neither. I have the smallest number down, but for some reason the a[a.size()] is returning the a[1] value instead of the maximum value.
I added a simple counter to see if everything was going into the array and the a.size() was counting correctly, but for some reason it's not working in the else if function. If anyone could explain to me why this is happening and what I'd need to do to fix the code I'd greatly appreciate it.
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <algorithm>
usingnamespace std;
int main()
{
vector<double> a;
double ball;
cout << "Please give me one number." << endl;
while (cin >> ball)
{
a.push_back(ball);
sort (a.begin(), a.end());
for (int i=0; i<1; ++i)
if (ball<a[1])
cout << '\n' << ball << " This is the smallest number yet!" << endl;
elseif (ball>a[a.size()])
cout << '\n' << ball << " This is the largest number yet!" << endl;
else
cout << "This is just another number" << endl;
}
That's a great tip regarding starting at 0, thank you for that. Unfortunately it's still not working. When I add "a.size()-1" it's treating that as a[0] so it never returns the result "this is the largest number yet." With my original code, instead of returning a[(maximum value)] it's returning a[1] and now is returning a[0].
Here's a thought: before you add the ball to the vector and sort it, check to see if it is smaller than the first or larger than the last. If you add the ball to the vector first, you end up comparing it to itself. (Also, get rid of your for loop that only executes once.)
`a[0]' holds the smallest element
`a[size-1]' holds the largest
¿how do you expect `ball' to be smaller than the smallest or larger than the largest?
> When I add "a.size()-1" it's treating that as a[0]
¿how are you testing that?
> If I don't have the sort
sorting is overkill to figure out the smallest and largest values.
sorting in each iteration is overkill given that the only out of place value is the last one.
> The for (int i=0 etc) is needed
that loop will only run once, always. There is no point to it.
You're correct about the (int i=0) i changed that.
> When I add "a.size()-1" it's treating that as a[0]
¿how are you testing that?
What I meant to say is that it's comparing it to nothing. Sorry I'm new to programming.
> If I don't have the sort
sorting is overkill to figure out the smallest and largest values.
sorting in each iteration is overkill given that the only out of place value is the last one.
Wouldn't that disable the smallest value though? If I didn't sort the new number as the smallest if it is, then it would always consider a[1], whatever that might be, to be the smallest previous value.
¿how do you expect `ball' to be smaller than the smallest or larger than the largest?
Ball is a set of numbers that I just named ball as an input so it would be quick and easy to type. This was originally intended for my eyes only since it was practice.
Here's a thought: before you add the ball to the vector and sort it, check to see if it is smaller than the first or larger than the last. If you add the ball to the vector first, you end up comparing it to itself. (Also, get rid of your for loop that only executes once.)
That sounds like a great idea. I now just have to figure out how to write the if so that it recognizes if it's the first value. Currently the prompt blows up because it's comparing it to nothing. Thanks for your help! I'll keep trying to get this to work and let you know if it does.
> What I meant to say is that it's comparing it to nothing.
There is no `nothing' in c++.
Show your update code, and describe the symptoms that make you think that
> Wouldn't that disable the smallest value though?
no
> then it would always consider a[1]
if you don't sort you'll need another algorithm.
I'm saying that sorting in each iteration is terrible inefficient.
Also, the first element is `a[0]'
> Ball is a set of numbers that I just named ball
I know.
You put `ball' inside the vector, then you sort it. If `ball' were the smaller then it would end on `a[0]' it cannot be smaller than itself.
It's comparing it to an imaginary number then; one that was never created. I don't know what other algorithms there are than sort for now. I'm still extremely new to programming.
This is my new code but when I enter a number it just crashes because it's not comparing it to an original number. So I have to find a way to store a[0]'s value before running it. Doing a[0]=50 before the if statement or before the while causes it to crash as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
vector<double> a;
double ball;
cout << "Please give me one number." << endl;
while (cin >> ball)
{
if (ball<a[1])
cout << '\n' << ball << " This is the smallest number yet!" << endl;
elseif (ball>a[a.size()-1])
cout << '\n' << ball << " This is the largest number yet!" << endl;
else
cout << "This is just another number" << endl;
a.push_back(ball);
sort (a.begin(), a.end());
}
}
I got it to work. The problem was that I had to do a.size()-2 because if I sorted it before that, a.size()-1 would be the ball if it's the biggest number. So now a.size()-2 compares ball to the previously biggest number. Thanks everyone for your help!