You are invited to Log in or Register a free Frihost Account!

# Funny with C code.

test32rota
Several funny puzzles with following code:
 Code: /*-------------------------------------------------------------------*/ int i, n = 20; for (i = 0; i < n; i--) {           printf("X"); } /*-------------------------------------------------------------------*/

1 Add only one symbol to print x 20 times
Can you provide 3 solutions ?

2 Add only one symbol to print x 21 times

3 Add only one symbol to print x 1 time

4 Add only one symbol to print x many-many times.
Can you provide 3 solutions ?

5 Add only one symbol to not print x

6 Add one line only to print x 20 times

7 Add one line only to print x only once

PS: solutions can be sent later.

Good luck!
Antoine_935
Some answers are "particular", but they all compile under gcc
test32rota wrote:
Several funny puzzles with following code:
 Code: /*-------------------------------------------------------------------*/ int i, n = 20; for (i = 0; i < n; i--) {           printf("X"); } /*-------------------------------------------------------------------*/

1 Add only one symbol to print x 20 times
Can you provide 3 solutions ?

-
 Code: for (i = 0; -i < n; i--)

-
 Code:

2 Add only one symbol to print x 21 times

3 Add only one symbol to print x 1 time

4 Add only one symbol to print x many-many times.
Can you provide 3 solutions ?

- don't add anything...
-
 Code: for (i = 0; i <= n; i--)

-
 Code: for (i = 0; !i < n; i--)

-
 Code: for (i != 0; i < n; i--)

5 Add only one symbol to not print x
-
 Code: int i, n = [b]-[/b]20;

6 Add one line only to print x 20 times
add some one-line test...
 Code: if (condition) break;

7 Add one line only to print x only once

 Code: for (i = 0; i < n; i--) {           printf("X");           [b]break;[/b] }

PS: solutions can be sent later.

Good luck!
muggle
I don’t like this one:
Antoine_935 wrote:
 Code: for (i != 0; i < n; i--)

because the value of i is undefined.

And I’m not sure how to interpret the original definition of the problem – adding characters and/or lines is always performed to the original text or to the text from the previous step.
The solutions will be different if you take one or the other approach.
test32rota
muggle wrote:
I don’t like this one:
Antoine_935 wrote:
 Code: for (i != 0; i < n; i--)

because the value of i is undefined.

Agree, this is bad solution.

 muggle wrote: And I’m not sure how to interpret the original definition of the problem – adding characters and/or lines is always performed to the original text or to the text from the previous step. The solutions will be different if you take one or the other approach.

Let's be exact, you have to modify only original text, every puzzle absolutely independent.
muggle
 test32rota wrote: 2 Add only one symbol to print x 21 times

...
for (i = 0; ~i < n; i--)
...

Unfortunately this solution is machine dependant.
test32rota
I don't see C-enthusiasts more. Is it a time to publish solution ?
test32rota
Ok, I don't see more volunteers. So this is my personal solution:
 Code: ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; i--) {           printf("X"); } ------------------------------------------------------------------- 1.1 :: print x 20 times ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; n--) {           printf("X"); } ------------------------------------------------------------------- 1.2 :: print x 20 times ------------------------------------------------------------------- int i, n = 20; for (i = 0; -i < n; i--) {           printf("X"); } ------------------------------------------------------------------- 1.3 :: print x 20 times ------------------------------------------------------------------- int i, n = -20; for (i = 0; i < n; i--) {           printf("X"); } ------------------------------------------------------------------- 2 :: print x 21 times ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; i--1) {           printf("X"); } ------------------------------------------------------------------- 3 :: print x 1 time ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; i--n) {           printf("X"); } ------------------------------------------------------------------- 4.1 :: print x many-many times ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; i--i) {           printf("X"); } ------------------------------------------------------------------- 4.2 :: print x many-many times ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; -i--) {           printf("X"); } ------------------------------------------------------------------- 4.3 :: print x many-many times ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; i---) {           printf("X"); } ------------------------------------------------------------------- 5 :: don't print x ------------------------------------------------------------------- int i, n = 20; for (i = 20; i < n; i--) {           printf("X"); } ------------------------------------------------------------------- 6 :: print x 20 times ------------------------------------------------------------------- #define printf(a) (printf(a);if(i<-20)break;) int i, n = 20; for (i = 0; i < n; i--) {           printf("X"); } ------------------------------------------------------------------- 7 :: print x only once ------------------------------------------------------------------- #define printf(a) (printf(a);if(i<-1)break;) int i, n = 20; for (i = 0; i < n; i--) {           printf("X"); } -------------------------------------------------------------------
muggle
It will be interesting if you tell us which compiler you used to compile `your` solutions.
I don’t want to be too much punctual but here are my comments after compiling and running proposed solutions whit gcc 3.4.2 on SunOS 5.9:

 test32rota wrote: 1.1 :: print x 20 times ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; n--) { printf("X"); } ------------------------------------------------------------------- here you are replacing a character 1.3 :: print x 20 times ------------------------------------------------------------------- int i, n = -20; for (i = 0; i < n; i--) { printf("X"); } ------------------------------------------------------------------- 0 is never less than -20; this solution doesn't print x at all 2 :: print x 21 times ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; i--1) { printf("X"); } ------------------------------------------------------------------- syntax error 3 :: print x 1 time ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; i--n) { printf("X"); } ------------------------------------------------------------------- syntax error 4.1 :: print x many-many times ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; i--i) { printf("X"); } ------------------------------------------------------------------- syntax error 4.3 :: print x many-many times ------------------------------------------------------------------- int i, n = 20; for (i = 0; i < n; i---) { printf("X"); } ------------------------------------------------------------------- syntax error 6 :: print x 20 times ------------------------------------------------------------------- #define printf(a) (printf(a);if(i<-20)break;) int i, n = 20; for (i = 0; i < n; i--) { printf("X"); } ------------------------------------------------------------------- syntax error; if you replace () with {} it will print x 22 times 7 :: print x only once ------------------------------------------------------------------- #define printf(a) (printf(a);if(i<-1)break;) int i, n = 20; for (i = 0; i < n; i--) { printf("X"); } ------------------------------------------------------------------- syntax error; if you replace () with {} it will print x 3 times
test32rota
U-a, so many errors. I'm busy right know, but I will try to compile it on bcc32, later.

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.