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;

}

Printable View

- 09-05-2008mr_coffeeCan someone explain to me what this function is doing?
Hi guys I'm confused on what exactly this function is doing.

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;

}

- 09-05-2008tabstop
What's stopping you from tracing through it? Pick an n (say, 22, or 45, or 78) and "be the computer".

- 09-05-2008mr_coffee
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 - 09-05-2008matsp
Yes, but when does the loop end?

--

Mats - 09-05-2008whiteflags
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:

Code:`n c`

111 2

101 1

91 0

- 09-05-2008master5001
This looks like a homework question. I am surprised you got so many answers given that fact. It must have been your lucky day.

- 09-05-2008mr_coffee
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. - 09-05-2008master5001
Ah ok, well in that case, we all love logical problems (particularly ones that require disproving a solution--and especially disproving one anothers' solutions).