/** * Assignment #2, Program 2 * Program to calculate cyclist's rate of acceleration * Author: Mark Allen Weiss * SSN: 000-00-0000 * Course: COP-2210 Section 8 * Date: September 28, 1998 * * CERTIFICATION OF SOLE AUTHORSHIP * I certify that this work is solely my own and * that none if it is the work of any other person. * I further certify that I have not provided any * assistance to other students enrolled in COP-2210 that * would render their CERTIFICATION OF SOLE AUTHORSHIP * invalid. I understand that violations of this pledge * may result in severe sanctions. * * * * ----------------------------------- * (Mark Allen Weiss) <--- Put your name here, and sign above */ #include #include "apstring.h" // Function declarations void display_instructions( ); double get_input( apstring message ); double compute_rate_of_deceleration( double final_velocity, double initial_velocity, double time_interval ); double time_to_rest( double initial_velocity, double rate_of_deceleration ); void print_answers( double rate_of_deceleration, double stopping_time ); int main( ) { // Inputs double initial_velocity; double final_velocity; double time_interval; // Outputs double rate_of_deceleration; double stopping_time; // Read values display_instructions( ); initial_velocity = get_input( "initial velocity" ); final_velocity = get_input( "final velocity" ); time_interval = get_input( "time interval" ); // Compute answers rate_of_deceleration = compute_rate_of_deceleration( final_velocity, initial_velocity, time_interval ); stopping_time = time_to_rest( initial_velocity, rate_of_deceleration ); // Print the answers print_answers( rate_of_deceleration, stopping_time ); return 0; } /** * Print instructions. */ void display_instructions( ) { cout << "This program will compute the total time it takes for" << endl; cout << "a cyclist to stop, given three parameters:" << endl; cout << "\t1. An initial velocity" << endl; cout << "\t2. A slower velocity" << endl; cout << "\t3. The time between the two measurement points above." << endl; cout << "Stopping time is from the initial "; cout << "velocity until velocity of 0." << endl; cout << "The program will also compute the rate of deceleration." << endl << endl; } /** * Print answers. */ void print_answers( double rate_of_deceleration, double stopping_time ) { cout << endl; cout << "The rate of deceleration is " << rate_of_deceleration << endl; cout << "The time to stop, measured from the point of initial velocity is " << stopping_time << endl; } /** * Read an input; message is used to give a prompt. */ double get_input( apstring message ) { double value_read; cout << "Please enter " << message << ": "; cin >> value_read; return value_read; } /** * Compute the rate of deceleration. This is a positive * number if we are decelerating. */ double compute_rate_of_deceleration( double final_velocity, double initial_velocity, double time_interval ) { return ( initial_velocity - final_velocity ) / time_interval; } /** * Compute the time to rest. * Since a = (vf - vi) / t, it follows that * t = (vf - vi) / a. By definition, vf = 0, so * t = -vi / a, and since deceleration is given by -a, * t = vi / d. */ double time_to_rest( double initial_velocity, double rate_of_deceleration ) { return initial_velocity / rate_of_deceleration; }