Hi guys I'm confused on what exactly this function is doing.
Any ideas?Code:int strangefunction(int n) { int c; for (int c = 1; c != 0; ) { if (n > 100) { n = n - 10; c--; } else { n = n + 11; c++; } } return n; }
Hi guys I'm confused on what exactly this function is doing.
Any ideas?Code:int strangefunction(int n) { int c; for (int c = 1; c != 0; ) { if (n > 100) { n = n - 10; c--; } else { n = n + 11; c++; } } return n; }
What's stopping you from tracing through it? Pick an n (say, 22, or 45, or 78) and "be the computer".
well I see what is returning when I enter in numbers, I just don't see a pattern, it just seems like just
if (n > 100)
subtract 10
else
just add 11 to n
Yes, but when does the loop end?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Well it also increments or decrements the c variable, which will impact how the loop is executed.
It may have helped to put braces on separate lines and keep it to one statement per line so that you don't miss a step.
As far as n goes, a very short loop is n = 100:
And n is 91.Code:n c 111 2 101 1 91 0
This looks like a homework question. I am surprised you got so many answers given that fact. It must have been your lucky day.
thanks guys!
I was just making sure it didn't have any mathematical relevance, which I now see it doesn't.
Well knowing what the function does isn't the homework, rewriting this function in a functional language (scheme) is the homework, so no, this isn't a homework question, I was just curious if it had any under lying relevance.
Ah ok, well in that case, we all love logical problems (particularly ones that require disproving a solution--and especially disproving one anothers' solutions).