GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Dec-2004, 20:40
kakamuti kakamuti is offline
New Member
 
Join Date: Dec 2004
Posts: 5
kakamuti is on a distinguished road

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:
#include <stdio.h>

int recursivePrint(int i, int j);

  main()
 {
  
 
 int x=0,y=0;

  recursivePrint();

  printf ("*%d\n#%d", recursivePrint(x,y));

  return 0;

 }

 int recursivePrint(int i, int j){
  
  int num2=0;

 printf ("Enter an integer> ");
  scanf ("%d", &num2);

  for (i=num2;i>0;i--)
   for (j=1;j<num2;j++)
	return (i,j);  
  
  
 }

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 02:31. Reason: Please insert your c code between [c] & [/c] tags
  #2  
Old 14-Dec-2004, 21:51
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Hasn't anyone mentioned CODE TAGS yet? Ahhh, I see twice...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 16-Dec-2004, 08:55
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello kakamuti. The specific error that you are getting is right here:

CPP / C++ / C Code:
#include <stdio.h>

int recursivePrint(int i, int j);

  main()
 {
  
 
 int x=0,y=0;

  recursivePrint();   //This line is calling a function recursivePrint with no parameters...

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:
return (i,j);  

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.
  #4  
Old 16-Dec-2004, 19:05
kakamuti kakamuti is offline
New Member
 
Join Date: Dec 2004
Posts: 5
kakamuti is on a distinguished road
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:
#include <stdio.h>

 
 recursivePrint(int a,int b);
 int num=0;

  main()
 {
  
 int x=0;
 int y=0;

 
  printf ("Enter an integer> ");
  scanf ("%d", &num);
  
  printf ("*%d\n#%d", recursivePrint(x,y));
  
  return 0;

 }

  recursivePrint(int a,int b){
  
  int i;
  int j;
  a=0;
  b=0;
  
  
 for (i=num;i>0;i--)
  i=a;
  for (j=1;j<num;j++)
    j=b;
  return recursivePrint(a,b);
 
  }
Last edited by dsmith : 17-Dec-2004 at 20:34. Reason: Please use [c] & [/c] for syntax highlighting
  #5  
Old 19-Dec-2004, 08:47
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
void output1(int number)
{
	while(number--)
    	printf("* %d\n",number+1);
}

void output2(int number)
{
	int x;
	
	for(x=0;x<number;x++)
		printf("# %d\n",x+1);
}

int main()
{
	output1(3);
	output2(3);
	
	return 0;
}


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:
  1. Print the reverse print element.
  2. Check to see if the number is greater than 1
    1. If the number is greater than one, then recursively call the function with the number - 1.
    2. Otherwise do nothing.
  3. Print the forward print element.

Here is the recursive version:
CPP / C++ / C Code:
void rprint(int number)
{
	printf("* %d\n",number);
	if(number>1)
		rprint(number-1);
	printf("# %d\n",number);
}

int main()
{
	rprint(3);
	
	return(0);
}

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
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 04:38
Nested for loop with function Tori C++ Forum 11 08-Nov-2004 14:02
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 05:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:45.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.