This is probably something I should be able to figure out, but I'm a bit stuck and would be very grateful for any help from an inspired and very patient person!
I have written C++ code for doing a calculation. The calculation results are stored in a vector. What I want to do next is:
1) Check that each element of the vector is positive.
2) Write a rather complicated conditional statement. If the condition is not met, I want to change the value of one variable (let's call it "int dt" (it's a timestep) and go through the loop again.
It seems like I should use a "do" loop, but I am confused about how to write the condition! I have written do loops with simple conditions before, but maybe the conditions I need here are too complicated? Any advice about the best approach would be very much appreciated.
In case you're interested, here's the condition I would like to implement:
At the end of the calculation, once the vector contains the results, I want to:
1) Check whether each element is positive. If it is, the calculation is over.
2) If any element is negative, I want to (i) compare that element's value to the initial (pre-calculation) value. Then I want to take the absolute difference between the initial (positive) and final (negative) values. For example, if 4 is the initial and -1 is the final, the difference would be 5.
3) Then I want to calculate the proportion of 5 that is above 0. Here it would be 0.8.
4) Then I want to scale int dt to 0.8 of its existing value. So if it was originally 1 second, I would like to rescale it to 0.8 second.
For example:
void main ()
{
int dt = 1; // Timestep
vector<double> cat(5, 1); // Make container for calculation results and put in initial values
do
{
// do calculation and fill vector cat with results
} while (???); // Here's my problem. Is it possible to implement the complicated conditions I described above, or should I be using a completely different approach (not a do/while loop)?
}
Thanks a lot.
I have written C++ code for doing a calculation. The calculation results are stored in a vector. What I want to do next is:
1) Check that each element of the vector is positive.
2) Write a rather complicated conditional statement. If the condition is not met, I want to change the value of one variable (let's call it "int dt" (it's a timestep) and go through the loop again.
It seems like I should use a "do" loop, but I am confused about how to write the condition! I have written do loops with simple conditions before, but maybe the conditions I need here are too complicated? Any advice about the best approach would be very much appreciated.
In case you're interested, here's the condition I would like to implement:
At the end of the calculation, once the vector contains the results, I want to:
1) Check whether each element is positive. If it is, the calculation is over.
2) If any element is negative, I want to (i) compare that element's value to the initial (pre-calculation) value. Then I want to take the absolute difference between the initial (positive) and final (negative) values. For example, if 4 is the initial and -1 is the final, the difference would be 5.
3) Then I want to calculate the proportion of 5 that is above 0. Here it would be 0.8.
4) Then I want to scale int dt to 0.8 of its existing value. So if it was originally 1 second, I would like to rescale it to 0.8 second.
For example:
void main ()
{
int dt = 1; // Timestep
vector<double> cat(5, 1); // Make container for calculation results and put in initial values
do
{
// do calculation and fill vector cat with results
} while (???); // Here's my problem. Is it possible to implement the complicated conditions I described above, or should I be using a completely different approach (not a do/while loop)?
}
Thanks a lot.