473,399 Members | 3,656 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,399 software developers and data experts.

plz help me....I can't understand the answer to the following program

#include<stdio.h>

void crazy(int a,int b, int m, int n)
{
if(n==0)
return;
crazy(a+m,b,m+b,n-1);
printf("%d %d %d %d ",a,b,m,n);
printf("\n");
crazy(a,b+m,m+a,n-1);
}

int main()
{
crazy(2,5,1,3);
return (0);
}


what is the output?? i am confused about this
Oct 21 '15 #1
5 1229
weaknessforcats
9,208 Expert Mod 8TB
What don't you understand?

This is an example of recursion.
Oct 21 '15 #2
donbock
2,426 Expert 2GB
What output did you get?
Oct 22 '15 #3
recursion 1:
a=2 b=5 m=1 n=3

recursion1.1:
a=2+1 b=5 m=1+5 n=2

recursion 1.1.1:
a=3+6 b=5 m=6+5 n=1

recursion 1.1.1.1:
a=9+11 b=5 m=11+5 n=0

return
print-> 20 5 16 0

The second recursion call makes it very complex. you have to handle two recursions that are kinda nested.
Working out the second part....I guess ill leave it to you. :p
Oct 23 '15 #4
weaknessforcats
9,208 Expert Mod 8TB
I am concerned that you have two calls to crazy() inside
crazy().

Usually there is only one recursive call:

Expand|Select|Wrap|Line Numbers
  1. void crazy(int a,int b, int m, int n)
  2. {
  3.    if(n==0)
  4.      return;
  5.    printf("%d %d %d %d ",a,b,m,n);
  6.    printf("\n"); 
  7.    crazy(a,b+m,m+a,n-1);
  8. }
  9.  
In this example you print if n is not zero then call crazy() with new argument values. Those value get printed in the next call, which changes the values and calls crazy()again.

Finally, n is zero and that remote crazy() returns causing the next remote to return, until the first call to crazy() returns.

The number of lines you print would be the number of calls in the recursion.
Oct 23 '15 #5
donbock
2,426 Expert 2GB
@jaseel97 I agree except for the last line. I think it will be
print-> 9 5 11 1
because crazy(a,b,m,0) doesn't print anything.

My pencil-and-paper analysis suggests a pattern -- for crazy(a,b,m,n), the number of lines printed will be 2^(0) + 2^(1) + ... + 2^(n-1) = 2^(n) - 1. The pattern looks like a family tree.

@AndrewCool Have you run this program yet? If so, what was the output?
Oct 23 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rich | last post by:
Hi all I am using DEVCPP 4.9.8.1 which uses MingW32 GCC 3.2 compiler I am trying to compile this program, but without success, I am sure the compiler is set up ok, as I can compile and run...
6
by: PengYu.UT | last post by:
Hi, I run into error with the following program. Would you please help me? Best wishes, Peng struct tag1{}; struct tag2{};
6
by: Bilbo | last post by:
Hi, I am a long time matlab user, now I am learning C but can´t understand the following: 1)If I define a variable, without giveing a value to it, the compiler gives any value to it. 2) when I...
1
by: ssubbarayan | last post by:
Gurus, One of my friend mailed me this sample piece of code: #include <stdio.h> main() { int a,b,c; int count = 1; for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\ TFy!QJu ROo TNn(ROo)SLq SLq...
0
by: CMEDIA_SOUND | last post by:
I have a peculiar problem, I have a tabpage with a label control on it. When i set a background image to the tabpage and drag the label around it has paint issues in that it is slow, granted the...
2
by: mikelinyoho | last post by:
Does the following program architecture make sense? #include <stdio.h> #include "test1.c" #include "sample.cpp" int main(void){ output();
10
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.