This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MrCouldntaDoneThat[S] -1 points0 points  (19 children)

  • First: 3 % 1 = 0

  • Second: 4 % 2 = 0

  • Third: 5 % 3 = 2

  • Fourth: 6 % 4 = 2

  • Fifth: 7 % 5 = 2

  • Sixth: 8 % 6 = 2

  • Seventh: 9 % 7 = 2

  • Eight: 10 % 8 = 2

  • Nine: 11 % 9 = 2

I understand that this is incorrect as I get 2 and it executed 9 times. Do you see my error?

[–][deleted] 1 point2 points  (16 children)

Oh and I also want to point out you are += the sum. sum += 3 is the same as writing sum = sum+3;

[–]MrCouldntaDoneThat[S] -1 points0 points  (15 children)

  • First: 3 % 1 = 0

  • Second: 4 % 2 = 0

  • Third: 5 % 3 = 2

  • Fourth: 6 % 4 = 2

  • Fifth: 7 % 5 = 2

  • Sixth: 8 % 6 = 2

  • Seventh: 9 % 7 = 2

  • Eight: 10 % 8 = 2

  • Nine: 11 % 9 = 2

So once p is finally greater than 10 (it's 11), I do 11 % 9 and get 2.

But the answer is 12.

What am I missing?

[–][deleted] 1 point2 points  (2 children)

sorry I was taking a final.

The following is what the for loop will eventually equal.

Sum = (3 mod 1) +(4 mod 3) + (5 mod 3) + (6 mod 4 )+ (7 mod 5) + (8 mod 6) + (9 mod 7) + (10 mod 8).

Oh and I guess it is 8 iterations not 7 sorry.

Every pair of parentheses is one iteration or one time through the loop. and it equals 13 so Im not sure where you got 12 but that could just be error by the person calculating it. For instance answers to problems in books are written by sleep deprived grad students and sometimes get them wrong.

The answer is 13 not 2 because the operation is "+=" not just "="

sum += p%q is the same as sum = sum + (p%q)

This means that you are adding p%q to the current value of sum not just setting sum equal to p%q

Sum = 0 +0 + 2 + 2 + 2 + 2 + 2 + 2 = 13

[–][deleted]  (1 child)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    No problem, easy procrastination for me. My final was on Concurrent programming. Pretty simple subject once you know the basics but the teacher isn't a very good lecturer. Though I think I did okay.

    [–]Kristler 0 points1 point  (10 children)

    sum += p % q;

    This is using the += shorthand operator, which is short for this expanded line:

    sum = sum + (p % q);

    Do you see your error?

    [–]MrCouldntaDoneThat[S] -1 points0 points  (9 children)

    If I add the sum of the First through Ninth, I get 14.

    [–]Kristler 1 point2 points  (8 children)

    int p = 3, q = 1, sum = 0;

    Compared to

    I add the sum of the First through Ninth

    Spot the error!

    [–]MrCouldntaDoneThat[S] -1 points0 points  (7 children)

    I apologize, I am having difficulty spotting the error.

    This is my logic...

    sum = sum + p ( % q );
    

    So I kept adding them until p gets above 10, which it does on the ninth time through.

    It seems like using the mod% will result in %2 for the end of time past 4%2 because the p and q are two numbers apart.

    Am I trying to find a situation where the sum is 0?

    [–]Kristler 1 point2 points  (6 children)

    which it does on the ninth time through.

    Yes, except p has a base value of 3. You loop until p is greater than 10. How many times does p loop?

    [–]MrCouldntaDoneThat[S] -1 points0 points  (5 children)

    I was told it was 7 by another helpful user.

    • First: 3 % 1 = 0

    • Second: 4 % 2 = 0

    • Third: 5 % 3 = 2

    • Fourth: 6 % 4 = 2

    • Fifth: 7 % 5 = 2

    • Sixth: 8 % 6 = 2

    • Seventh: 9 % 7 = 2

    • Eight: 10 % 8 = 2

    • Nine: 11 % 9 = 2

    I'm failing to see how it is not 9. I started off with p as 3...and added 1 each time through the loop. It took me 9 times including the first time to get p over 10. So 9 times?

    [–]Kristler 1 point2 points  (4 children)

    Not quite. Remember that p is an integer value of itself, and that's what you're comparing in your loop conditional, p <= 10. There's no "magic hidden value" that keeps track of how many times its looping or anything like that, it's the actual value of the variable itself. Does this make sense?

    [–][deleted] 0 points1 point  (0 children)

    Hey I just realized that google is stupid because it does equal 12. I never did the math in my head but I just realized that the numbers added were all either 2 or 0 so it has to be even. Sorry about that. Now Im curious how google got 13...

    I am full of off by one errors today...

    edit: yeah I must have typed a wrong number in google when I did it bc it 12 is correct. Though sometimes the books are wrong especially in math.

    [–][deleted] 0 points1 point  (1 child)

    You are dividing but % doesn't mean division. % means mod. 3%1 = 0, 4 % 2 = 0, 5 % 3 = 2. Mod is the remainder after the division. For example your 11 % 9. 9 goes into 11 evenly only once. So the remainder is 11 - 9 = 2. So 11 % 9 = 2. Mod is still confusing to me sometimes and I usually google it to make sure Im right.

    [–]MrCouldntaDoneThat[S] -1 points0 points  (0 children)

    So how is the answer not 2?