Go4Expert

Go4Expert (http://www.go4expert.com/)
-   $1 Daily Competition (http://www.go4expert.com/contests/daily-competition/)
-   -   Are you a great C coder? | Feb. 13, 2010 (http://www.go4expert.com/contests/c-coder-feb-13-2010-t20952/)

techgeek.in 11Feb2010 09:27

Are you a great C coder? | Feb. 13, 2010
 
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..
:):):):):)

shabbir 13Feb2010 18:00

Re: Are you a great C coder?
 
Approved

xpi0t0s 14Feb2010 15:13

Re: Are you a great C coder? | Feb. 13, 2010
 
Use if and goto. Not tested, cos I rarely use gotos:
Code:

int i=0;
:loop
printf("%d ");
i++;
if (i<10) goto loop;


techgeek.in 14Feb2010 18:52

Re: Are you a great C coder? | Feb. 13, 2010
 
dnt use GOTO...goto is not structured...

techgeek.in 14Feb2010 18:54

Re: Are you a great C coder? | Feb. 13, 2010
 
shabbir...wud u continue this? I think noone is able to give the ans...

shabbir 14Feb2010 19:26

Re: Are you a great C coder? | Feb. 13, 2010
 
Lets do it buddy

xpi0t0s 14Feb2010 22:12

Re: Are you a great C coder? | Feb. 13, 2010
 
Answer must be tail recursion then:
Code:

void test2a(int i)
{
        if (i<10)
        {
                printf("%d ",i);
                test2a(i+1);
        }
}

void test2()
{
        test2a(0);
}


techgeek.in 14Feb2010 23:11

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

Originally Posted by xpi0t0s (Post 64228)
Answer must be tail recursion then:
Code:

void test2a(int i)
{
        if (i<10)
        {
                printf("%d ",i);
                test2a(i+1);
        }
}

void test2()
{
        test2a(0);
}


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

xpi0t0s 15Feb2010 14:13

Re: Are you a great C coder? | Feb. 13, 2010
 
Only if you've got enough stack space. If you haven't then you have to (a) use goto or (b) unroll the loop.

shabbir 15Feb2010 16:50

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

Originally Posted by xpi0t0s (Post 64262)
Only if you've got enough stack space. If you haven't then you have to (a) use goto or (b) unroll the loop.

Offtopic comment:
Have you seen your Visitor Message?


All times are GMT +5.5. The time now is 21:51.