Computer Science Canada Bubble sorting?! |
Author: | m84uily [ Tue Mar 10, 2009 3:50 pm ] | ||
Post subject: | Bubble sorting?! | ||
The question is commented in the code so here it is:
This is my first time using bubble sort and I'm really quite confused. I copied the bubble sort part from another post on the forums, and, not surprisingly it's not working out for me. My first and foremost problem would be the error I get when I try to run this code " 'temp' has been previously declared" which I really don't know how to solve. My second problem is, once I get bubble sort working I'm not entirely sure of how to display the numbers in the sorted order. Thanks in advance and all help is very much appreciated. |
Author: | Insectoid [ Tue Mar 10, 2009 4:10 pm ] | ||||||||
Post subject: | RE:Bubble sorting?! | ||||||||
What are the A, b, c, d, e... variables for? the variable 'temp' is being declared more than once. You have
in there more than once, or in a loop/conditional. First, make an variable to hold the array length.
Now, declare your array or random numbers.
Use a for loop to initialize the array now, a bubble sort works like so.
then use a for loop to display the result! You'll have to figure out the %swap part yourself. |
Author: | m84uily [ Tue Mar 10, 2009 4:37 pm ] | ||||
Post subject: | Re: Bubble sorting?! | ||||
I think I understand what you mean by swap, and I'm trying to use temp to do so. However at the line
I receive the error "Variable has no value" How can I fix this? Also! variables a - k are so I can work with them outside the for, because the question asks me to display the unordered numbers also. |
Author: | Tony [ Tue Mar 10, 2009 5:12 pm ] |
Post subject: | RE:Bubble sorting?! |
start by fixing your variable names. Then actually assign some value to it. |
Author: | m84uily [ Tue Mar 10, 2009 5:36 pm ] | ||||||
Post subject: | Re: Bubble sorting?! | ||||||
Quote: start by fixing your variable names. Then actually assign some value to it.
Well, if by fixing variable names you mean, they're silly, then I understand, otherwise I don't really. Could you please expand a little more on this? And I'd assumed I'd assigned a value to variable bleh in the lines:
and count is defined in the previous for statement in the lines:
So, I'm not entirely sure where the missing value is, although I'm certain you're right as that is the problem. Here is my code as a whole as it is now (including comments I have for myself):
|
Author: | Tony [ Tue Mar 10, 2009 5:43 pm ] | ||
Post subject: | Re: Bubble sorting?! | ||
Yes, make your variable names meaningful. I don't know what you are talking about, when you use variable "bleh" m84uily @ Tue Mar 10, 2009 5:36 pm wrote: And I'd assumed I'd assigned a value to variable bleh in the lines:
What's the value of bleh(1) then? |
Author: | m84uily [ Tue Mar 10, 2009 6:01 pm ] | ||||||
Post subject: | Re: Bubble sorting?! | ||||||
Tony @ Tue Mar 10, 2009 5:43 pm wrote: Yes, make your variable names meaningful. I don't know what you are talking about, when you use variable "bleh"
m84uily @ Tue Mar 10, 2009 5:36 pm wrote: And I'd assumed I'd assigned a value to variable bleh in the lines:
What's the value of bleh(1) then? Thank you! I understand now. Heh, just took a simple question to make it click. The sorter looks like this now:
Which executes and such, although I'm not 100% it's working. I had to take out the random that use to be there, shown in the code here:
Whether or not it needs to be there I'm not sure. Ultimately, to decipher if it's working properly I need to know how to display the ordered results? (Don't worry, I'll change the variables at the end Tony ![]() |
Author: | m84uily [ Tue Mar 10, 2009 6:24 pm ] | ||
Post subject: | Re: Bubble sorting?! | ||
It can now execute with the random in it:
Still not sure how to display, though. |
Author: | m84uily [ Tue Mar 10, 2009 6:39 pm ] | ||
Post subject: | Re: Bubble sorting?! | ||
HAH! Got it! Hail Satan! Here's the nearly completed code (still need to change variables and do some other stuff the question asks of me) :
|
Author: | Tony [ Tue Mar 10, 2009 6:39 pm ] |
Post subject: | RE:Bubble sorting?! |
I'm not quite sure -- what are you trying to display (and where, in your program, would that data be found)? |
Author: | Insectoid [ Tue Mar 10, 2009 7:37 pm ] |
Post subject: | RE:Bubble sorting?! |
To display the list in original order, just us a for loop and put statements before you sort it. |
Author: | Tallguy [ Tue Mar 10, 2009 7:49 pm ] |
Post subject: | RE:Bubble sorting?! |
check this topic out, it may help http://compsci.ca/v3/viewtopic.php?t=20423 |