# trouble with loop

• 10-15-2009, 04:01 AM
aamster
trouble with loop
I'm having trouble with a loop. Basically I want to calculate a variable "i" such that it is less than or equal to a number typed in by the user, which i'm calling pickUpAtTime. As well, this variable i must satisfy the equation j=i*(3+1)+1 such that j is less than or equal to a number typed in by the user, which i'm calling totalToothpicks.

Oh yeah, and i'm trying to calculate the max i that will satisfy j<totalToothpicks, which is why i did the for loop.

So right now i've got:

EDIT:
Code:

```int j =0; while(j<totalToothpicks ){                 for(i=1; i<pickUpAtTime; i++)                                                         j+=i*(3+1)+1;                                                 }```
is supposed to be:
Code:

```int j =0; while(j<totalToothpicks ){                 for(i=1; i<pickUpAtTime; i++)                                                         j+=i*(pickUpAtTime+1)+1;                                                 }```
but anyway that doesn't matter.

Why doesn't the for loop run, testing each instance of i=1 to i<pickUpAtTime, making sure that when each instance of i is plugged into the formula for j that j is less than totalToothpicks? Because for example, I'm getting answers like 13 for j when totalToothpicks is 12??
• 10-15-2009, 04:23 AM
Eranga
What I can see in your code is that simply added number from 1 to the value which is user entered. Can you send the complete error you get?
• 10-15-2009, 08:52 AM
r035198x
Why are you saying
Code:

`j=i*(3+1)+1`
not
Code:

`j=i*4 + 1`
?
• 10-15-2009, 09:32 PM
aamster
r035198x, sorry, I fixed the code, but the loop still doesn't work!!
• 10-15-2009, 10:20 PM
toymachiner62
First of all you havent given totalToothpicks a value. But if you did and forgot to post it, it's because J is inside your for loop and is more than incrementing before it exits the for loop.

For example if a user enters 3, substitute all the pickUpAtTime with 3

Code:

```pickUpAtTime = 3; int j =0; while(j<totalToothpicks ){                 for(i=1; i<pickUpAtTime; i++)                                                         j+=i*(pickUpAtTime+1)+1;                                         }```
would equal
Code:

```pickUpAtTime = 3; int j =0; while(j<totalToothpicks ){                 for(i=1; i<3; i++)                                                         j+=i*(3+1)+1;                                                 }```
with this i=1
and j=0
j = (0) + (1)*(3+1)+1 which is the same as j = 5;

now go through the loop again since i=1 which is still less than 3.
This time i=2 and j=5
j = (5) + (2)*(3+1) + 1 which is the same as j = 14;

now i = 3 which is not less than pickUpAtTime so it will exit the for loop.

Now it goes back to the while loop and checks if j < totalToothpicks.
• 10-16-2009, 07:08 PM
aamster
ok, so i don't know what's going on. I had the program print out the i values and j values inside the loop and it returned:
"i=0
j=1
i=1
j=5
i=2
j=9
i=3
j=13
There are 11 left.
The computer takes 4 toothpicks."

where the 4 in the "4 toothpicks" at the end is the i value. It only printed up to 3, though? So why is it printing 4 in this statement?? Also, for the above example, i had the totalToothpicks value as 12. But in the while loop i thought that it would check the value it returned from the for loop "13" and make sure that it is less than the totalToothpicks value. 13 is not less than 12! So, how do i write the loop so that it checks the j value after each iteration to make sure that it is less than the totalToothpicks value?

Sorry for the many questions, but hope you can help!
• 10-16-2009, 07:26 PM
toymachiner62
Code:

```pickUpAtTime = 3; int j =0; while(j<totalToothpicks ){                 for(i=1; i<3; i++)                                                         j+=i*(3+1)+1;                                                 }```
Quote:

ok, so i don't know what's going on. I had the program print out the i values and j values inside the loop and it returned:
"i=0
j=1
i=1
j=5
i=2
j=9
i=3
j=13
There is no possible way it can print i = to 0 if you are setting i = 1 in your for loop
• 10-16-2009, 07:27 PM
toymachiner62
If you want it to check the j value after every iteration don't use a for loop inside of a while.
• 10-16-2009, 07:31 PM
r035198x
We can't see what your current code looks like now so we can't say too much.
• 10-16-2009, 07:44 PM
aamster
sorry toymachine, change i=1 to i=0 in the for loop, and also what would you suggest to help fix the problem?
How can I check the j value after every iteration?
\
• 10-16-2009, 09:18 PM
toymachiner62
Post your updated code for us to help you. the problem is you are going through a loop inside of a loop.

Study this diagram, it should help with nested loops
http://users.evtek.fi/~jaanah/IntroC...flow_nest1.gif