LINEAR INTERPOLATION OF FLIGHT RECORDER DATA Altitude data from a rocket flight
ID: 3630384 • Letter: L
Question
LINEAR INTERPOLATION OF FLIGHT RECORDER DATA
Altitude data from a rocket flight recorder is stored in a data file named flight.txt. The data in this file are arranged in two columns. The first column values are times listed in seconds, and the second column values are the heights in meters that correspond to these times. There is no sentinel value to indicate the end of data, nor is there a pre-number in the data file to indicate the number of records that follow. The time values are always in ascending order.
An example of a data file is shown below:
0.0 0.0
1.0 7.9
2.0 15.7
3.0 23.3
5.0 38.7
10.0 72.3
20.0 128.9
30.0 168.3
40.0 192.5
50.0 200.1
60.0 192.8
70.0 168.3
80.0 128.4
90.0 72.7
Write a program that will work with a data file that is structured like the file described above. You will not know how many data values are in the file ahead of time. You must use the EOF at the end of the file to stop reading data.
Your program should do the following:
1. Ask the user for an input time.
2. Use this input time and the data file flight.txt to determine the corresponding height of the rocket.
3. Print out the input time and the corresponding rocket height. If the input time is outside the time range covered by the data file, then your program should print a message to that effect and not try to determine the corresponding height.
If the input time matches a time in the data file, then the corresponding height will also be in the data file (it will be the very next data value). If the input time is between two adjacent times in the data file, then your program will need to linearly interpolate the data to determine the height corresponding to the input time.
(over)
Note on linear interpolation:
If an input time t is between t1 and t2 in the data file, then the height h corresponding to it can be determined from the equation:
h=h1+(t-t1/t2-t1)*(h2-h1)
Where h1 and h2 are the heights that correspond to t1and t2 respectively.
Explanation / Answer
#include <iostream> #include <string> #include <fstream> #include <iostream> #include <string> #include <fstream> using namespace std; //Define constants and declare function prototypes const int T=100; double maxval(const double x[], int n); double minval(const double x[], int n); int main() { // Declare objects int tpts(0); double y[T], flight_range, coef_lift; string filename; ifstream tunnel; // Prompt user for file name and open date file. cout << "Enter the name of the data file"; cin >> filename; tunnel.open(filename.c_str()); if (tunnel.fail()) { cout << "Error opeing input file "; } else { // Read a data value from the file tunnel >> flight_range >> coef_lift; // While there is room in the array and // end of file was not encountered, // assign the value to the array and // input the next value. while (tpts <= T-1 && !tunnel.eof()) { y[tpts] = flight_range; tpts++; tunnel >> flight_range; } // Find and print the maximum value cout << "Range of flight-path angles: " << minval(y,tpts) << " to " << maxval(y,tpts) << " degrees"<< endl; // Close file and exit program tunnel.close(); } return 0; } // Close file and exit program tunnel.close(); } return 0; } /* ------------------------------------------------------*/ /* This function returns the maximum */ /* value in the array x with n elements */ double maxval(const double x[], int n) { // Declare local objects double max_x; // Determine maximum value in the array max_x = x[0]; for (int k=1; k<=n-1; k++) { if (x[k] > max_x) max_x = x[k]; } // Return maximum value return max_x; } /* ------------------------------------------------------*/ /* This function returns the minimum */ /* value in an array x with n elements */ double minval(const double x[], int n) { // Declare objects double min_x; // Determine minimum value in the array min_x = x[0]; for (int k=1; k<=n-1; k++) { if (x[k] < min_x) min_x = x[k]; } // Return minimum value return min_x; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.