Thread: code fragment

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    10

    code fragment

    Code:
    what is the output of the following fragment
    int i-4;
    do{
    printf("i intermediate value is %d\n", i);
    i--;
    }while (i>0);
    printf("final value of i is %d", i);
    pls, does anyone the output to this queston
    Last edited by mikel360; 05-23-2011 at 10:05 AM.

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    10
    my answer is 1,2,3,4
    pls, am i write

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    First you need to figure out what the line that you have written as "int i-4" is supposed to be, because that's not a valid line. Then you need to step through each line, one by one, with a table of variables (or in this case, variable) and evaluate each line as it happens, one at a time.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > my answer is 1,2,3,4
    Mmm, you get that by counting backwards?

    It would take all of 30 seconds just to paste it into your code editor, compile and run it to get the answer.

    Or as tabstop says, run it "in your head" to see what it would do.
    This too is an important skill, as being able to envisage what code would do is a good step to being able to write it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The only explanation I can think of for questions like these is that it's hard to compile code when you're sitting in an exam.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    10
    that was how the question is in the exam paper.
    i dont undestand what you mean by "that's not a valid line"

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mikel360
    that was how the question is in the exam paper.
    In that case the correct answer is: the following code fragment produces no output as it results in a compile error. But it is more likely that you simply forgot the exact text, or you have a typographical error.

    Quote Originally Posted by mikel360
    i dont undestand what you mean by "that's not a valid line"
    It results in a compile error because the operator - at that point is invalid. It probably should be:
    Code:
    int i = 4;
    The point is this: in the exam, and for practice, you should trace over the code yourself, mentally and/or on paper. But to check your answers, instead of asking us, write a program with this code fragment, compile and run it and observe the output for yourself.
    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

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    10
    when i compiled and run it, it was an error message showing though am suppose to use my head
    thanks for your feedback

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mikel360 View Post
    my answer is 1,2,3,4
    pls, am i write
    Ummm, no... the output of that code, exactly as presented, would be "Syntax Error"...

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    10
    what about if the code starts with

    [code]
    int i=4;
    [\code]

    what will be the output

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Why don't you actually use your brain and think about it!? If you can't figure this out, programming probably isn't for you.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mikel360
    what will be the output
    It would be:
    Code:
    i intermediate value is 1
    i intermediate value is 2
    i intermediate value is 3
    i intermediate value is 4
    final value of i is 5
    Of course, I might not be telling the truth

    To find out the truth, compile and run the code yourself.
    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
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mikel360 View Post
    what about if the code starts with

    [code]
    int i=4;
    [\code]

    what will be the output
    BE the computer... i = 4 ... ok, now what happens next?
    Step by step... just like the computer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please explain me this C code fragment
    By stefboombastic in forum C Programming
    Replies: 6
    Last Post: 12-16-2010, 01:29 PM
  2. Question about red-black tree and little code fragment.
    By mindvera in forum C++ Programming
    Replies: 0
    Last Post: 07-05-2010, 11:42 AM
  3. what is the output of this code fragment?
    By ryan_qut in forum C Programming
    Replies: 3
    Last Post: 11-13-2004, 02:05 PM
  4. Fragment of C++ code
    By nsimos81 in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2003, 03:49 PM
  5. code fragment pls help
    By Damo in forum C Programming
    Replies: 2
    Last Post: 04-15-2002, 09:57 AM