Jump to content

I eat corn


Gunman

Recommended Posts

  • 2 weeks later...

So how did you get 55 then? I thought I read it as, while x is less than or equal to 10, x++ (or add one to x). That is how I came up with 11. Remember though, I am a code n00b. Just did some random scripts here or there and none in C++.

Link to comment
Share on other sites

well you set i = to 0, and you increment x each time it does the loop. Inside the loop you add i + x = i;

 

the first number is i and the second is x.

 

so heres each step of the loop

0+0=0

0+1=1

1+2=3

3+3 =6

6+4=10

10+5=15

15+6=21

21+7=28

28+8=36

36+9=45

45+10 = 55

 

x is then bumped to 11, which is greater than 10. So you fall out of the loop and print i. i is then = to 55

Link to comment
Share on other sites

Member
(edited)
int y,x;

int n=0;

 

for (x=0;x<=10;x++){

  for (y=10;y>0;y--){

      x++;

      if ((x>5)&&(y>1))

        n++;

}   

}

 

What is n?

 

you hit the first for loop and its passes and you go to the next loop. It will do the next loop and when it falls out of the inside loop, you will have y=1, x=9 and n=3. Then we go back to the first for loop again since x is incremented it will =10. Passes the loop and does the inside loop again. This time the if statement will always fail because x will always be greater than 5, so N will stay the same. hits the first loop again and fails because x will be something like 19? So N=3 and the top loop is useless?

Edited by NOFX
Link to comment
Share on other sites

×
×
  • Create New...