How do I write a loop for this?

Apr 21, 2009 at 1:14pm
If I have a random number and if it is even divide it by 7 and if its odd multiply it by 4 and it keeps doing this untill it is equal to 0.

1
2
3
4
5
6
7
int a = 111
int counter = 0

    for (a;a%2==1;a*4)
         for( ??;??;??)

cout<< counter<<endl;
Apr 21, 2009 at 1:22pm
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    int n = 298;
    int counter ;
        for (n;n%2==1;n*4)
            for (n;n%2==0;n/7)
                ++n;
                
                
            
        cout<< counter <<endl;



i do this.. and I get 67. which is wrong.
and I realised if I made counter = 0 it will always output the answer 0
dont know what Im doing wrong.
Apr 21, 2009 at 1:26pm
pseudo code
n = random value <- Initialization
counter = 0 <- Initialization

while n ≠ 0 <- Condition

if n%2 |
n*=4 |
else | Increase
n/=7 |
counter++ |


If you need a for, the form is for (initialization,condition,increase)
You would need the ?: operator for the increase part in this case or you can move it in the block
Last edited on Apr 21, 2009 at 1:29pm
Apr 21, 2009 at 1:44pm
Im confused.
Apr 21, 2009 at 8:42pm
1
2
3
4
5
6
7
8
9
10
11
for(int n = RANDOM, counter = 0; n != 0; ++counter)
{
if(n%2)
{
  n *= 4;
}
else
{
 n /= 7;
}
}
Last edited on Apr 21, 2009 at 8:42pm
Topic archived. No new replies allowed.