Here is what I ended up with. It does all of the correct things that are supposed to happen with the code that the teacher wanted it to output. It does in between values but if I put in say -4 or -2 or 0 or someother angle that is in the data file it tries to compute the in between value.
I'm not sure about how to do a flight-path angle that is equal to one of the numbers in the data file. I've tried doing:
Code:
if (b = range[j]}
{
a = range[j];
f_a = coef[j];
}
But it will cout "Lift coefficient for -2 degrees -0.056" for any number entered. Any suggestions how to to get it to cout "Lift coefficient for (any degree entered that is in data file) degrees (coefficient in data file).
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
// Declare variables
double range[17], a, b, c;
double coef[17], f_a, f_b, f_c;
char repeat, N, n;
ifstream tunnel;
int j;
// open data file "tunnel.txt"
tunnel.open("tunnel.txt");
if (tunnel.fail()) // Check to see if file opened
{
cout << "Error opening data file 'tunnel.txt'" << endl;
}
else // If opened successfully, continue with code
{
for (int i = 0; i <= 16; i++)
{
tunnel >> range[i] >> coef[i];
}
cout << "Range of flight-path angles:" << range[0] << " to " << range[16] << " degrees" << endl;
cout << endl;
do
{
cout << "Please enter a flight-path angle in degrees: ";
cin >> b; // Get flight path angle from user
if (b < range[0] || b > range[16]) // Test to see if angle is outside flight-range
{
cout << "**** Sorry, " << b << " is not within data range ****" << endl;
cout << endl;
cout << "Would you like to enter another value (Y/N)?";
cin >> repeat;
cout << endl;
} // If angle is inside flight-range
else
{
for (j = 0; j < 17; j++) // Find values of a and f_a
{
tunnel >> range[j] >> coef[j]; // Read data into two dimensional array
if(b > range[j])
{
a = range[j];
f_a = coef[j];
}
}
for (j = 17; j > 0; j--) // Find values of c and f_c
{
tunnel >> range[j] >> coef[j]; // Read data into two dimensional array
if(b < range[j])
{
c = range[j];
f_c = coef[j];
}
}
f_b = f_a + (((b - a) / (c - a)) * (f_c - f_a)); // Computer lift coef with use of linear interpolation
cout << endl;
cout << "Lift coeffiecient for " << b << " degrees: " << f_b << endl;
cout << "Would you like to enter another value (Y/N)?";
cin >> repeat;
cout << endl;
}
}
while (!(repeat == 'N' || repeat == 'n')); // Conditon for do loop
}
tunnel.close(); // Closes "tunnel.txt"
return 0; // Program ended successfully
}