#include #include #include void pause (void) { cout << "Hit any key to continue...\n"; getch (); } bool read_in_table (int capacity, double temperatures[], double pressures[], double u_sub_f[], double u_sub_g[], int &table_size) { ifstream fin; char file_name[60]; double temperature_value; for (;;) { cout << "Enter name of input file: "; cin >> file_name; fin.open (file_name); if (!fin.fail()) { break; } cout << "Unable to open input file - please try again.\n"; } table_size = 0; // no values so far for (;;) { fin >> temperature_value; if (fin.fail()) { if (!fin.eof()) { cout << "\nThe input file contains a bad temperature value.\n"; return false; } if (table_size < 2) { cout << "The table contains fewer than two lines.\n"; return false; } return true; } if (table_size == capacity) { cout << "\nThe table is too large to be read..\n"; return false; } temperatures[table_size] = temperature_value; fin >> pressures[table_size] >> u_sub_f[table_size] >> u_sub_g[table_size]; if (fin.fail()) { cout << "\nThe input files contains bad data or a partial line.\n"; return false; } table_size++; } } void output_results (double T, double pressure, double u_sub_f, double u_sub_g) { cout << "\nAt " << T << " degrees C:\n" << " Pressure = " << pressure << " bar\n" << " u sub f = " << u_sub_f << " KJ/kg\n" << " u sub f = " << u_sub_g << " KJ/kg\n"; } void calculate_and_output (int table_size, double temperatures[], double pressures[], double u_sub_f[], double u_sub_g[], double T) { int low, high, mid; double k; if ((T < temperatures[0]) || (T > temperatures[table_size - 1])) { cout << "Temperature is not within the range covered by the table.\n"; return; } low = 0; high = table_size - 1; for (;;) { if (high == (low + 1)) { k = (T - temperatures[low]) / (temperatures[high] - temperatures[low]); output_results (T, pressures[low] + (k * (pressures[high] - pressures[low])), u_sub_f[low] + (k * (u_sub_f[high] - u_sub_f[low])), u_sub_g[low] + (k * (u_sub_g[high] - u_sub_g[low]))); return; } mid = (low + high) / 2; if (temperatures[mid] == T) { output_results (T, pressures[mid], u_sub_f[mid], u_sub_g[mid]); return; } if (temperatures[mid] > T) { high = mid; } else { low = mid; } } } void main (void) { const int capacity = 100; double temperatures[capacity], pressures[capacity], u_sub_f[capacity], u_sub_g[capacity], T; int table_size; if (!read_in_table(capacity, temperatures, pressures, u_sub_f, u_sub_g, table_size)) { cout << "\nUnable for read table - program execution terminated.\n"; pause (); return; } cout << "\nTable successfully read.\n" << "The table contains " << table_size << " entries.\n" << "Temperatures run from " << temperatures[0] << " degrees C to " << temperatures[table_size - 1] << " degrees C.\n"; for (;;) { cout << "\nEnter temperature (-999 to end): "; cin >> T; if (T == -999) { return; } calculate_and_output (table_size, temperatures, pressures, u_sub_f, u_sub_g, T); } }