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 19-Mar-2008, 22:59
Sabreen Khan Sabreen Khan is offline
New Member
 
Join Date: Mar 2008
Posts: 8
Sabreen Khan is on a distinguished road

Recursion function


I am having a lot of problem with recursive functions which i'v recently started learning.
The problem: The user gives 2 int inputs, eg 1 and 1. The output should b a series like this 1 1 2 3 5 8 13 21 34 ...till 50
i.e the next number is the sum of the previous 2 numbers.Recursive functions are a bit confusing and since i am a beginner, i would be glad if you could help me.
  #2  
Old 20-Mar-2008, 00:21
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
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

Re: Recursion function


Sure, we'd be glad to help. But this isn't the Psychic Programmer's Forum. unless you show us what you've done and explained what the problem is, we're at a loss to help.

Read the Guidelines
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 20-Mar-2008, 01:37
Sabreen Khan Sabreen Khan is offline
New Member
 
Join Date: Mar 2008
Posts: 8
Sabreen Khan is on a distinguished road

Re: Recursion function


CPP / C++ / C Code:
#include<stdio.h>
#include<conio.h>
 
int sum;
int rec(int previous,int result,int sum)
{
    if (result<50)
    {
    sum= result+previous;
    previous=result;
    result=sum;
    return(result);
    rec(previous,result,sum);    
 
    }
 
}
 
main()
{ 
    int previous=0;
    int result=1;
    //rec(previous,result,sum);
    int z;
    z=rec(previous,result,sum);
    printf("%d",z);
}
 
Code:
Compiling... recursive function.c E:\AIUB NOTES\PROGRAMMING LANGUAGE\recursive function.c(49) : warning C4715: 'rec' : not all control paths return a value recursive function.obj - 0 error(s), 1 warning(s)

The output gives 1. Whereas i should be getting : 11235813...............till less than 50
Last edited by admin II : 20-Mar-2008 at 05:20. Reason: Please surround your C code with [cpp] your code [/cpp]
  #4  
Old 20-Mar-2008, 01:38
Sabreen Khan Sabreen Khan is offline
New Member
 
Join Date: Mar 2008
Posts: 8
Sabreen Khan is on a distinguished road

Re: Recursion function


I feel that i have probably placed the printf in the wrong place,or there has been some problem with the allignment of the codes. I dont understand what i have done wrong. Please help me if you can. it would be very kind of you.
  #5  
Old 21-Mar-2008, 20:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
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

Re: Recursion function


No, it's because when you call rec() the first time, you return and the program ends. You never get a second call to rec().

Execute the code you have by hand. Then rethink your logic in the function.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #6  
Old 21-Mar-2008, 23:56
Sabreen Khan Sabreen Khan is offline
New Member
 
Join Date: Mar 2008
Posts: 8
Sabreen Khan is on a distinguished road

Re: Recursion function


hey, Waltp...Im stuck. Please help me out. Even if i place return after calling rec() it gives the same output.
  #7  
Old 23-Mar-2008, 21:49
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
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

Re: Recursion function


And what did you come up with when you did your re-think? Did you follow your code as I suggested? What happened? What did you find out?
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #8  
Old 24-Mar-2008, 11:51
Sabreen Khan Sabreen Khan is offline
New Member
 
Join Date: Mar 2008
Posts: 8
Sabreen Khan is on a distinguished road

Re: Recursion function


CPP / C++ / C Code:
#include<stdio.h>
#include<conio.h>
 
 
 
int rec(int previous,int result)
{
    int sum;
    if (result<50) 
    {
    sum= result+previous;
    previous=result;
    result=sum;
    printf("%d ",result);
    rec(previous,result);    
}
    return;
 
}
 
main()
{ 
    int previous=0;
    int result=1;
    rec(previous,result);
 
}



i finally came up with this and it works...thanku anyways..
Last edited by admin II : 24-Mar-2008 at 11:57. Reason: Please surround your C code with [cpp] your code [/cpp]
  #9  
Old 29-Mar-2008, 15:34
khushal_kkk's Avatar
khushal_kkk khushal_kkk is offline
Awaiting Email Confirmation
 
Join Date: Mar 2008
Posts: 50
khushal_kkk is an unknown quantity at this point

Re: Recursion function


check it out


CPP / C++ / C Code:
#include<iostream.h>
#include<conio.h>
 
long Fibonacci(int); // function declaration //
 
 
void main()
{
clrscr();
int fib;
 
cout<<"Enter The Limit To Find The Fibonacci Series=";
    cin>>fib;
 
for (int i=0;i<fib;i++) // to display first 10 fibonacci numbers//
    cout << Fibonacci(i) << " ";
 
    getch();
 
}
 
 
long Fibonacci(int num) // function calling //
{
if (num == 0)
    return 0;
if (num == 1)
    return 1;
return Fibonacci(num-1) + Fibonacci(num-2); // use of recursion //
}
Last edited by admin II : 29-Mar-2008 at 16:42. Reason: Please surround your C++ code with [cpp] your code [/cpp]
  #10  
Old 31-Mar-2008, 09:22
faisalnet5 faisalnet5 is offline
New Member
 
Join Date: Feb 2008
Posts: 25
faisalnet5 is on a distinguished road

Re: Recursion function


Dude...Sabreen....u r basically working with a very common problem in Computer Science.....it is called "Fibonacci series". Just Google it up....see the algorithm for that series...try to understand it, try to get the meaning of recursive functions....thats it....U will be the one then...

Good Luck
 
 

Recent GIDBlogProgramming ebook direct download available 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
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Flex and bison coding lucky88star C++ Forum 5 24-Dec-2007 12:57
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
Message Class TransformedBG C++ Forum 5 29-Nov-2006 22:28
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12

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

All times are GMT -6. The time now is 02:29.


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