473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Somple Recursion Question

Hi guys,

I am having to figure out the recusion in this function.

int recfn(int v)
{
if(v==1 || v==0)
return 1;
if(v%2==0)
return recfn(v/2)+2;
else
return recfn(v-1)+3;
}
recfn(7) gives me 11
I am not able to figure out the steps and numbers that got added up to that.

Please help.

Thanks alot,
Johnny

Nov 13 '05 #1
5 1314
Johnny Shih writes:
I am having to figure out the recusion in this function.

int recfn(int v)
{
if(v==1 || v==0)
return 1;
if(v%2==0)
return recfn(v/2)+2;
else
return recfn(v-1)+3;
}
recfn(7) gives me 11
I am not able to figure out the steps and numbers that got added up to

that.

I think the most informative way to proceed is to add a print statement as
the first statement in the function. Print the value of v. If necessary,
keep adding print statements until it makes sense.
Nov 13 '05 #2
Johnny Shih <aw****@hotmail.com> wrote:
Hi guys, I am having to figure out the recusion in this function. int recfn(int v)
{
if(v==1 || v==0)
return 1;
if(v%2==0)
return recfn(v/2)+2;
else
return recfn(v-1)+3;
}
recfn(7) gives me 11
I am not able to figure out the steps and numbers that got added up to that. Please help. Thanks alot,
Johnny

recfn(7) -> 3 + recfn(6)
recfn(6) -> 2 + recfn(3)
recfn(3) -> 3 + recfn(2)
recfn(2) -> 2 + recfn(1)
recfn(1) -> 1

do the math

--
Harrison Caudill | .^ www.hypersphere.org
Computer Science & Physics Double Major | | Me*Me=1
Georgia Institute of Technology | '/ I'm just a normal guy
Nov 13 '05 #3
Thanks guys.

Charles Harrison Caudill wrote:
Johnny Shih <aw****@hotmail.com> wrote:
Hi guys,


I am having to figure out the recusion in this function.


int recfn(int v)
{
if(v==1 || v==0)
return 1;
if(v%2==0)
return recfn(v/2)+2;
else
return recfn(v-1)+3;
}

recfn(7) gives me 11
I am not able to figure out the steps and numbers that got added up to that.


Please help.


Thanks alot,
Johnny


recfn(7) -> 3 + recfn(6)
recfn(6) -> 2 + recfn(3)
recfn(3) -> 3 + recfn(2)
recfn(2) -> 2 + recfn(1)
recfn(1) -> 1

do the math


Nov 13 '05 #4
Johnny Shih wrote:
Hi guys,

I am having to figure out the recusion in this function.

int recfn(int v)
{
if(v==1 || v==0)
return 1;
if(v%2==0)
return recfn(v/2)+2;
else
return recfn(v-1)+3;
}
recfn(7) gives me 11
I am not able to figure out the steps and numbers that got added up to
that.


recfn(7) = recfn(6) + 3
= recfn(3) + 2 + 3 = recfn(3) + 5
= recfn(2) + 3 + 5 = recfn(2) + 8
= recfn(1) + 2 + 8 = recfn(1) + 10
= 1 + 10 = 11

Now try doing your trivial busywork for yourself

--
Martin Ambuhl

Nov 13 '05 #5
On 2003-10-27, Johnny Shih <aw****@hotmail.com> wrote:
Hi guys,

I am having to figure out the recusion in this function.

int recfn(int v)
{
if(v==1 || v==0)
return 1;
if(v%2==0)
return recfn(v/2)+2;
else
return recfn(v-1)+3;
}
recfn(7) gives me 11
I am not able to figure out the steps and numbers that got added up to that.

Please help.


Try putting in printf statements.

int recfn(int v)
{
int r;

if(v==1 || v==0) {
r = 1;
} else if(v%2==0) {
r = recfn(v/2)+2;
} else {
r = recfn(v-1)+3;
}

printf("recfn(%d) == %d\n", v, r);
return r;
}

-- James
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
4
by: M Hayouka | last post by:
hi can anybody give me a web that I can learn about a recursion function I don't really understand it
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
9
by: JustSomeGuy | last post by:
I have a class that looks something like this (Don't try to compile it, I haven't tested this) class KVP { string key; string value; list<KVP> sublist; };
4
by: Thrillhouse | last post by:
I'm a college computer science major and I'm studying for my final exam by going over previous tests. There's one question that I answered correctly but I cannot, for the life of me, figure out...
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
12
by: Muzammil | last post by:
i want good practice over recursion. can any one give me links for recursion questions site.?? or question.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.