// Programmer: Bill McQuain // Last modified: March 17, 2003 // Compiler: Visual C++ .NET // // This program applies the velocity formula for a falling body to // create a table of data for a skydiver falling for a given number // of seconds. The formula takes into account air resistance and // the coefficient of drag of the falling body. // #include // for file streams #include // for output format manipulators #include // for string variables #include // for sqrt() and tanh() functions using namespace std; // put all of the above in scope const double g = 32.2; // gravitational accelereration const int COLUMNWIDTH = 8; // table column width setting void readDiverData(string& Name, double& Weight, double& Drag, int& Seconds); void writeProjectHeader(ofstream& Out); void writeDataSummary(ofstream& Out, const string& Name, double Mass, double Drag); void writeTableHeader(ofstream& Out, int totalSeconds); void writeVelocityData(ofstream& Out, int totalSeconds, double Mass, double Drag); double calcMass(double Weight); double Velocity(double Mass, double Drag, int Time); int main() { string DiverName; // name of skydiver double Weight, // weight of skydiver in pounds Mass, // mass of skydiver DragCoeff; // coeff of drag for skydiver int TimeInterval; // number of seconds to report readDiverData(DiverName, Weight, DragCoeff, TimeInterval); ofstream Log("FreefallLog.txt"); // Open the output file stream. Log << fixed << showpoint; // Prepare it for decimal output. // Write an identification header to the output file: writeProjectHeader(Log); Mass = calcMass(Weight); // Calculate the mass of the skydiver. // Write the header data to the output file: writeDataSummary(Log, DiverName, Mass, DragCoeff); // Write the table header: writeTableHeader(Log, TimeInterval); // Write the sky diver velocity data: writeVelocityData(Log, TimeInterval, Mass, DragCoeff); Log.close(); // Close the log file. return 0; } //////////////////////////////////////////////////////////////// readDiverData // readDiverData reads the name, weight, and coefficient of drag from the // input file, along with the time interval for the report. // // Parameters: // Name upon reutrn, the name of the skydiver // Weight upon reutrn, the weight (in lbs) of the skydiver // Drag upon reutrn, the coefficient of drag of the skydiver // Seconds upon reutrn, the number of seconds for which the report must // contain results // // Pre: The input data file, named FreefallData.txt must exist and be // in the correct directory, and be formatted as described in the // specification of the project. // // Post: The specified data values will have been read, and stored in // the corresponding parameters. // // Returns: The function does not return a value. // // Called by: main() // Calls: getline(), ifstream::close() // void readDiverData(string& Name, double& Weight, double& Drag, int& Seconds) { ifstream Data("FreefallData.txt"); // Open the input file stream. getline(Data, Name); // Read the name of sky diver Data >> Weight // and weight >> Drag // and drag coefficient >> Seconds; // and time interval Data.close(); // Close the input stream. } //////////////////////////////////////////////////////////////// writeProjectHeader // writeProjectHeader writes the programmer name and assignment description // to the log file. // // Parameters: // Out output file stream to which header should be written // // Pre: Out must be opened on the output file. // // Post: The specified data has been written to the output file. // // Returns: The function does not return a value. // // Called by: main() // Calls: none // void writeProjectHeader(ofstream& Out) { Out << "Programmer: Bill McQuain" << endl; Out << "CS 1044: Falling Bodies" << endl; Out << endl; } //////////////////////////////////////////////////////////////// writeDataSummary // writeDataSummary echoes the skydiver name, mass, and coefficient of drag // to the log file, with descriptive labels. // // Parameters: // Out output stream to which data should be written // Name name of skydiver // Mass mass of skydiver // Drag coefficient of drag of skydiver // // Pre: Out has been opened on the output file. // The remaining parameters have been assigned values. // // Post: The specified data has been written to the output stream. // // Returns: The function does not return a value. // // Called by: main() // Calls: setw(), setprecision() // void writeDataSummary(ofstream& Out, const string& Name, double Mass, double Drag) { const int HEADERWIDTH = 25; Out << "Skydiver: " << setw(HEADERWIDTH) << Name << endl; Out << "Mass: " << setw(HEADERWIDTH) << setprecision(2) << Mass << endl; Out << "Drag constant:" << setw(HEADERWIDTH) << setprecision(2) << Drag << endl; Out << endl; } //////////////////////////////////////////////////////////////// writeTableHeader // writeTableHeader writes the data labels, column headers, and separator for // the table to the output stream. // // Parameters: // Out output stream to which data will be written // totalSeconds number of seconds for which velocity is to be reported // // Pre: The output stream has been opened on the output file. // totalSeconds has been assigned the correct value. // The constant COLUMNWIDTH must be declared at global scope. // // Post: The specified labels and separator have been written. // // Returns: The function does not return a value. // // Called by: main() // Calls: setw(), setfill() // void writeTableHeader(ofstream& Out, int totalSeconds) { Out << "Seconds: "; int Second = 1; while ( Second <= totalSeconds ) { Out << setw(COLUMNWIDTH) << Second; Second = Second + 1; } Out << endl; int SeparatorWidth = 9 + totalSeconds * COLUMNWIDTH; // Write the separator: Out << setw(SeparatorWidth) << setfill('-') << "-" << setfill(' ') << endl; // Write the table data label: Out << "Velocity:"; } //////////////////////////////////////////////////////////////// writeVelocityData // writeVelocityData writes the velocity data, properly formatted, for each // specified time value. // // Parameters: // Out output stream to which velocity data is to be written // totalSeconds number of time values to include in table // Mass mass of skydiver // Drag coefficient of drag of skydiver // // Pre: Out has been opened on the output file. // The remaining parameters have been assigned correct values. // // Post: Velocity values for times 1 to totalSeconds have been written // to the output stream. // The constant COLUMNWIDTH must be declared at global scope. // // Returns: The function does not return a value. // // Called by: main() // Calls: Velocity(), setw(), setprecision() // void writeVelocityData(ofstream& Out, int totalSeconds, double Mass, double Drag) { double fpsV = 0.0; int Second = 1; while ( Second <= totalSeconds ) { // Calculate the velocity in feet per second: fpsV = Velocity(Mass, Drag, Second); // Write the velocity at the current current time: Out << setw(COLUMNWIDTH) << setprecision(1) << fpsV; // Step to the next time unit: Second++; } Out << endl; } //////////////////////////////////////////////////////////////// Velocity // Velocity computes the velocity of a free-falling body, given its mass and // coefficient of drag, and the number of seconds it has been falling. // // Parameters: // Mass mass of the body // Drag coefficient of drag of the body // Time number of seconds the body has been falling // // Pre: The parameters have been assigned correct values. // The gravitational accleration constant g must be declared // at global scope. // // Post: The velocity has been computed and returned. // // Returns: The velocity of the falling body. // // Called by: writeVelocityData() // Calls: sqrt(), tanh() // double Velocity(double Mass, double Drag, int Time) { return sqrt(Mass * g / Drag) * tanh(sqrt(g * Drag / Mass) * Time); } //////////////////////////////////////////////////////////////// calcMass // calcMass calculates the mass of a body that weighs Weight lbs. // // Parameters: // Weight the weight of the body, in lbs // // Pre: Weight has been assigned the correct value. // The gravitational accleration constant g must be declared // at global scope. // // Post: The corresponding mass has been computed and returned. // // Returns: The mass of the body. // // Called by: Velocity() // Calls: none // double calcMass(double Weight) { return ( Weight * g ); }