TWISTER 2: Read on....

N

Nirjhar Oberoi

TWISTER 2.1
¿? Your have to take a decision in a program but the creator of the
programming language forgot to supply the programmer with a IF-ELE
Construct? Can you find a work arround?

TWISTER 2.2
¿? You have to use loops and print 1 to 100 without using any kind of
looping not even GOTO statement? Can u do it?
(* *)
******************************0oo*********(_)******oo0**************************

SMALLEST CODE WILL BE APPRECIATED

:)
 
R

Richard Heathfield

Nirjhar Oberoi said:
TWISTER 2.1
¿? Your have to take a decision in a program but the creator of the
programming language forgot to supply the programmer with a IF-ELE
Construct? Can you find a work arround?

That depends on what you think IF-ELE should do. Personally, I think it
should subtract the value of ELE from the value of IF, yielding the
difference between the two. The workaround would be as follows:

diff = IF;
diff -= ELE;
TWISTER 2.2
¿? You have to use loops and print 1 to 100 without using any kind of
looping not even GOTO statement? Can u do it?

I don't think he can. I don't think anyone else can either, since using
loops without using any kind of looping is likely to challenge the
ingenuity of even the most perspicacious programmer. Nevertheless, printing
1 to 100 without using any kind of looping is trivial:

puts("1 to 100");
SMALLEST CODE WILL BE APPRECIATED

No problem. Here ya go:

main(){}
 
C

CBFalconer

Richard said:
Nirjhar Oberoi said:
.... snip ...


I don't think he can. I don't think anyone else can either, since
using loops without using any kind of looping is likely to
challenge the ingenuity of even the most perspicacious programmer.
Nevertheless, printing 1 to 100 without using any kind of looping
is trivial:

puts("1 to 100");

You forgot about recursion:

#include <stdio.h>

void putn(int n) {
if (n - 1) putn(n - 1);
printf("%d\n", n);
}

int main(void) {putn(100); return 0;}

which even the all seeing u can use. Why do people give their
children one character names?
 
Z

Zara

TWISTER 2.1
¿? Your have to take a decision in a program but the creator of the
programming language forgot to supply the programmer with a IF-ELE
Construct? Can you find a work arround?

You may take a peek at local newspapaers, probably ther is some work
around, where you are not asked to make decisions. Street sweeping?
Maybe
TWISTER 2.2
¿? You have to use loops and print 1 to 100 without using any kind of
looping not even GOTO statement? Can u do it?

Ah! that was VHDL, not C! Off Topic here.

library loops;
use loops.all;

print:std_logic_vector(1 to 100)=(others=>'U');

As you can see, not only U, but also OTHERS can do it!
SMALLEST CODE WILL BE APPRECIATED

Try with Huffman coding.

Regards,

Zara
 
R

Richard Heathfield

CBFalconer said:
You forgot about recursion:

No, I didn't. I was saving that in case he turned out to be serious, and to
provide some effort of his own.
 
C

CBFalconer

Richard said:
CBFalconer said:


No, I didn't. I was saving that in case he turned out to be serious,
and to provide some effort of his own.

Well, I kept the example subtly obtuse (the output order and end
points) to give him something to think about. However I think
"challenge the ingenuity of the most perspicacious" implies
impossibility or the need for non-standard code.
 
R

Richard Heathfield

CBFalconer said:
Well, I kept the example subtly obtuse (the output order and end
points) to give him something to think about. However I think
"challenge the ingenuity of the most perspicacious" implies
impossibility or the need for non-standard code.

Yes, I implied impossibility, and I stand by that. You can't use loops
without using loops, which is what he asked. A recursive call is not a loop
per se. Nevertheless, I agree that it is probably what his tutor had in
mind.
 
E

Eric Sosman

CBFalconer said:
Well, I kept the example subtly obtuse (the output order and end
points) to give him something to think about. However I think
"challenge the ingenuity of the most perspicacious" implies
impossibility or the need for non-standard code.

The original problem statement, in full, was
> ¿? You have to use loops and print 1 to 100 without using any kind of
> looping not even GOTO statement? Can u do it?

.... and neither CBF's nor RH's code meets the requirement to
"use loops." I offer the following as a possible solution:

#include <stdio.h>
int main(void) {
char buff[293], *p = buff;
int n;
for (n = 1; n <= 100; ++n) {
sprintf(p, "%d\n", n);
while (*p)
++p;
}
fputs (buff, stdout);
return 0;
}

This program

- Uses loops (two of them; it could have been simpler were
"loops" not in the plural)

- "and"

- Prints 1 to 100 without using any kind of looping (the
output is generated by one execution of an fputs() call).

However, even this doesn't answer the question. It doesn't matter
if I or CBF or RH can produce a solution; the question asks whether
the former Secretary General can do it. Anybody know how to get
in touch with him?
 
R

Richard Heathfield

Eric Sosman said:

The original problem statement, in full, was


... and neither CBF's nor RH's code meets the requirement to
"use loops."

....and yours doesn't meet the requirement *not* to use "any kind of
looping".
 
G

Guest

Richard said:
Eric Sosman said:



...and yours doesn't meet the requirement *not* to use "any kind of
looping".

There is no such requirement. There is a requirement to print 1 to 100
without using any kind of looping. Eric Sosman's code uses a loop, but
not for the actual printing.
 
E

Eric Sosman

Richard said:
Eric Sosman said:




...and yours doesn't meet the requirement *not* to use "any kind of
looping".

It "print 1 to 100 without using any kind of looping."
All the loops are over and done with before the printing starts,
and the printing uses no loops. To look at it another way, I'm
choosing to parse the problem statement with a low precedence
assigned to "and."
 
R

Richard Heathfield

Eric Sosman said:

It "print 1 to 100 without using any kind of looping."
All the loops are over and done with before the printing starts,
and the printing uses no loops. To look at it another way, I'm
choosing to parse the problem statement with a low precedence
assigned to "and."


Those 0s in "100" look suspiciously like loops to me! :)
 
R

Richard Tobin

... and neither CBF's nor RH's code meets the requirement to
"use loops."
[/QUOTE]
...and yours doesn't meet the requirement *not* to use "any kind of
looping".

char *loops = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 "
"21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 "
"41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 "
"61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 "
"81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100";
printf("%s\n", loops);

-- Richard
 
R

Richard Heathfield

Richard Tobin said:
...and yours doesn't meet the requirement *not* to use "any kind of
looping".

char *loops = [...]
printf("%s\n", loops);[/QUOTE]

This fails to meet the req - oh, you SOAB. :)
 
L

Lew Pitcher

Nirjhar said:
TWISTER 2.1
¿? Your have to take a decision in a program but the creator of the
programming language forgot to supply the programmer with a IF-ELE
Construct? Can you find a work arround?

Sure I can. Can't you? What do you think the trinary operator is used
for if not evaluating conditions? And that's just the most esoteric
one; there are also for(), while() and do() operations that could also
substitute for an if-else construct with some work.

OTOH, I'd fire any language developer who neglected to include if/else
in his C compiler, wouldn't you?


TWISTER 2.2
¿? You have to use loops and print 1 to 100 without using any kind of
looping not even GOTO statement? Can u do it?

Of course I can.

The simplest way is to just
puts("1 2 3 4 5 6 7 8 9 10");...
until you print all 99 of your numbers.

There are other, fancier ways, of course.
 
N

Nirjhar Oberoi

Consider this code and give ur Opinion

Code written in Turbo C
------------------------------------------------
#include <stdio.h>
#include <conio.h>

static int i = 1;

void foo() {
if (i > 100)
return;
else {
printf("%d ",i);
foo();
}
}

int main(void) {
foo();
}
 
R

Richard Heathfield

Nirjhar Oberoi said:
Consider this code and give ur Opinion

Code written in Turbo C

Well, C, pretty much, if you just lose the conio.h header, which is
non-standard and which you don't need.
------------------------------------------------
#include <stdio.h>
#include <conio.h>

static int i = 1;

void foo() {
if (i > 100)
return;
else {
printf("%d ",i);
foo();
}
}

int main(void) {
foo();
}
-------------------------------------------------

What say? will it work????
No.


Ofcourse!!!!

Of course not. Try it!
 
S

santosh

Nirjhar Oberoi wrote:

Please quote the message to which you're replying.
Consider this code and give ur Opinion

Code written in Turbo C
------------------------------------------------
#include <stdio.h>
#include <conio.h>

static int i = 1;

void foo() {
if (i > 100)
return;
else {
printf("%d ",i);
foo();
}
}

int main(void) {
foo();
}
-------------------------------------------------

What say? will it work????

Ofcourse!!!!

No, it won't. Compile it and see.
 
L

Lew Pitcher

Nirjhar said:
TWISTER 2.1
¿? Your have to take a decision in a program but the creator of the
programming language forgot to supply the programmer with a IF-ELE
Construct? Can you find a work arround?

TWISTER 2.2
¿? You have to use loops and print 1 to 100 without using any kind of
looping not even GOTO statement? Can u do it?

OK, now make certain I get my share of your marks.

#include <stdio.h>
void CountTo(int n)
{
(n-1)?CountTo(n-1):0; printf("%d\n",n);
}

int main(void)
{
CountTo(100);
return 0;
}
 
C

CBFalconer

Nirjhar said:
Consider this code and give ur Opinion

Ur was an ancient city in Mesopotamia. I don't think the
inhabitants ever heard of the C language. Makes it hard for the
city to have an opinion.
Code written in Turbo C

So what?

Non-standard include. No such thing.
static int i = 1;

void foo() {
if (i > 100)
return;
else {
printf("%d ",i);
foo();
}
}

int main(void) {
foo();
}

Failure to return a value. At least you defined main correctly.
With the corrections indicated the program will probably print "1"
some number of times and then crash. Or it might assassinate all
males over 15. Neither of these is especially desirable.
 

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
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top