Using Recursive Function



Help for C/C++ for MVS, OS/390 C/C++, z/OS C/C++ and C/C++ Productivity Tools for OS/390

Using Recursive Function

Postby Sabreen Khan » Wed Mar 19, 2008 5:49 pm

Hi..Im sabreen, a beginner in the language C. Very recently i have started learning about functions and i am having some trouble solving a problem of recursive function.

The problem is :
I have to write a code in which the user will input 2 integers-let them be a=1,b=1. The output should be an arithmetic series in which each number is the sum of the previous 2 numbers. For example, in this case, the 1st two numbers are 1 and 1. The series should be like this :
1 1 2 3 5 8 13 21 34
The series should continue from 1 to 50.

I'm having trouble understanding the recursive function which we have to use to write the codes for this program.

If you can, please help me.
btw, Dick, thanks for your reply to my last post.
Sabreen Khan
 
Posts: 4
Joined: Sat Mar 01, 2008 2:22 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Using Recursive Function

Postby dick scherrer » Wed Mar 19, 2008 9:09 pm

You're welcome :)

Unfortunately, i don't speak "c". . . I've solved similar requirements in other languages, but they did not have a built-in function for recursion (we wrote our own). Probably wouldn't help as you want to learn to use the built-in function.

d
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Using Recursive Function

Postby pcdoctor » Tue Mar 25, 2008 9:38 pm

This is a normal Fibonacci series program..U necessarily need not use recursion to implement this..Google for fibonacci series using c and ull get it..If u still find difficult to undstand then post here..
pcdoctor
 
Posts: 17
Joined: Mon Feb 18, 2008 10:21 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Using Recursive Function

Postby dick scherrer » Tue Mar 25, 2008 10:01 pm

Hello,

I believe the goal was to use recursion. Solving the calculation was merely something to do that would allow the use of the recursion function. If i understood correctly. . . which i might not ;)
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Using Recursive Function

Postby Sabreen Khan » Wed Mar 26, 2008 8:01 am

hey...thanx...bt i tried and tried and i finally got the solution for fibonacci function using recursion functin..
Sabreen Khan
 
Posts: 4
Joined: Sat Mar 01, 2008 2:22 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Using Recursive Function

Postby dick scherrer » Wed Mar 26, 2008 8:08 am

You're welcome :)

If you post your solution, it may help someone with a similar question later.

d
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Using Recursive Function

Postby Sabreen Khan » Wed Mar 26, 2008 11:08 am

#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);

}
Sabreen Khan
 
Posts: 4
Joined: Sat Mar 01, 2008 2:22 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Using Recursive Function

Postby dick scherrer » Wed Mar 26, 2008 7:16 pm

Thank you :)

d
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Using Recursive Function

Postby darvin13 » Thu Jul 01, 2010 6:18 am

my problem:
i have to write a code using recursive function about arithmetic series, the user will input two integers the length of the output and the difference, the output must start to 2.

example:

length: 5 //this is not fixed
difference: 2 //this is not fixed

the output must be: 2 4 8 32 256
i need help..
nivrad13
darvin13
 
Posts: 4
Joined: Wed Jun 30, 2010 6:11 pm
Location: Philippines
Has thanked: 0 time
Been thanked: 0 time

Re: Using Recursive Function

Postby darvin13 » Thu Jul 01, 2010 6:20 am

im darvin..
i need the codes later..
thanks :)
i need help..
nivrad13
darvin13
 
Posts: 4
Joined: Wed Jun 30, 2010 6:11 pm
Location: Philippines
Has thanked: 0 time
Been thanked: 0 time

Next

Return to C, C++

 


  • Related topics
    Replies
    Views
    Last post