0

I'm trying to find the smallest value in 3 different array but it's not turning out right I don't think. It never seems to return the middle array even if it is the smallest. It's always either 0 or 2. What seems to be my logic error?

int smallest;
for(int i = 1; i < 3; i++)
{
    if(queue[i].getCount() < queue[0].getCount())
      smallest = i;
    else
      smallest = 0;
}
1
  • 4
    You compare to the entry at position 0, instead of the current value of queue[smallest].getCount() in the loop. (After making the correction, don't forget to initialize smallest properly in the declaration.) Commented Dec 12, 2012 at 5:56

1 Answer 1

1

Given the errors in your code, it's a little difficult to see what you are trying to do.

I would think you want something more like this:

int smallest = queue[0].getCount();
for(int i = 1; i < 3; i++)
{
    if(queue[i].getCount() < smallest)
        smallest = queue[i].getCount();
}

If you instead want the resulting index, try something like this:

int smallest = 0;
for(int i = 1; i < 3; i++)
{
    if(queue[i].getCount() < queue[smallest].getCount())
        smallest = i;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I'm actually getting the index of the smallest queue. Or The Index of the Queue with the least amount of objects in it.
I've added another version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.