I am quite newly learner of C++ and I need a help for a small problem. I find a value , which is -250 , after some calculations. What I need to do is to find smallest value after certain iterations of calculation. Let me give an example: In first iteration I find -250, in 2nd iteration -200. The first value -250 is still the smallest. In 3rd iteration , I find -50. Still, -250 is the smallest. First, my code should count how many successive iterations, the first value(-250) does not decrease. If ,let's say in third iteration, I find -300, which is smaller than 250, I should update minimum value -250 with -300 and the successive counter should be updated to 0. Thank you so much for all helps
However, this is not what I need. In my code, it compares always the newly obtained value with previuos one. What I want is, it should check successive five values, if after five iterations, if the smallest does not change we keep it and the counter becomes 5 and the loop stops. If it finds something smaller before 5 iterations, it should update the smallest value and counter should be updated to 0.
#include <iostream>
#include <limits>
#include <cassert>
double calculate() ; // this is just a declaration (it must be defined somewhere)
// check successive n values, if after n iterations, if the smallest does not change, return false
// if something smaller is found after an iteration, update smallest_so_far (note that it is passed by reference)
// and return true immediately
bool smallest_changed_from_n_iterations( double& smallest_so_far, int n )
{
// if smallest_so_far is already less than or equal to the lowest finite value of double, return false immediately
// we assume that calculate() will not return negative infinity; comment out if that is a possibility
if( smallest_so_far <= std::numeric_limits<double>::lowest() ) returnfalse ;
for( int i = 0 ; i < n ; ++i ) // iterate max n times
{
constdouble value = calculate() ; // get the value for this iteration
// if calculate() returned NaN (not a number), there is probably an error in our program and we need to fix it
assert( value == value ) ; // NaN values never compare equal to themselves
if( value < smallest_so_far ) // if something smaller is found
{
smallest_so_far = value ; // update smallest_so_far
returntrue ; // stop the iterations and return true imediately
}
}
returnfalse ; // no smaller value was found after n iterations
}
int main()
{
constexprint NUM_ITERATIONS = 5 ;
double smallest = calculate() ; // initially, this is the first value
// if the smallest does not change after 5 iterations, false is returned, and the loop stops
// If it finds something smaller after an iteration, it updates the smallest value
// and returns true immediately, and our loop continuues
while( smallest_changed_from_n_iterations( smallest, NUM_ITERATIONS ) ) ; // our loop
std::cout << "smallest value " << smallest << '\n' ;
}
@JLBorges, thank you so much for your reply. I had thought maybe we could solve this problem in a simpler way. For example by saving the the results of latest five iteration under a variable TempX and if there is no improvement in last five iteration we keep the best one
k=-infinity
TempX=-infinity
X=-Infinity
for (int t=0, t<100, t++){
x=calculate() // it finds -250.
if (x>k)) {
k= calculate()
if (x> TempX) {
but it seems, my proposition does not compare currently obtained value with the values obtained from the latest five iterations, it checks the values five by five and find smallest until iteration 100.