Results 1 to 23 of 23
Thread: Recursive call to main() in C
-
28-06-2012, 09:58 PM #1In The Zone
- Join Date
- Apr 2012
- Location
- Kannur, Kerala
- Posts
- 202
Recursive call to main() in C
Write a c pgm which rewrites "The universe is never ending" using recursion so that it terminate after 17 calls. Your pgm should consist of a single main() function that calls itself recursively.
This question seems to be simple but asked for 15 marks. So I need answers from u guys.
Help me. Thanks in advance.
-
28-06-2012, 10:10 PM #2Human Spambot
- Join Date
- Nov 2004
- Location
- Madurai
- Posts
- 2,382
Re: help in a C program
Why dont you try to answer it yourself and see if you get errors. If you do, try to solve them yourself and only if you cannot solve a problem, post your code and note where you have a problem...
Please dont use the forum to solve your assignments for you.
Arun
-
28-06-2012, 10:11 PM #3
Re: help in a C program
Yes it's simple. I'd say try it yourself and post your attempt. No one would write your program for your assignment.
But if you do mistakes, everyone will help you.- Read The Forum RULES First.
- Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
- Before Starting A New Thread Read The STICKY THREADS First
- Before Participating In Bazaar Section Read The BAZAAR RULES
-
28-06-2012, 10:30 PM #4In The Zone
- Join Date
- Apr 2012
- Location
- Kannur, Kerala
- Posts
- 202
Re: help in a C program
This question is asked in my theory exam for the subject computer programming in engineering... Not in pratical xam.
What I actully written in the xam is given below.
read * as plus symbol. I dont know why it is not printed here.Code:#include<stdio.h> int main() { static int i=0; while(i<17) { printf("THE UNIVERSE IS NEVER ENDING"); i**; main(); } }
is this the right answer? If yes then does this question deserves 15 marks?
-
29-06-2012, 12:17 AM #5
Re: help in a C program
You need to declare and define a counter before entering recursion, you can implement termination condition within method's body.
Twitter: twitter.com/SharmaTushar
Facebook: facebook.com/tushar.sharma
-
29-06-2012, 01:05 AM #6
Re: help in a C program
Should be something like this.Code:#include<stdio.h> int main() { static int i = 0; rec_func(); return 0; } void rec_func() { if(i>=17) return; else { print("THE UNIVERSE IS NEVER ENDING"); i++; rec_func(); } }- Read The Forum RULES First.
- Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
- Before Starting A New Thread Read The STICKY THREADS First
- Before Participating In Bazaar Section Read The BAZAAR RULES
-
29-06-2012, 01:56 AM #7
Re: help in a C program
Its offtopic but here's the java version:
Code:public class Universe { public static void univ(int i){ if ( i <= 16 ){ System.out.println("The universe is never ending"); univ(i+1); } } public static void main(String [] args ){ univ(0); } }Core i5 750, biostar h55 A+ (X16+X4), 4gb 1333 ddr3, corsair vx450, cm elite 335,Asus EAH 5750 FORMULA , samsung 2033 sw plus, wd green 1tb , wd 1tb my book ,Seagate freeagent ext hdd 1tb , hp dvd writer,Apc 650 va, logitech z313 , Micromax Funbook , Klipsch image S3 (IEM).
-
29-06-2012, 08:41 AM #8In The Zone
- Join Date
- Apr 2012
- Location
- Kannur, Kerala
- Posts
- 202
Re: help in a C program
I could not understand you. Can you explain in detail? What is a counter?
Is my answer wrong?
Is there any mistakes?
In the question it is clear that the main() function should call it recursively. So yours is wrong?
Pls clarify.
I wrote only this question. Did not consider the "OR" question. Will my 15 marks be waived off?
-
29-06-2012, 09:01 AM #9
Re: help in a C program
Counter is actually any integer variable (e.g. int ctr=0
which is used in a loop to find out how many times the loop has actually revolved around.
for e.g.
int ctr=0;
int x=0;
for(x=0;x<5;x++)
{
ctr++;
}
the output will be
ctr=5 since the loop has revolved for 5 times.
I hope you got it.Spoiler:
-
29-06-2012, 09:09 AM #10
Re: help in a C program
I'd explain it to you, but your brain would explode... Trust me
i5 760, Asus P7P55D-EVO, Zotac GTX 460, VX550, Win7 64bit,4GB RAM @1600MHz, WD 500GB+1.5TB Caviar Black, NZXT Phantom
-
29-06-2012, 09:42 AM #11
Re: help in a C program
- Read The Forum RULES First.
- Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
- Before Starting A New Thread Read The STICKY THREADS First
- Before Participating In Bazaar Section Read The BAZAAR RULES
-
29-06-2012, 10:12 AM #12
Yes, main function can call itself. Afterall main is also a function. And just like other function main also can accept parameters and return values.
Try attempting the conversion of that recur function to main. It should be simple.
Posted from a mobile device.
-
29-06-2012, 10:17 AM #13In The Zone
- Join Date
- Apr 2012
- Location
- Kannur, Kerala
- Posts
- 202
Re: help in a C program
-
29-06-2012, 10:29 AM #14In The Zone
- Join Date
- Sep 2011
- Location
- Vellore
- Posts
- 369
Re: help in a C program
You can expand letters or articles or reports in english if you feel that they are small, but you can't do the same with a computer program. There is nothing like minimum or maximum word limit here.
The 15 marks are for the logic, or the teachers want to make passing easy.HP DV6 6165tx - Core i7 2670QM | 4GB RAM | 2GB AMD Radeon 6770m | 750 GB HDD | Seagate GoFlex 1TB
-
29-06-2012, 06:37 PM #15
Re: help in a C program
Thanks guys for confirming the same.
- Read The Forum RULES First.
- Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
- Before Starting A New Thread Read The STICKY THREADS First
- Before Participating In Bazaar Section Read The BAZAAR RULES
-
29-06-2012, 09:13 PM #16
Re: help in a C program
Sorry, didn't read the question carefully. Since it requires recursion of main(), you need a static counter declared obviously inside the main()'s body as you have done in post #4, it just doesn't need a while loop, but a selection (if..else, switch-case) which terminates the loop when counter reaches 17.
Spoiler:Twitter: twitter.com/SharmaTushar
Facebook: facebook.com/tushar.sharma
-
29-06-2012, 09:38 PM #17
Re: help in a C program
Recrusion of main() in C++ is not legal.The compiler will produce error.
HOwever , in C you are allowed to do it but its little complicated in barely used.Spoiler:
-
29-06-2012, 09:46 PM #18In The Zone
- Join Date
- Apr 2012
- Location
- Kannur, Kerala
- Posts
- 202
-
29-06-2012, 11:32 PM #19
Re: help in a C program
AMD Phenom II X6 1055T|MSI 880GMA-E45|MSI 7770|2x2 Corsair DDR3 1333MHz|CM Elite 310|Corsair CX400W|DELL ST2320L
Myself @ nbaztec.co.in
Build Your Rig! (XBlade) @ nbaztec.co.in - Build Your Rig!
My Inner Artist @ nbaztec.co.in - Designs
-
01-07-2012, 08:15 AM #20
Re: help in a C program
Twitter: twitter.com/SharmaTushar
Facebook: facebook.com/tushar.sharma
-
02-07-2012, 05:01 PM #21In The Zone
- Join Date
- Apr 2012
- Location
- Kannur, Kerala
- Posts
- 202
-
03-07-2012, 02:45 PM #22
-
03-07-2012, 02:54 PM #23In The Zone
- Join Date
- Apr 2012
- Location
- Kannur, Kerala
- Posts
- 202
Re: help in a C program
^^ no chance this time. Best of luck next time
.
Similar Threads
-
Need Main Differences between G31, G33, G35 and P35 , P45 chipsets.
By hayabusa_ryu in forum QnA (read only)Replies: 9Last Post: 18-11-2008, 07:28 PM -
K550(c)i Chinese Main and FS
By ChrisTom in forum Mobiles and TabletsReplies: 4Last Post: 01-05-2008, 08:30 PM -
Exception in thread main...
By nikhil ramteke in forum QnA (read only)Replies: 2Last Post: 30-08-2007, 01:33 PM -
VB Beginners.. Main guidelines??
By //siddhartha// in forum ProgrammingReplies: 6Last Post: 13-03-2005, 12:43 AM -
VB Beginners.. Main guidelines??
By //siddhartha// in forum TutorialsReplies: 6Last Post: 13-03-2005, 12:43 AM



LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks