Autoscale complex function
C++
Comments indicate how code works.
#include&lt;iostream&gt;
#include&lt;vector&gt;
using namespace std;

int main(){
    // input stores input, sum_values stores the sum of values in the valid range after multiplying
    vector&lt;double&gt;input;
    vector&lt;double&gt;sum_values;

    // stores input, and ends of range
    double num, lower, higher;

    cout&lt;&lt;&quot;Enter the array of values, use 0 to exit: &quot;;

    // input
    while(1){
        cin&gt;&gt;num;
        if(num==0)break;
        input.push_back(num);
    }

    cout&lt;&lt;&quot;Enter the mimimum and maximum values: &quot;;

    cin&gt;&gt;lower&gt;&gt;higher;

    // if array is empty or lower is greater than higher, print 1 and exit
    if(lower&gt;higher||input.size()==0){cout&lt;&lt;&quot;1&quot;;return 0;}

    // iterate through all values between .0001 and .9999
    for(double d=.0001;d&lt;1;d+=.0001){
        
        // check each d for valid ranges
        for(int i=0,valid=0;i&lt;input.size();i++){
            
            // check if input * d is in valid range
            if(d*input[i]&gt;=lower&amp;&amp;d*input[i]&lt;=higher)valid++;

            // store final number
            if(i==input.size()-1)sum_values.push_back(valid);
        }
    }

    // iterate through compiled sum of ranges and find highest
    int position=0, biggest=-.1;
    for(int i=0;i&lt;sum_values.size();i++){

        // return value closest to 1 for equal values
        if(sum_values[i]&gt;=biggest){
            biggest=sum_values[i];
            position=i;
        }
    }

    // print value
    cout&lt;&lt;(double)position*.0001;

    return 0;
}