1. Member Join Date
Oct 2009
Posts
8
Rep Power
0

## 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:
Java Code:
```int j =0;
while(j<totalToothpicks ){
for(i=1; i<pickUpAtTime; i++)

j+=i*(3+1)+1;

}```
is supposed to be:
Java 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??
Last edited by aamster; 10-15-2009 at 09:30 PM.  Reply With Quote

2. ## 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?  Reply With Quote

3. Senior Member Join Date
Aug 2009
Posts
2,388
Rep Power
14

## Why are you saying
Java Code:
`j=i*(3+1)+1`
not
Java Code:
`j=i*4 + 1`
?  Reply With Quote

4. Member Join Date
Oct 2009
Posts
8
Rep Power
0

## r035198x, sorry, I fixed the code, but the loop still doesn't work!!  Reply With Quote

5. Member Join Date
Oct 2009
Posts
63
Rep Power
0

## 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

Java Code:
```pickUpAtTime = 3;

int j =0;
while(j<totalToothpicks ){
for(i=1; i<pickUpAtTime; i++)

j+=i*(pickUpAtTime+1)+1;

}```
would equal
Java 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.  Reply With Quote

6. Member Join Date
Oct 2009
Posts
8
Rep Power
0

## 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!  Reply With Quote

7. Member Join Date
Oct 2009
Posts
63
Rep Power
0

## Java Code:
```pickUpAtTime = 3;

int j =0;
while(j<totalToothpicks ){
for(i=1; i<3; i++)

j+=i*(3+1)+1;

}```
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  Reply With Quote

8. Member Join Date
Oct 2009
Posts
63
Rep Power
0

## If you want it to check the j value after every iteration don't use a for loop inside of a while.  Reply With Quote

9. Senior Member Join Date
Aug 2009
Posts
2,388
Rep Power
14

## We can't see what your current code looks like now so we can't say too much.  Reply With Quote

10. Member Join Date
Oct 2009
Posts
8
Rep Power
0

## 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?
\  Reply With Quote

11. Member Join Date
Oct 2009
Posts
63
Rep Power
0

## 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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•