Finding minimum value after iterations

Hello everyone,

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

Last edited on
OK, so take one step at a time, do you know how to loop over these values?
@Peter87, yes sure , let me show what I did:


k=-infinity
for (int t=0, t<100, t++){
x=calculate() // it finds -250.
if (k>x){

k=calculate();
counter=0;}
else{
counter= counter+ 1; }
}

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.
If I have understood the requirements correctly, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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() ) return false ;

    for( int i = 0 ; i < n ; ++i ) // iterate max n times
    {
        const double 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
            return true ; // stop the iterations and return true imediately
        }
    }

    return false ; // no smaller value was found after n iterations
}

int main()
{
    constexpr int 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) {

TempX = x;
counter= 0;
}
}
else { counter= counter+ 1; }

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.
Last edited on
Topic archived. No new replies allowed.