Thread: CAN ANYONE HELP ME to write a c program using only "one for loop" and without using a

1. CAN ANYONE HELP ME to write a c program using only "one for loop" and without using a

1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100

2. I'm declaring this the worst question of the month, and it's only the first day!

"...and without using a..."
You put the entire question in the title, but it lost key information at the end.
Using a what?
- Power Drill
- Sheet folder
- copy of TurboC

> 1 2 4 3 6 9 4 8 12
I'm guessing there is some formatting going on here, but who the hell would know from your hasty puke on the forum.

How To Ask Questions The Smart Way

3. If I had to take a shot in the dark, I'd say they were going for "without using a goto".

EDIT: A like for anyone who decipher those numbers

4. Look at it this way and it becomes obvious.
Code:
1
2 4
3 6 9
4 8 12 16
...

5. My guess is this.
Code:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100
My answer is 2 lines of code inside a single for loop.

6. I'm just not as smart as you guys.

Code:
#!/usr/bin/perl

#1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8
#16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90
#100

#argh. I can't see a pattern...oh well.
for my \$i (1 .. 55){
if(\$i == 3){
print '4';
}elsif(\$i == 4){
print '3';
}elsif(\$i == 5){
print '6';
}elsif(\$i == 6){
print '9';
}else{ #etc...
print \$i;
}
}

7. Originally Posted by Salem
My answer is 2 lines of code inside a single for loop.
care to share a hint about how you solved this with two lines of code?

8. care to share a hint about how you solved this with two lines of code?
I have no idea how Salem did it, but I was able to make it happen using some comma-operator trickery.

9. PM'ed code to Elkvis and Matticus

10. Originally Posted by Salem
PM'ed code to Elkvis and Matticus
May I have take a gander at that too, please?

11. Originally Posted by Matticus
I have no idea how Salem did it, but I was able to make it happen using some comma-operator trickery.
Well, if you use the ternary operator, you can easily do it with a single instruction (in the body of the loop).

The body of my loop:
Code:
fprintf(stdout,"%d ", k);
Output:
Code:
1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100
* HINT: Don't forget you can do as many assignments as you need in the loop instruction.

12. I did not even think of the ternary operator before when I was trying to figure this out. I used ... other methods, to achieve the two-line result.

Also, my solution incorporated newlines per post #5.

13. Effectively you can consider commas and the ternary operator to be cheating as they would both be evaluating multiple instructions in one statement. Though, admittedly, I can't see the math I think I need to eliminate the condition. With the way I'm using the ternary operator putting the newline characters is trivial as the loop maintains both the base term of each line and a sequential figure. Suffice it to say there is more than one way to cook this egg. Mine might just have a bit too much salt.

14. I was about to figure out what Salem did, but then I stopped caring and tried to do it even shorter in python.
Code:
for k in [[j*i for i in range(1, j+1)] for j in range(1, 11)]: print(k)

15. It looks like there has still been no clarification. In the absence of clarification, I'd ask: why on earth would anyone need a loop to print a single string once? Unless you want to format the string over multiple lines, a correct solution here is a one-liner, not a two-liner, and a loop is unnecessary complication.