without loop printing 1 to n

C

Clark S. Cox III

Yahooooooooo said:
no gotos
no loops
how is it possbile.....
Asked in interview ...

Regards
MA

#include <stdio.h>

int main(void)
{
printf("1 to n\n");
return 0;
}
 
J

jd

Yahooooooooo said:
no gotos
no loops
how is it possbile.....
Asked in interview ...

Regards
MA#

recusion?

void foo(int a, b) {
if(a=b) then return
else {
printf("%d\n", a);
foo(a+1, b);
}
}
 
J

jd

jd said:
recusion?

void foo(int a, b) {
if(a=b) then return
else {
printf("%d\n", a);
foo(a+1, b);
}
}

Whoops. Too hasty, == for equality, include stdio.h etc..
 
J

jd

jd said:
Bah! It gets worse! No 'then' obviously. Guess who has to program in
Delphi at the moment?

AAARRRGHH! And it only prints up to N-1 too. Should have been a>b.

I promise I'll never post again to clc.
 
R

Richard Heathfield

Yahooooooooo said:
no gotos
no loops
how is it possbile.....

Method 1 of N:

#include <stdio.h>

int main(void)
{
puts("1 to n");
return 0;
}

Method N of N:

#include <stdio.h>
#include <assert.h>

void printrange(unsigned long low, unsigned long high)
{
unsigned long range = 0;
assert(low <= high);
range = high - low + 1;
if(range > 1)
{
unsigned long this = low + range / 2;
unsigned long lolo = low;
unsigned long lohi = this - 1;
unsigned long hihi = high;
unsigned long hilo = this + 1;

if(lolo <= lohi)
{
printrange(lolo, lohi);
}
printf(" %lu", this);
if(hilo <= hihi)
{
printrange(hilo, hihi);
}
}
else
{
printf(" %lu", high);
}
}

int main(void)
{
printrange(1UL, 1000UL); /* insert your own unsigned long value here */
putchar('\n');
return 0;
}

Finding methods 2 to N-1 is left as an exercise.
 
Z

Zara

SORRY forgot to metion....
No recursion also :(


Not real recursion, but template recursion:

<code>
#include <iostream>

template <int M,int MAX> struct whatever {
ostream& intoStream(ostream& os) const {
os<<M;
if (M<MAX) os<<','<<whatever<M+1,MAX>();
return os;
}
};

template <int M> struct whatever<M,M> {
ostream& intoStream(ostream& os) const {
return os<<M;
}
};

template <int M,int MAX>
ostream& operator<<(ostream& os,const whatever<M,MAX>& thing)
{
return thing.intoStream(os);
}
int main() {
const int n=10;
std::cout<<whatever<1,n>()<<std::endl;
char c;std::cin>>c;
}
</code>

It is limited by your compiler limits on template recursion, and it is
a fixed program.

Regards,

Zara
 
Z

Zara

Not real recursion, but template recursion:

<...>

I am so sorry. I thought I was reading comp.lang.c++.

I hope no one will be disturbed by this unintended slip of C++ code
into C list. I swear I will try to aviod this kind of errors in the
future.

Best and sorrowful regards,

Zara
 
K

Kohn Emil Dan

SORRY forgot to metion....
No recursion also :(


When hope has died... setjmp/longjmp save the day! I still agree that
technically it's a loop, but not a usual one ...;-)

Did I pass the interview?

Enjoy,

Emil


/*------------------------------------------------*/

#include <stdio.h>
#include <setjmp.h>

#define N 1000

static int curr_N = 0;

static jmp_buf jmpbuf;

void print(void)
{
curr_N++;
printf("%d\n",curr_N);
longjmp(jmpbuf,curr_N);
}


int main()
{
if (setjmp(jmpbuf) == N)
return 0;

print();

return 0; /* Just to prevent a compiler warning */
}
 
R

Random832

2006-12-22 said:
AAARRRGHH! And it only prints up to N-1 too. Should have been a>b.

I promise I'll never post again to clc.

You missed a semicolon </piling on>
 
L

Laurent Deniau

Marc said:
pre-processor recursion also.

no recursion ;-)

This would means that n is an integral constant literal and known at
compile-time and therefore would allow to expand the loop manually.

a+, ld.
 
U

Ulrich Eckhardt

Yahooooooooo said:
no gotos
no loops
how is it possbile.....
Asked in interview ...

...and lateron
No recursion also

Barring the typical ways to do this, i.e. some form of looping (using
for/while/goto) or recursion, there are pretty little choices left.

1. You could use a library function but I wouldn't be aware of one in the C
stdlib (more on that later).
2. You could use predefined, external data. For any Unixy system that boils
down to a system("seq ...") call or similar things.
3. Similarly, you could simply count external events, but there is no
construct that I'm aware of that lets you do that without somehow entering
a loop first...

Just wondering, are you sure that question was about C? Just for the
record, there are a few interesting ways in C++ that e.g. make use of
constructors or the C++ standardlibrary, which -other than the C one- does
have a function that can be used to generate sequences.

Anyhow, the notion of repeating anything with changed parameters until some
condition is fulfilled is called looping, no matter how you finally spell
it out and no matter where exactly the code is executed, so any of the
above methods don't qualify in the stricter sense.

The only thing that does not qualify as a loop is to spell it out (more or
less to unroll the loop):

if i > 0
print 1
if i > 1
print 2
if i > 2
print 3
...

What did the interviewer expect?

Uli
 
S

Stephen Sprunk

Yahooooooooo said:
no gotos
no loops
how is it possbile.....
Asked in interview ...

Most iterative problems can also be solved with recursion, and vice
versa. One or the other will be cleaner in most situations, but with a
bit of creativity you can transform one into the other.

#include <stdio.h>

void foo(unsigned n) {
if (!n) return;
foo(n-1);
printf("%u\n", n);
}

If you could print them from N to 1, instead of 1 to N, you could use a
slightly different form that a reasonably smart compiler could optimize
better (aka tail recursion).

S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top