iteration logic help

Nov 2, 2014 at 11:48pm
hey guys i need help with iteration logic.
i need help with
1-prints out the integers between 2 and 1,048,576 (2^20) that are integer powers of 2.
2-prints out the integers between 1,048,576 down to 2 that are integer powers of 2
for 2nd question does this looks correct?
1
2
3
4
5
  for (int i = 1048576; i >= 2; i >>= 1)
   {
      if ((i &(i - 1)) == 0)
         cout << i << " ";
   }
Nov 3, 2014 at 12:41am
anyone?
Nov 3, 2014 at 12:42am
Not sure what the question is and what you want to do?
Nov 3, 2014 at 12:45am
prints out the integers between 2 and 1,048,576 (2^20) that are integer powers of 2
and
prints out the integers between 1,048,576 down to 2 that are integer powers of 2
Nov 3, 2014 at 12:54am
What is the purpose of line 3?
Nov 3, 2014 at 12:57am
do i not need that line?
Nov 3, 2014 at 1:04am
I am not going give you that determination. What do you think it is doing?

What happens when you run the original code?

What happens when you run the code with line 3 removed?

I am beginning to think that you found the code (on the internet or else where) and did not write it? So if you want more of my help, please explain what each part of the code you showed does in English (or your native language)? As a step for that, consider rewriting the for loop in its while loop equivalent. That is a good exercise under many circumstances.
Last edited on Nov 3, 2014 at 1:05am
Nov 3, 2014 at 1:12am
closed account (48T7M4Gy)
@akai09

Just as everybody else is saying p[lease show us the code you have for part 1. Part 2 naturally follows from it.

As a start part 1 is:

set y to 1

for values of x from 0 to 20
double the current value of y
print out y
loop on incrementing x

Part 2 is the reverse of this once you have the outcome of part 1.

Topic archived. No new replies allowed.