![]() |
|
#1
|
|||
|
|||
Recursive functions using pointershi ,
im writing this(recursive) code for the quick sort algorithm and I'm using pointers as the function parameters. but the problem Im having is that the swap function which i call just won't change the variables places in the global array I've declared. I'm using refrenced parameters in this swap function , and i have no idea why it won't change their places. here's the code: CPP / C++ / C Code:
thanks in advance |
|
#2
|
|||
|
|||
|
Quote:
When I have a function that I suspect is not working, I make a test program that just calls that function and does nothing else except print the results: CPP / C++ / C Code:
My output: Code:
So the function does seem to work. Now as to your program: what made you think that the problem was the swap() function? When I tried your program it gave this: Code:
It never printed the sorted array. What could have happened? Well, here's how I start to debug things like this: First of all, I usually put print statements around function calls (especially recursive ones): in main: CPP / C++ / C Code:
Now the output that I get is this: Code:
So, as I suspected, it's never returning back to main() from quicksort(). (The function probably calls itself and calls itself and calls itself ... until the program runs out of stack space to hold return addresses, then the program unceremoniously quits --- no message or anything, just returns.) Now,to check this, I made a global variable named counter and initialized it to zero: CPP / C++ / C Code:
Then, in quicksort(), I put this at the beginning: CPP / C++ / C Code:
Try it and see what it tells you. (Ctrl-C makes the madness stop.) If you want to "single-step" through the infinite loop, you can put cin.get() after the cout statement. Now it stops each time and waits for you to press "Enter" before continuing. Look at the first few times then hit Ctrl-C to exit. If you need to drill down into lower functions, put printout statements as each one is called, etc. The idea is to make the program tell you exactly what it is doing, then you should be make it do the right thing. Regards, Dave |
|
#3
|
||||
|
||||
|
your swap function is fine but the arguments you're passing are not. I'm not sure what the problem is but try passing "start" and "end" by reference instead pointer like this
Code:
|
|
#4
|
|||
|
|||
|
hi,
i'm compiling with vc++ , and i started debugging line to line , i mean by using F10 and F11, after the swap function was called, the variables in the array weren't changed in the output window(they were changed inside the swap function but the array stayed the same) .so this is how i (think )i know thats where the problem occurs... so i wrote a similar program , just like the one u wrote above ,but that answered , and i though maybe it's somthing i don't know , the pointers are pointing to the correct places , and the swap function does seem to be swaping the variables , but the array stays the same . it seems to be constant . but i'll go try what u said ,(both of u) and see what happens. thanks again. |
|
#5
|
||||
|
||||
|
Quote:
CPP / C++ / C Code:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#6
|
|||
|
|||
|
Quote:
No. He is passing by reference (a and b are ints, not pointers). I personally would have used pointers as arguments, but I didn't want to impose my sense of style on his, since I didn't want to make him change his program only to find that it had no bearing on his problem. Your snippet won't compile in any standard C++ compiler, since it is trying to de-reference the un-de-referenceble. (And C compilers generally don't allow pass-by-reference, so they wouldn't even begin to compile it.) My previous example, changed so that swap uses pointers, becomes the following: (I wouldn't normally dream of second-guessing anyone, but this time I am guessing that this is kind of what you had in mind.) CPP / C++ / C Code:
The output: Code:
So one way works as well as the other. Regards, Dave (Both Stroustrup and I believe that gratuituous use of pass-by-reference can be hazardous to your mental health. Just because you can pass by reference doesn't mean that you should pass by reference. It's not "wrong", however.) |
|
#7
|
||||
|
||||
|
Quote:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#8
|
||||
|
||||
|
la la, to sum things up:
CPP / C++ / C Code:
__________________
People should read the rules and regulation before posting! The Best is yet to be... |
|
#9
|
||||
|
||||
|
Quote:
Not everyone agrees. But I appreciate most the people who can accept another point of view - like you and Mr. Stroustrup. I'm not decided on this issue myself, but I don't like reading the self-righteous opinions I found all over the Internet - "my way is the way". Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#10
|
|||
|
|||
|
Quote:
I have no idea what u mean, I havn't declared anything , I'm just saying swap the "variables" the pointers are pointing to, derefrence the pointer . if i say: cout<<*startptr<<endl; what would that mean?what would it see out? that is definetly not the problem,what u've said is too basic., and as i said i already wrote another program testing to see if it actually swapped them , and it did. ANYWAY, I think I'm gonna have to work on it a bit more. |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help in c++ recursive functions | vinch | C++ Forum | 7 | 04-Jan-2006 13:22 |
| Endless recursive function | teflon | C++ Forum | 3 | 26-Jul-2005 12:31 |
| Major problem with recursive function, help.. | kakamuti | C Programming Language | 4 | 19-Dec-2004 08:47 |
| Understanding Recursive Functions | Nexa | C++ Forum | 5 | 19-Nov-2004 12:51 |
| recursive algorithm | vienne | C Programming Language | 2 | 21-Jul-2004 12:54 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The