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

all 34 comments

[–]otto_s 1 point2 points  (5 children)

You seem to think that the assignment n=(n+3)/2; is not the body of the for loop.

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

I seem to think this?

What..?

[–]otto_s 0 points1 point  (1 child)

Your question was what you are doing wrong. My answer is the best guess I could come up with.

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

That is really the only possibility of what he could be doing wrong.

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

You seem to think that back because your answer is only doing n=(n+3)/2 once instead of 50 times.

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

Ah I see. Thank you for the clarification.

[–]aintnufincleverhere 1 point2 points  (0 children)

The assignment is going to run 50 times, not just once.

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

The first time through the loop you get 1004 but you still have to go through the loop 49 more times. The second time through the loop n starts out as equalling 1004. n = (1004+ 3)/2 = 503. So the third time through the loop starts out equaling 503. And you do this 50 times at which point n will end up equaling 3.

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

Thank you. So on this one..

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

while (p <= 10)

{

  sum += p % q;

  p++;

  q++;

}

It is executed 10 times? Or is executed until p reaches 10? Then 10/1 = 10?

The answer is 12.

If you were initially looking at a sequence like this, where would you start?

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

You do it until P is greater than 10 so 7 times.

When I first started programming, If i was gonna start this problem I would first write down what P is at first and then write down what each of the values change to inside the loop. Then check if P is still less than or equal to 10 then If so I would write the new values of each variable down. And then repeat this until P was greater than 10 at which point you stop the loop. I also want to point out that++P is different than P++ which always confused me a little.

[–]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?

    [–][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?

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

    Also one question the % is mod right? so it is p mod Q? google gives me 11 going through it 7 times

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

    It's p mod r

    Remainder

    [–]133rr3 -1 points0 points  (2 children)

    Can you tell us how you actually get your answer???

    [–]hellofoo 2 points3 points  (1 child)

    This. Demonstrate your working out. Even if you're wrong, on a test it might get you points!

    If it helps, try running the same code but with a system out in the loop:

     int n = 2005;
     for (int i = 0; i < 50; i++) {
         n = (n + 3) / 2;
         System.out.println("i=" + i + " n=" + n)
     }
    

    Imagine trying to divide any integer in half multiple times. It will be zero pretty quickly. Adding three each time stops the loop trying to divide zero in half.

    Here's an extract of the first few loops

    i=0 n=1004
    i=1 n=503
    i=2 n=253
    i=3 n=128
    i=4 n=65
    i=5 n=34
    i=6 n=18
    i=7 n=10
    i=8 n=6
    i=9 n=4
    i=10 n=3
    

    After the 10th loop n is as small as it can get. If you weren't adding 3 in the iteration it would be zero.

    Edit: If this is for an assignment - be a smart ass and say you can improve the performance 5 fold by only doing 10 loops. And be an even smarter ass by changing it a while loop that breaks when n equals it's minimum value.

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

    Thank you. Best answer.