Thread: Need help with mock exam question

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    Need help with mock exam question

    Hi,

    I have an exam coming up and I need to complete this mock exam. If i post the code, can anyone tell me if my understanding is correct and where to look at next:

    What will this code output:
    Code:
    int i=1, j=1
    for (i=0; i>0; i--)
    {
          j += i*(i-1);
    };
    printf("%d",j+1);
    What I understand is this. First i and j are defined as intergers, 1 value for both. Then there is a for loop, with the starting condition as i=0, theloop continues while i is more than zero and 1 is taken away from i each loop.

    The problem I have up to this point is initially i is set as 1, but then in the loop x is set at 0? Also what does += mean?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is a trick question; the loop won't iterate so the answer is 2.

    Code:
    i=0; i>0;   // hmmm
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Hi,

    Thank you for the help. Im going to need to know how to answer these questions though so i still need a little help getting the answer.

    It seems like i is defined twice with two different values (0 and 1). Is this correct? If so, what does this mean? Which value is followed?

    Also does += mean more than or equal to? Much like >=.

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    x+=y means x = x + y
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I went completely through one of these the other day with transgalactic2 and at the very end, after I manage to demonstrate that the 60-70 of obfuscationism by his prof didn't do anything at all, s/he tells me the real question now is "what is the return value of what2"?

    So in the Original Post,
    What will this code output:
    This code will output "2". Now:
    It seems like i is defined twice with two different values (0 and 1). Is this correct? If so, what does this mean? Which value is followed?
    This code does nothing. This code does not mean anything. You will never, ever, ever, ever, ever see this code in a real program. Ever. So I don't know what you are suppose to do with it, analysis wise, but there is no point in asking "what does this mean".

    But that is correct, i is defined twice [no, I take that back, see below]. The first definition is superfluous, but part of the declaration, which is necessary.
    Last edited by MK27; 04-11-2009 at 07:17 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    x += y is the same as x = x + y, except that x is evaluated only once. This matters if x is an operation with side effects, like "++ptr".
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by spadez View Post

    It seems like i is defined twice with two different values (0 and 1). Is this correct? If so, what does this mean? Which value is followed?
    No, look again. i is define once. The second time it is simply assigned a new value. Redefining i would require "int i=0". This is valid only in C99.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I assent to King Mir on this one. Unless you want to distinguish between redefinition (i=) and redeclaration (int i=).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Hi guys, thank you so much for the help. So that I understand this, i is being declared at the top and assigned the value 1. Below in the code it is then reassigned (but not redeclared) a different value, in this case 0.

    So it would make no difference if the code at the top was instead:
    Code:
    int i
    int j=1
    Therefore not giving i a value, since it will be set a value later on in the loop.

    To check my understanding of the next part, if x=1 and y=2, would the code:
    Code:
    x += y
    Mean that the new value of X would be 3?
    (3[x] = 1[x] + 2[y])

    Finally printf("%d",j+1); would just simply add one to the value of j and then print its value?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by spadez View Post
    Mean that the new value of X would be 3?

    Finally printf("%d",j+1); would just simply add one to the value of j and then print its value?
    1) Yes, "+=" adds the right side to the left side.
    2) Yes.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  3. sort problem for an exam
    By rjeff1804 in forum C Programming
    Replies: 10
    Last Post: 02-12-2003, 10:33 PM
  4. another exam question
    By rjeff1804 in forum C Programming
    Replies: 4
    Last Post: 02-12-2003, 10:29 PM
  5. The AP Exam.....
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 02-10-2003, 09:46 PM