EvilZone

Programming and Scripting => C - C++ => : Drahgon December 18, 2015, 08:23:48 PM

: Ordering Numbers into Ascending Order
: Drahgon December 18, 2015, 08:23:48 PM
Hey, so a few posts ago I showed some code regarding ordering numbers into ascending order. I discovered the bubble-sort algorithm and after being sent this code by a user on this forum, decided to see if it worked. However it doesn't output anything.

I have absolutely no idea why as the for loop at the bottom should output all the numbers ... but for some reason it doesn't (or at least 1 number).

(https://gyazo.com/a5a82040bb8a524e592def3c1d7c6683.png)

This should work .. the first loop checks the value of the first number against the value of the second and then the third etc.
The outer for loop counter then makes it reset the j counter inside and so starts all over again. There are 100 checks on the various numbers in the array 's'.

Then at the end the for loop will simply print out the array 's' based on the value of 'i' at each of the loop counts. I fail to see what is actually going wrong here.

This is the console window:

(https://gyazo.com/00e3249d2677dec854008d0afd2ce767.png)

As you can see, the array seems to get stored and potentially sorted.. but then the program just ends.

Any help would be great :)
: Re: Ordering Numbers into Ascending Order
: Kurajber December 18, 2015, 09:48:41 PM
Hello again.

Glad to see your progress.

Your problem is in your for loops, you don't declare starting value for i. After the first loop, i has a value of 10, so other two loops don't even start.
: Re: Ordering Numbers into Ascending Order
: kenjoe41 December 18, 2015, 11:22:16 PM
In all yoooour looooops, you never declare a starting value for you palceholders come on, really? Get to work boy.

And next time, don't use images, post the code here, i care less now above your BBcode coloring but do post it.
: Re: Ordering Numbers into Ascending Order
: Drahgon December 19, 2015, 02:20:32 AM
Hello again.

Glad to see your progress.

Your problem is in your for loops, you don't declare starting value for i. After the first loop, i has a value of 10, so other two loops don't even start.

Thanks, its been going well(ish). I see ok .. but I declare that starts from 0 at the top ? Do I have to purely declare it's value locally?
: Re: Ordering Numbers into Ascending Order
: Drahgon December 19, 2015, 02:21:57 AM
In all yoooour looooops, you never declare a starting value for you palceholders come on, really? Get to work boy.

And next time, don't use images, post the code here, i care less now above your BBcode coloring but do post it.

As I said to the post above, I declared the value within main's scope. Not sure why i have to declare it locally.
: Re: Ordering Numbers into Ascending Order
: spaceman December 19, 2015, 07:41:03 AM
Before first loop you set i to 0 ok but sice first for loop i changed from 0 to 9. So if you want use i again in other loops as counter set it back to 0. Or use diferent counter.
: Re: Ordering Numbers into Ascending Order
: Kurajber December 19, 2015, 09:52:30 AM
Thanks, its been going well(ish). I see ok .. but I declare that starts from 0 at the top ? Do I have to purely declare it's value locally?

Basically, the for loops work by increasing the given value while given condition is true. So, your condition being i<10, when i reaches 10, the loop stops. So, as I said, after the first loop, that is the value of i.

Most commonly, the initial value is declared when you call the loop, eg.
:
for(x=1; x<=10; x++)Take a look at this: http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

I still feel that you're getting ahead of yourself, I suggest you again to try to actually understand how basic stuff works before trying to do stuff.
: Re: Ordering Numbers into Ascending Order
: Drahgon December 19, 2015, 07:59:47 PM
Basically, the for loops work by increasing the given value while given condition is true. So, your condition being i<10, when i reaches 10, the loop stops. So, as I said, after the first loop, that is the value of i.

Most commonly, the initial value is declared when you call the loop, eg.
:
for(x=1; x<=10; x++)Take a look at this: http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

I still feel that you're getting ahead of yourself, I suggest you again to try to actually understand how basic stuff works before trying to do stuff.

So I finally got it to work ! Thanks for all the help