Thread: I have a midterm tomorrow and I'd like to know if I have these questions right.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    9

    Exclamation I have a midterm tomorrow and I'd like to know if I have these questions right.

    The example from the previous slide was:
    Code:
    sum = n = 0;
    while (sum <= 1000)
    {
    n++;
    sum += n;
    }
    printf("The first sum > 1000 is %d, and n is %d\n", sum, n);
    Since we now know how to use the increment operators in expressions, we could get the same results by replacing the above while statement
    with exactly one of the following:

    Code:
    while (sum <= 1000)
    sum += n++;
    or

    Code:
    while (sum <= 1000)
    sum += ++n;
    ALSO:

    Code:
    for (n = 1; n <= 50; n += 1)
    printf("%8d %8d %8d\n", n, n * n, n * n * n);
    What is the value of n for the above for loop?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I can't tell you if you're right because you haven't actually tried to answer either of those. Tell us what you think the answer should be and why, and we'll tell you if you're right. You need to be the compiler/computer. Get out a pencil and paper, and keep track of the value of each variable after each statement. Run through the code by hand for several iterations of the loops.

    For the first one, you need to know the difference between ++n and n++. Is the value of n used before or after the increment?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    For the first one, I think it would be

    sum+ = ++n
    because in the first part, n++ is before the sum+=n.

    For the second one, I think the final value of n will be 50, because it says n<= 50, therefore after testing the 51th value, the program will stop because 51>50.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CProgWiz View Post
    What is the value of n for the above for loop?
    What do you mean by that? While the loop is running, or after it is done running? It has a bunch of values while it's running. It has another value once it's done.

    Also, x = ++n and x = n++ give different values for x.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    Code:
    for (n = 1; n <= 50; n += 1)
    printf("%8d %8d %8d\n", n, n * n, n * n * n);
    What is the value of n for the above for loop?[/QUOTE]


    For that I meant to type, what value is n once the above forloop finishes. I think it would be 50, but I'm not sure.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Does it not occur to you to simply type these things up, compile them and see for yourself?

    You can grade your own test paper...

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    Actually yeah I did do that now hahahahahahhahahahahaha

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    But seeing what a compiler does gives you a quandary if your interpretation differs from that of the compiler. In that case, is the compiler wrong or you? Compilers do get things wrong - they just do it repeatably.

    More importantly, for an exam where you don't have a compiler at hand, can you explain why you expect one behaviour instead of another. If you cannot explain why, you have a lesser chance of reasoning out the right answer to a similar, but slightly different, question.
    Quote Originally Posted by CProgWiz View Post
    For that I meant to type, what value is n once the above forloop finishes. I think it would be 50, but I'm not sure.
    You are mistaken on that one. Look more closely.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello all,need help with test tomorrow
    By Freikorp in forum C Programming
    Replies: 10
    Last Post: 05-27-2011, 02:14 PM
  2. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  3. C++ Midterm understanding
    By KoshiB in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2006, 05:20 PM
  4. Midterm Tomorrow, Link List help
    By WildLyxn in forum C++ Programming
    Replies: 2
    Last Post: 11-20-2002, 09:25 AM

Tags for this Thread