-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearInterpolation.cpp
45 lines (37 loc) · 1.21 KB
/
LinearInterpolation.cpp
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>
using namespace std;
static double linear_interpolation() {
cout << "Linear Interpolation Calculator: \n";
// Input values
double max_given_y, min_given_y, target_y, max_given_x, min_given_x;
cout << "Enter value for max_given_y: ";
cin >> max_given_y;
cout << "Enter value for min_given_y: ";
cin >> min_given_y;
cout << "Enter value for target_y: ";
cin >> target_y;
cout << "Enter value for max_given_x: ";
cin >> max_given_x;
cout << "Enter value for min_given_x: ";
cin >> min_given_x;
// Left side of equation
double left_side = (max_given_y - min_given_y) / (max_given_y - target_y);
// Solve for target_x value on right side of equation
double target_x = max_given_x - ((max_given_x - min_given_x) / left_side);
return target_x;
}
int main() {
while (true) {
double target_x = linear_interpolation();
cout << "The interpolated value target_x is: " << target_x << endl;
// Option to restart or exit program loop
string repeat;
cout << "Restart? (y/n) ";
cin >> repeat;
cout << "\n";
if (repeat != "y") {
break;
}
}
return 0;
}