Thread: Output for ++i*++i*++i ?

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Question Output for ++i*++i*++i ?

    Hi,

    This question is from my exam:

    Predict the output of the following program:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	int i=3,j;
    	j=++i*++i*++i;
    	cout<<j;
    	return 0;
    }
    I solved it like this:

    4*5*6= 120
    But the correct answer (according to the teacher) is:

    6*6*6 = 216
    When I compile this program, it gives warning: operation on ‘i’ may be undefined

    and then prints out 150 as the output!

    Please help me figure out what's wrong.
    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The teacher is right. 216 is a correct answer. You are also right. 120 is a correct answer. The compiler is right. 150 is a correct answer.

    The reason is that the behaviour is undefined. Read Stroustrup's answer to the FAQ: What's the value of i++ + i++?

    EDIT:
    In case you were interested, MSVC8 (Microsoft Visual C++ 2005) gives the answer as 216, the MinGW port of g++ 3.4.5 gives the answer as 150.
    Last edited by laserlight; 03-03-2008 at 01:42 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by laserlight View Post
    The teacher is right. 216 is a correct answer. You are also right. 120 is a correct answer.
    I will state it another way - You and teacher are wrong.
    The compiler is right - behavior is undefined.

    And as a result correct answer will be:
    Result of the progam is unpredictable. -1 is also a reasonable good result for operation involsing undefined behavior...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I will state it another way - You and teacher are wrong.
    The compiler is right - behavior is undefined.
    They can write their own compilers to produce the answers they like
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Unfortunately your teacher has neglected to teach you about sequence points.
    Quote Originally Posted by Wikipedia
    A sequence point in imperative programming defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation.
    Full article here.

    Assignment is an operation that has side effects. This was mostly explained to me by Gerald J. Sussman when he talked about it during his lecture videos based on SICP.

    The idea being that i initially has a value, and that value changes when you assign it a new one (of course). This is a problem because the change in value of i has effected the correctness of any result produced by previously exzecuted code depending on it's value.

    As the article states, it's come up for you in this code, because due to a lack of sequence points, the compiler is free to evaluate your math equation in several ways, and be "right."

    Fix i's value before you cube it.
    Last edited by whiteflags; 03-03-2008 at 01:52 AM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by laserlight View Post
    They can write their own compilers to produce the answers they like
    That what comes to my mind. I like -1 answer. I think it should be returned for any operation relying on the undefined behavior for consistency.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Thanks for all the answer.

    But I won't argue with my teacher on that one, coz if I do, he will simply execute that thing in Turbo C++ and show me the result. "216"

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Just as showing numbers from 1 to 99 that all are less than 100 to prove that all natural numbers are less than 100
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by abk View Post
    Thanks for all the answer.

    But I won't argue with my teacher on that one, coz if I do, he will simply execute that thing in Turbo C++ and show me the result. "216"
    Not only does your teacher preach undefined behavior, but uses Turbo C, as well...
    I feel sorry for you :/
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by Elysia View Post
    Not only does your teacher preach undefined behavior, but uses Turbo C, as well...
    I feel sorry for you :/
    No need to feel sorry. I'm a member of cboard, where I get my problems solved. I just get some good sleep in his lectures

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Then I feel sorry for your classmates.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But I won't argue with my teacher on that one, coz if I do, he will simply execute that thing in Turbo C++ and show me the result. "216"
    One tactic would be to ask your teacher for help in explaining the output of this program:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	int i=3,j;
    	j=i++ + i++;
    	cout<<j;
    	return 0;
    }
    Ensure you have a computer with Internet access handy. When your teacher is nearly done with the explanation, say that you got this from Stroustrup's homepage, and then present that FAQ that I linked to. If your teacher happens not to know who Stroustrup is, click on the "bio" link. The claim that 'Bjarne Stroustrup is the designer and original implementer of C++ and the author of "The C++ Programming Language"' can easily be verified by borrowing a copy of that book from your local library. Plus, if you have a copy of the C++ Standard, you might find something like this in the acknowledgements section: "The C++ programming language as described in this International Standard is based on the language as described in Chapter R (Reference Manual) of Stroustrup: The C++ Programming Language (second edition, Addison-Wesley Publishing Company, ISBN 0–201–53992–6, copyright &#169; 1991 AT&T)."

    Of course, you probably have little to gain from this in terms of marks: if I was your teacher, I would just void the question... so those who guessed the answer that matched the model answer would get a little upset with you
    Last edited by laserlight; 03-03-2008 at 07:19 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Of course, you probably have little to gain from this in terms of marks: if I was your teacher, I would just void the question... so those who guessed the answer that matched the model answer would get a little upset with you
    Or give everyone who answered a numeric answer "half score", and full points for "undefined behaviour".

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM