My program initially asks the user to input 3 values, x1, x2 and x3.

These get fed into a mathematical equation and come out of it the other side slightly altered.

The program then prints to screen the new values of x1, x2, and x3.

These x values must go through the equation many times, so i placed them into the equation in a loop.

if(i>0; i=100; i++)

This gave me 100 values of x1, x2, and x3 and printed them to screen, which is fine.

However, I would like to do a while loop. I would like the program to stop when the values of x1, x2, and x3 aren't changing by more than 0.0001 every time.

I thought something like:

while (x1 - x1 > 0.0001){ DO EQUATION }

But of course, this wont work. Also, I want it so it's is not only x1 that isnt changing by 0.0001 any more, it must once all of the x values have stopped changing by 0.0001.

Does anybody have any ideas?

I would put the variables in their own parenthesis's.
(x1-x2) .

This looks like it might be some asymptotic equation. So the question is if the difference will EVER be less than that value. Otherwise you might be waiting a very long time for the loop to terminate.

I'm not quite sure what you're having problems with. You can make a fourth variable that holds the previous value of x1 . Your while statement would then look something like this (I used the absolute value function abs because I'm not sure which way the variables are changing):

while ( abs(x1_old - x1) > 0.00001 ) {
   x1_old = x1;
...
}

However, this might not work due to the inconstancies of floating point numbers (I assume you're using floats to store your decimal numbers). In this case you might have to use an external math library to gain the accuracy required to handle your math.

Yeah, basically x1, x2, and x3 change every time it is run, but it changes a little bit less each time. For example:

x1: 0.56778
x2: 0.12234
x3: 0.32214

x1: 0.56877
x2: 0.12945
x3: 0.32715

x1: 0.56878
x2: 0.12948
x3: 0.32719

So we get new values each time, but they're getting less and less different.

I want it to stop when the very last (x1, x2, and x3) minus the second to last (x1, x2 and x3) is not > than 0.0001 anymore.

Doable?

Basically you would create 3 variables, x1_old , x2_old , and x3_old . You'd assign them to the original variables at the beginning of the loop (before they change). Then in the loop conditions, you'd only continue if the difference between the old variables and the new variables is more than 0.00001. However, you may experience issues with floating point precision. See the last bit of my previous post for more information regarding this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.