Thread: Can someone explain to me what this function is doing?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    47

    Question Can 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;
    }
    Any ideas?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What's stopping you from tracing through it? Pick an n (say, 22, or 45, or 78) and "be the computer".

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    And n is 91.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This looks like a homework question. I am surprised you got so many answers given that fact. It must have been your lucky day.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    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.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah ok, well in that case, we all love logical problems (particularly ones that require disproving a solution--and especially disproving one anothers' solutions).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM