1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Are you a great C coder? | Feb. 13, 2010

Discussion in '$1 Daily Competition' started by techgeek.in, Feb 11, 2010.

  1. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    one day I and my friend were wondering what if C had no "Loops" ? Suppose C language has no "loops"(while, forloop, do while loop). Now, how are you going to implement a loop logic in C without while, for loop and do while loop ?? Explain with example..
    :):):):):)
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Re: Are you a great C coder?

    Approved
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Use if and goto. Not tested, cos I rarely use gotos:
    Code:
    int i=0;
    :loop
    printf("%d ");
    i++;
    if (i<10) goto loop;
    
     
  4. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    dnt use GOTO...goto is not structured...
     
  5. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    shabbir...wud u continue this? I think noone is able to give the ans...
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Lets do it buddy
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Answer must be tail recursion then:
    Code:
    void test2a(int i)
    {
    	if (i<10)
    	{
    		printf("%d ",i);
    		test2a(i+1);
    	}
    }
    
    void test2()
    {
    	test2a(0);
    }
    
     
  8. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    Wow! at last...
    Tail recursion is the only way to implement loop logic.. this process is used in PLT scheme.Take another example from me:-

    Code:
    # include<stdio.h>
    # include<conio.h>
    
    void ForLoop(int,int);
    void main()
    {
    
    int i,n;
    clrscr();
    i=0;
    n=10;
    ForLoop(i,n);
    getch();
    }
    
    
    void ForLoop(int i,int n)
    {
    static int count;
    if(i==n)
    printf("loop ended with count= %d", count);
    else
    {
    count++;
    i++;
    ForLoop(i,n);
    }
    }
    
    believe me you can implement any loop program using tail recursion. :p
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Only if you've got enough stack space. If you haven't then you have to (a) use goto or (b) unroll the loop.
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    [comment]Have you seen your Visitor Message?[/COMMENT]
     

Share This Page