Problem with implementation BST(without array) in C

  Kiến thức lập trình

The program takes input parameters such as the number of elements (n) and a target value (t), as well as arrays representing distances (s) and velocities (v).
It aims to calculate a value ‘k’ such that the sum of distances divided by the adjusted velocities (‘realSpeed’) equals the target value ‘t’ as closely as possible.
The calculateK function computes the difference between the target value ‘t’ and the sum of distances divided by the adjusted velocities, given a candidate value ‘k’.
The findK function utilizes binary search to find the optimal value of ‘k’ that minimizes the difference between the target value ‘t’ and the calculated sum of distances divided by adjusted velocities.
The main function reads input values, invokes findK to determine the optimal ‘k’, and then prints the result.
Overall, the goal of the code seems to be to determine an optimal adjustment factor ‘k’ that minimizes the difference between the target value ‘t’ and the sum of distances divided by adjusted velocities.

double calculateK(int n, int t, int s[], int v[], double k) {
    double sum = 0;
    for (int i = 0; i < n; i++) {
        double realSpeed = v[i] + k;
        sum += (double)s[i] / realSpeed;
    }
    printf("%f:sum ", sum);
    return t - sum;
}

double findK(int n, int t, int s[], int v[]){
    double left = -1000.0, right = 1000.0; 
    double epsilon = 1e-6; // accuracy

    while (right - left > epsilon) {
        double mid = (left + right) / 2.0;
        double diff = calculateK(n, t, s, v, mid);
        printf(" %f:diff %f:mid %f:left %f:right n", diff, mid, left, right);
        if (diff < 0) {
            left = mid;
        } else {
            right = mid;
        }
    }
    
    return left;
}```


My problem seems to lie within the behavior of the binary search algorithm when trying to find the root of an equation. Initially, the variable 'right' decreases as expected, but then it starts increasing again. This unexpected behavior occurs despite efforts to ensure numerical stability and precision.


There are a few printf so 

4 10//my input
5 3
2 2
3 6
3 1

6.166667:sum 3.833333:diff 0.000000:mid -1000.000000:left 1000.000000:right
-0.026161:sum 10.026161:diff -500.000000:mid -1000.000000:left 0.000000:right
-0.017405:sum 10.017405:diff -750.000000:mid -1000.000000:left -500.000000:right
-0.014910:sum 10.014910:diff -875.000000:mid -1000.000000:left -750.000000:right
-0.013912:sum 10.013912:diff -937.500000:mid -1000.000000:left -875.000000:right
-0.013462:sum 10.013462:diff -968.750000:mid -1000.000000:left -937.500000:right
-0.013248:sum 10.013248:diff -984.375000:mid -1000.000000:left -968.750000:right
-0.013143:sum 10.013143:diff -992.187500:mid -1000.000000:left -984.375000:right
-0.013091:sum 10.013091:diff -996.093750:mid -1000.000000:left -992.187500:right
-0.013066:sum 10.013066:diff -998.046875:mid -1000.000000:left -996.093750:right
-0.013053:sum 10.013053:diff -999.023438:mid -1000.000000:left -998.046875:right
-0.013047:sum 10.013047:diff -999.511719:mid -1000.000000:left -999.023438:right
-0.013043:sum 10.013043:diff -999.755859:mid -1000.000000:left -999.511719:right
-0.013042:sum 10.013042:diff -999.877930:mid -1000.000000:left -999.755859:right
-0.013041:sum 10.013041:diff -999.938965:mid -1000.000000:left -999.877930:right
-0.013041:sum 10.013041:diff -999.969482:mid -1000.000000:left -999.938965:right
-0.013040:sum 10.013040:diff -999.984741:mid -1000.000000:left -999.969482:right
-0.013040:sum 10.013040:diff -999.992371:mid -1000.000000:left -999.984741:right
-0.013040:sum 10.013040:diff -999.996185:mid -1000.000000:left -999.992371:right
-0.013040:sum 10.013040:diff -999.998093:mid -1000.000000:left -999.996185:right
-0.013040:sum 10.013040:diff -999.999046:mid -1000.000000:left -999.998093:right
-0.013040:sum 10.013040:diff -999.999523:mid -1000.000000:left -999.999046:right
-0.013040:sum 10.013040:diff -999.999762:mid -1000.000000:left -999.999523:right
-0.013040:sum 10.013040:diff -999.999881:mid -1000.000000:left -999.999762:right
-0.013040:sum 10.013040:diff -999.999940:mid -1000.000000:left -999.999881:right
-0.013040:sum 10.013040:diff -999.999970:mid -1000.000000:left -999.999940:right
-0.013040:sum 10.013040:diff -999.999985:mid -1000.000000:left -999.999970:right
-0.013040:sum 10.013040:diff -999.999993:mid -1000.000000:left -999.999985:right
-0.013040:sum 10.013040:diff -999.999996:mid -1000.000000:left -999.999993:right
-0.013040:sum 10.013040:diff -999.999998:mid -1000.000000:left -999.999996:right
-0.013040:sum 10.013040:diff -999.999999:mid -1000.000000:left -999.999998:right

-1000.000000 //result

In this case expected result was -0.508653377
Tried to rewrite from the begining but got nothing. 
Еhis problem only occurs with negative k. 

New contributor

Archi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT