Author Topic: Ordering Numbers into Ascending Order  (Read 1158 times)

0 Members and 1 Guest are viewing this topic.

Offline Drahgon

  • /dev/null
  • *
  • Posts: 14
  • Cookies: -10
    • View Profile
Ordering Numbers into Ascending Order
« on: 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).



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:



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

Any help would be great :)

Offline Kurajber

  • Serf
  • *
  • Posts: 43
  • Cookies: 7
  • Don't Drink and Root
    • View Profile
Re: Ordering Numbers into Ascending Order
« Reply #1 on: 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.
« Last Edit: December 18, 2015, 09:51:25 pm by Kurajber »
0000010100100000

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: Ordering Numbers into Ascending Order
« Reply #2 on: 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.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline Drahgon

  • /dev/null
  • *
  • Posts: 14
  • Cookies: -10
    • View Profile
Re: Ordering Numbers into Ascending Order
« Reply #3 on: 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?

Offline Drahgon

  • /dev/null
  • *
  • Posts: 14
  • Cookies: -10
    • View Profile
Re: Ordering Numbers into Ascending Order
« Reply #4 on: 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.

Offline spaceman

  • Serf
  • *
  • Posts: 27
  • Cookies: -5
    • View Profile
Re: Ordering Numbers into Ascending Order
« Reply #5 on: 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.

Offline Kurajber

  • Serf
  • *
  • Posts: 43
  • Cookies: 7
  • Don't Drink and Root
    • View Profile
Re: Ordering Numbers into Ascending Order
« Reply #6 on: 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.
Code: [Select]
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.
« Last Edit: December 19, 2015, 09:53:56 am by Kurajber »
0000010100100000

Offline Drahgon

  • /dev/null
  • *
  • Posts: 14
  • Cookies: -10
    • View Profile
Re: Ordering Numbers into Ascending Order
« Reply #7 on: 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.
Code: [Select]
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