![]() |
|
#1
|
|||
|
|||
Major problem with recursive function, help..Well guys I must say that I now have a pretty good feeling for C and understand a lot of its components very well except functions and especially recursive functions. I have this assignment that asks to write a recursive function , which takes an int as argument. The function should print :
a. ‘n’ lines with a * and a count decreasing from n to 1, then b. ‘n’ lines with a # and a count increasing from 1 to n For n = 3, the output should look like: * 3 * 2 3 lines with a * and count going from 3 to 1. * 1 # 1 # 2 3 lines with a # and count going from 1 to 3. # 3 Make sure you have appropriate base case to prevent infinite recursion The code that I am attempting to put together is this.. CPP / C++ / C Code:
It says "Too few actual parameters" at the call of the recursive print function in the main. Im sure I am doing something wrong here, I need some help with this if you can, I'd appreciate it. Last edited by LuciWiz : 15-Dec-2004 at 01:31.
Reason: Please insert your c code between [c] & [/c] tags
|
|||
|
#2
|
||||
|
||||
|
Hasn't anyone mentioned CODE TAGS yet? Ahhh, I see twice...
__________________
Definition: Politics Latin, from poly meaning many and tics meaning blood sucking parasites -- Tom Smothers |
|
#3
|
||||
|
||||
|
Hello kakamuti. The specific error that you are getting is right here:
CPP / C++ / C Code:
Also, your function has nothing to do with recursion except for its name ![]() A recursive function is one that calls itself and kills your stack A good example of using a recursive function is a binary tree. Here is a thread that talks about a binary tree and gives a recursive function example.Lastly, this is a problem as well: CPP / C++ / C Code:
C can only return one item. If you need to have the values of i & j passed back, then you need to pass these as reference parameters and not value parameters. __________________
The best damn Sports Blog period. |
|
#4
|
|||
|
|||
|
Ok DSmith, I defenetly messed up on that first program I posted. I put some more thought into it and now I have come up with the below code. I have called the num variable globaly so the function and the main use it. pretty simple to understand what I am trying to do I think. In the main I am scaning the number into the num variable which is called globaly. Than in the function the nested for loops to get teh numbers and finally back in the main I am using printf to print the function. Now I think I have made this function recursive because it is returning itself. It is still not working though, maybe you can help me out with some direction. I read the post you recommended earlier but it was kind of hard to understand because it was C++ and I am only in a C class. Thanks in advance.
CPP / C++ / C Code:
Last edited by dsmith : 17-Dec-2004 at 19:34.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#5
|
||||
|
||||
|
Hi kakamuti. Sorry I haven't been around the past couple of days, so I am just barely getting back to you. Your second program is a little bit better, but I think you should step back a bit and try to understand what your program is doing. First of all lets write this without recursion. Without recursion, I am going to need two functions (or two loops). Here is what I have:
CPP / C++ / C Code:
That gives you the output that you wanted, but the process is not using recursion. With recursion, the function can call itself and then return to the point that it is called. That is why your problem is asking for a backwards printing, followed by a forwards printing. This can all be done in one recursive function. This function needs to:
Here is the recursive version: CPP / C++ / C Code:
This is probably late, but this is a very straight forward sample of how recursion works and what the benifits of using it are. Cheers, d __________________
The best damn Sports Blog period. |
Recent GIDBlog
Compress Your Web Site by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| Help! Some basal questions about MFC | xutingnjupt | MS Visual C++ / MFC Forum | 1 | 05-Dec-2004 03:38 |
| Nested for loop with function | Tori | C++ Forum | 11 | 08-Nov-2004 13:02 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 04:59 |
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The