Results 1 to 23 of 23
  1. #1
    In The Zone
    Join Date
    Apr 2012
    Location
    Kannur, Kerala
    Posts
    202

    Default 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.

  2. #2
    Human Spambot
    Join Date
    Nov 2004
    Location
    Madurai
    Posts
    2,382

    Default 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

  3. #3
    Simply a DIGITian krishnandu.sarkar's Avatar
    Join Date
    Nov 2007
    Location
    Kolkata
    Posts
    3,493

    Default 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

  4. #4
    In The Zone
    Join Date
    Apr 2012
    Location
    Kannur, Kerala
    Posts
    202

    Default 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.
    Code:
    #include<stdio.h>
    int main()
    {
     static int i=0;
     while(i<17)
     {
        printf("THE  UNIVERSE IS NEVER ENDING");
     i**;
     main();
     }
    }
    read * as plus symbol. I dont know why it is not printed here.
    is this the right answer? If yes then does this question deserves 15 marks?

  5. #5
    VIP RazorbladeXtreme's Avatar
    Join Date
    May 2008
    Location
    Jaipur
    Posts
    239

    Default 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

  6. #6
    Simply a DIGITian krishnandu.sarkar's Avatar
    Join Date
    Nov 2007
    Location
    Kolkata
    Posts
    3,493

    Default Re: help in a C program

    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();
        }
    }
    Should be something like this.
    • 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

  7. #7
    I am the night...I am... vickybat's Avatar
    Join Date
    Aug 2009
    Location
    Gotham City
    Posts
    4,790

    Default 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).

  8. #8
    In The Zone
    Join Date
    Apr 2012
    Location
    Kannur, Kerala
    Posts
    202

    Default Re: help in a C program

    Quote Originally Posted by RazorbladeXtreme View Post
    You need to declare and define a counter before entering recursion, you can implement termination condition within method's body.
    I could not understand you. Can you explain in detail? What is a counter?

    Quote Originally Posted by krishnandu.sarkar View Post
    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();
        }
    }
    Should be something like this.
    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?

  9. #9
    tH3 !0$t $0u! Rishi.'s Avatar
    Join Date
    Jun 2012
    Location
    New Delhi.
    Posts
    1,061

    Default Re: help in a C program

    Quote Originally Posted by rijinpk1 View Post
    I could not understand you. Can you explain in detail? What is a counter?
    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:

    Current : Acer Aspire V3-571G.
    Config : Ivy Bridge Corei7 3610QM @2.3Ghz | 2GB GT 640m DDR3 GPU (O'Ckled to GTX660m DDR3) | 4 GB DDR3 RAM | 500GB HDD | 15.6' HD.

    Previous :
    NetBook : HP Pavilion DM1-4003au
    Config : AMD E-450 1.65Ghz dual Core with Radeon HD 6320 IGP.
    2 GB DDR3 1333Mhz RAM.
    11.6" Glossy Screen.| 6 Cell LIon Battery.

    RIG : Intel C2D E4500 2.2GHz | Intel DG41RQ | 1*2GB DDR2 RAM 800Mhz Kingston + 1GB Apacer DDR2 @666Mhz. | WD Caviar SE 250GB SATA 7200 RPM | WD Caviar Green 1 TB 7200 RPM with 64M Cache | Nvidia GeForce 8400GS 512MB DDR2.| Samsung SMB2030" 16x9. |.

    Audio Gear : Dunnu Hephaes DN-16 IEM | Tekfusion TwinWoofer V1.| Sansa CLip V1.
    HandHelds : Motorola Atrix 4G(MB860) | Nokia N95 8GB | Nokia 6630.


  10. #10
    Wise Old Owl pulkitpopli2004's Avatar
    Join Date
    Jul 2010
    Location
    D!ll!
    Posts
    1,411

    Default Re: help in a C program

    Quote Originally Posted by rijinpk1 View Post
    #include<stdio.h>
    void main()
    {
    static int i=0;
    while(i<17)
    {
    printf("THE UNIVERSE IS NEVER ENDING");
    i**;
    main();
    }
    }
    read * as plus symbol. I dont know why it is not printed here.
    yes this is completely correct.. A small thing you can get or teacher can deduct mark for is "\n" in printf command..
    You should have written that.
    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

  11. #11
    Simply a DIGITian krishnandu.sarkar's Avatar
    Join Date
    Nov 2007
    Location
    Kolkata
    Posts
    3,493

    Default Re: help in a C program

    Quote Originally Posted by rijinpk1 View Post
    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?
    I'm not too good in C

    I'd like to wait for some other member to verify that.

    But I don't know whether main() can call itself.
    • 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

  12. #12
    making future better Vyom's Avatar
    Join Date
    May 2009
    Location
    New Delhi
    Posts
    3,139

    Default

    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.

  13. #13
    In The Zone
    Join Date
    Apr 2012
    Location
    Kannur, Kerala
    Posts
    202

    Default Re: help in a C program

    Quote Originally Posted by pulkitpopli2004 View Post
    yes this is completely correct.. A small thing you can get or teacher can deduct mark for is "\n" in printf command..
    You should have written that.
    I actually put "\n" in my answer in the xam. But forgot to put here.
    But my question is does this question deserve 15 marks? I absolutely dont think so...

    Quote Originally Posted by krishnandu.sarkar View Post
    I'm not too good in C

    I'd like to wait for some other member to verify that.

    But I don't know whether main() can call itself.
    yes.. main() can call itself recursively if needed.

  14. #14
    In The Zone
    Join Date
    Sep 2011
    Location
    Vellore
    Posts
    369

    Default Re: help in a C program

    Quote Originally Posted by rijinpk1 View Post
    But my question is does this question deserve 15 marks? I absolutely dont think so...
    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

  15. #15
    Simply a DIGITian krishnandu.sarkar's Avatar
    Join Date
    Nov 2007
    Location
    Kolkata
    Posts
    3,493

    Default 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

  16. #16
    VIP RazorbladeXtreme's Avatar
    Join Date
    May 2008
    Location
    Jaipur
    Posts
    239

    Default 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:

    You shouldn't be seeing this
    Spoiler:

    Code:
    #include<stdio.h>
    int main(void) {
    	static int i=0; //This is counter
    	if(i==16) return 0; //Termination Condition
    	printf("The World is Never Ending\n");
    	i++; //Counting : )
    	main();
    }

    Twitter: twitter.com/SharmaTushar
    Facebook: facebook.com/tushar.sharma

  17. #17
    tH3 !0$t $0u! Rishi.'s Avatar
    Join Date
    Jun 2012
    Location
    New Delhi.
    Posts
    1,061

    Default 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:

    Current : Acer Aspire V3-571G.
    Config : Ivy Bridge Corei7 3610QM @2.3Ghz | 2GB GT 640m DDR3 GPU (O'Ckled to GTX660m DDR3) | 4 GB DDR3 RAM | 500GB HDD | 15.6' HD.

    Previous :
    NetBook : HP Pavilion DM1-4003au
    Config : AMD E-450 1.65Ghz dual Core with Radeon HD 6320 IGP.
    2 GB DDR3 1333Mhz RAM.
    11.6" Glossy Screen.| 6 Cell LIon Battery.

    RIG : Intel C2D E4500 2.2GHz | Intel DG41RQ | 1*2GB DDR2 RAM 800Mhz Kingston + 1GB Apacer DDR2 @666Mhz. | WD Caviar SE 250GB SATA 7200 RPM | WD Caviar Green 1 TB 7200 RPM with 64M Cache | Nvidia GeForce 8400GS 512MB DDR2.| Samsung SMB2030" 16x9. |.

    Audio Gear : Dunnu Hephaes DN-16 IEM | Tekfusion TwinWoofer V1.| Sansa CLip V1.
    HandHelds : Motorola Atrix 4G(MB860) | Nokia N95 8GB | Nokia 6630.


  18. #18
    In The Zone
    Join Date
    Apr 2012
    Location
    Kannur, Kerala
    Posts
    202

    Default Re: help in a C program

    Quote Originally Posted by Rishi. View Post
    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.
    I didnt know that. I will check it for sure. Need to clarify that asap. Anyway thanks for commenting.

  19. #19
    Master KOD3R nbaztec's Avatar
    Join Date
    Sep 2010
    Location
    New Delhi
    Posts
    342

    Default Re: help in a C program

    Quote Originally Posted by rijinpk1 View Post
    I didnt know that. I will check it for sure. Need to clarify that asap. Anyway thanks for commenting.
    Rishi is right. Unlike C, recursion of main() is invalid in C++.
    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

  20. #20
    VIP RazorbladeXtreme's Avatar
    Join Date
    May 2008
    Location
    Jaipur
    Posts
    239

    Default Re: help in a C program

    Quote Originally Posted by sarthak View Post
    The 15 marks are for the logic, or the teachers want to make passing easy.
    Never happens with me. I write just what's asked in the question, but unless you explain history of every term encountered in the question, you've got very slim chances of scoring
    Twitter: twitter.com/SharmaTushar
    Facebook: facebook.com/tushar.sharma

  21. #21
    In The Zone
    Join Date
    Apr 2012
    Location
    Kannur, Kerala
    Posts
    202

    Default Re: help in a C program

    Quote Originally Posted by RazorbladeXtreme View Post
    Never happens with me. I write just what's asked in the question, but unless you explain history of every term encountered in the question, you've got very slim chances of scoring
    absolutely true for all UNIVERSITY exams.....

  22. #22
    Broken In audiophilic's Avatar
    Join Date
    Mar 2012
    Location
    India
    Posts
    181

    Default Re: help in a C program

    Quote Originally Posted by sakumar79 View Post
    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
    I was about to say the same thing.

  23. #23
    In The Zone
    Join Date
    Apr 2012
    Location
    Kannur, Kerala
    Posts
    202

    Default Re: help in a C program

    ^^ no chance this time. Best of luck next time.

Similar Threads

  1. Replies: 9
    Last Post: 18-11-2008, 07:28 PM
  2. K550(c)i Chinese Main and FS
    By ChrisTom in forum Mobiles and Tablets
    Replies: 4
    Last Post: 01-05-2008, 08:30 PM
  3. Exception in thread main...
    By nikhil ramteke in forum QnA (read only)
    Replies: 2
    Last Post: 30-08-2007, 01:33 PM
  4. VB Beginners.. Main guidelines??
    By //siddhartha// in forum Programming
    Replies: 6
    Last Post: 13-03-2005, 12:43 AM
  5. VB Beginners.. Main guidelines??
    By //siddhartha// in forum Tutorials
    Replies: 6
    Last Post: 13-03-2005, 12:43 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Close