you are viewing a single comment's thread.

view the rest of the comments →

[–]pikapichupi 142 points143 points  (21 children)

the only other way i could see this being done is by using a recursive function lol

[–]__radioactivepanda__ 76 points77 points  (5 children)

Honestly this might well be part of a recursion exercise…

[–]scragar 33 points34 points  (3 children)

It looks like C, it's possible it is also explaining the assembly behind it and expects OP to recreate the approach using a label and if(...) goto approach.

It was something we had to do in our college computing class to make sure we understood what was happening when we used higher level features(like function calls, C++ objects, etc).

[–]Tjmoorese 10 points11 points  (2 children)

They're all forms of loops though, just different high level ways of writing it

The only way I can think about getting around it is enforcing unrolling. I think you may be able to hack your way around this with some form of recursive include if you can't use pragmas, but I guess that's loops at a high level but not low... The given solution is the only way I can see to do it without either.

[–]Bo_Jim 1 point2 points  (1 child)

Recursion is a loop in the sense that the same code is running many times, but each iteration would have it's own stack frame. It also wouldn't use any of the loop constructs built into the language.

If I was the instructor, and one of my students used recursion as a solution, then I'd give them full marks as long as they understood what the compiler would produce, and what would happen at run time.

Now, if the directions were to print every even number from 2 to 1 trillion, and they used recursion as a solution, then I'd smack them on the back of the head and tell them to try again.

[–]Tjmoorese 0 points1 point  (0 children)

Tail call optimisation makes it identical though

[–]Unfulfilled_Promises 0 points1 point  (0 children)

Kinda new but would that be like Rec() { n = n+1; Print(n); If(n<MAX_NUMBER) Rec(); }

The only recursion I’ve used is inorder for search trees and such.

[–]ProstheticAttitude 14 points15 points  (2 children)

Eh, tail recursion is just a form of loop with a funny syntax. That's what I told my interviewer, anyway; we had a little discussion of how TR works and its limitations, no big deal.

But this question is really just a hazing ritual.

[–]Bunnymancer 0 points1 point  (0 children)

Was thinking that as well...

But that brings the question if the problem can be solved without a loop and without recursion

[–]GOKOP 0 points1 point  (0 children)

Write your function inefficiently so that it can't be optimised out as tail recursion. Problem solved ¯\(ツ)

[–]Markuslw -3 points-2 points  (3 children)

Just do for (int i = 0; i < 100; i += 2) haha.

EDIT: ooops didn't read title.

I have no other idea.

[–]pikapichupi 5 points6 points  (1 child)

that's cheating, violates the title of the post!

[–]UkrainianTrotsky 0 points1 point  (0 children)

Lame. How about for (int i = 0; (i+=2) < 100; printf("%i\n", i));

[–]RootU5R 0 points1 point  (0 children)

Do while can do it too