Hello, before I post my question I just wanted to thank those who helped me last time I posted. I was seriously considering dropping my class thinking that I wouldn't be able to do it. However, with my last hw assignment I came here and Niek_e especially pointed out some of my errors. I stayed up until around 3 am that night but I finally figured it out. So I just wanted to say thanks. Now my question. Here's what I'm supposed to do:

1. After following statements have executed, find hte value of the variable a.

int a=5;
while (a<=12)
       a=a*2;

My main question is when I plug 5 in for a and get a=10, does that 10 replace the a=5 at the beginning of the problem? So for this problem my answer would be 10, correct?

2.

int a=2
   while (a<=25)
          a=a*a;

and for this one when I multiply 2 by itself and get 4, does that 4 replace the 2? So my answers would be...4 and 16?

Thanks, and hopefully I'm doing this correctly.

Thank you :cheesy:
Problem #1:

So for this problem my answer would be 10, correct?

Correct.
Problem #2:

So my answers would be...4 and 16?

Well, the value of 'a' would first be 2, then 4 and then 16, so you're correct. But I wouldn't call it answers.

Edited 6 Years Ago by Nick Evan: n/a

Thanks again for the help. I have another question though.

find the value of the variable a

int a=2, i=0;
while (i<=5){
       a=a+i;
       i++;
}

I'm not really sure how to do this one. i++ = 1, so the first time through a= 2, i=1. Then a=3, i=2 and that's where the program would end, right? Sorry if that doesn't make sense.

I'm not really sure how to do this one. i++ = 1, so the first time through a= 2, i=1. Then a=3, i=2 and that's where the program would end, right? Sorry if that doesn't make sense.

No, the i++ is executed AFTER a=a+i, so it starts with a =2 & i=0
Then: while (i<=5){ i starts at '0' and ends with '5', so the code will be executed 6 times:

times   a   i   | new-a (output..)
---------------------
  1      2   0  |   2
  2      2   1  |   3
  3      3   2  |   5
  4      5   3  |   8
  5      8   4  |   12
  6      12  5  |   17

That's it!

Edited 6 Years Ago by Nick Evan: n/a

for the original problems, the answers should be 20 and 256. Since it's a loop, it'll repeat until the condition is false (i.e. until a>12 and a>25 respectively).

Comments
Good catch, totally missed it.... -Niek E
This article has been dead for over six months. Start a new discussion instead.