Simple question

Can't figure out why biggest changes every time...it is only supposed to change if ary[i] is greater than value in biggest. What did I do wrong?

 ``123456789101112131415161718192021222324252627`` ``````#include using namespace std; int main() { int ary[10] = {10,5,16,28,55,100,90,14,15,31};// Declare array of 10 int end = 9; // Variable for last position in array int biggest; int pos = 0; // Variable to hold position where largest value is found biggest = ary[0]; // Set first array value to the variable that holds biggest for (int i = 0; i < end; i++) // For n - 1 passes { for (i = 0; i <= end; i++) // For all elements in array { if (ary[i] > biggest); // If biggest element is larger than second element { biggest = ary[i]; // Set that value to 'biggest' pos = (i); // Hold this position # } } ary[pos] = ary[end]; // Once we found biggest # in array, swap end value with ary[end] = biggest; // biggest position's value } for (int p = 0; p <= end; p++) cout << ary[p] << endl; // Print out all elements in array in order now }``````
IMHO you're making it yourself pretty difficult.
(I can't answer your question at once... sorry) but why are you using 2 loops?
Why wouldn't you use just one loop?

Assuming ary[0] is biggest at first, you could use:
 `` `` ``if (ary[i] > biggest) ary[i];``
Oh. Well, because I have to put the whole array in order, so I need to make (n-1) passes through the list (the outer loop) and then for each array, I need to go through each element in the array (inner loop.)

Is that incorrect?
Your using a 'linear sort algorithm', which isn't wrong (but there are more efficient sort algorithms).

Please search the web for 'linear sort algorithm' and you'll find many examples (and more efficient examples also) with good explanations (better than I can... :-))
Oh. Yeah, I know there are more efficient ones, but this is the one I'm supposed to use for the assignment.

I think the problem (or one of them) is that I need to make the end of the searchable part of the array shorter every pass through.
But that still doesn't explain why the if statement in line 16 doesn't seem to work. Why do we go into that loop every time, even when ary[i] < biggest? <-----OH! I bet it's that semicolon!
Last edited on
Your outerloop is iterating only once! In the first iteration of the outerloop, the inner loop runs also, and they share the var 'i'. So when the inner loop is finished, var 'i' has the value 10, which keeps the outerloop from iterating again.

Use some debugging:
 ``12345`` ``````for (int i = 0; i <= end; i++) // For n - 1 passes { cout << "Outerloop: " << i << endl; // .. the inner loop and rest... }``````

EDIT: And your 'swapping' isn't correct!
Last edited on
Yeah, thank you. I did figure out the looping problem, and now I have it working up until the first 2 numbers. I need to fix this last thing...
Okay, it's kind of icky code at the bottom of the outer loop, but it works.

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include using namespace std; int main() { int ary[10] = {10,5,16,28,55,100,90,14,15,31};// Declare array of 10 int end = 9; // Variable for last position in array int biggest; // Variable to hold value of largest # int pos = 0; // Variable to hold position where largest value is found for (int i = 0; i < end; i++) // For n - 1 passes { biggest = ary[0]; // Set first array value to the variable that holds biggest for (i = 0; i <= end; i++) // For all elements in array { if (ary[i] > biggest) // If biggest element is larger than second element { biggest = ary[i]; // Set that value to 'biggest' pos = i; // Hold this position # } else if (ary[1] < ary[0])// I know this is crappy, but I can't figure out how to do it better. { int temp = ary[i+1]; // Sweap the first two elements if second is less than first ary[i+1] = ary[i]; ary[i] = temp; } } ary[pos] = ary[end]; // Once we found biggest # in array, swap end value with ary[end] = biggest; // biggest position's value end--; // Stop including last # in search i = 0; // Now we start over again to find the next largest # } // Now the list we compare is one shorter end = 9; // Reset end back to 9 for (int p = 0; p <= end; p++) // For all elements in array cout << ary[p] << endl; // Print elements } ``````
Topic archived. No new replies allowed.