CSCI 131 Techniques of Programming--Spring 2014

    Home | | Syllabus | | Assignments | | Lectures | | Resources

    Solution to Project 1

    //**************************************************************************
    // proj1.cc
    //    Author: Brenda Student
    //    Date: February 10, 2014
    //    Class: CSCI 131 project1, Professor Royden
    //    Purpose: Computes the final position and velocity of a falling object
    //             given the initial position and velocity
    //    Input: (from standard input) Inputs the initial position, initial
    //             velocity and elapsed time.
    //    Output: (to myproj1.out file) Outputs the intial position, initial
    //              velocity, elapsed time, final position and final velocity.
    //***************************************************************************
    
    #include <iostream>                    //required io libraries
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    int main(void) {
       const float GRAVITY = 32.0;            //gravitational acceleration
       float xInitial;                        //Initial position
       float vInitial;                        //Initial velocity
       float time;                            //elapsed time
       float xFinal;                          //Final position
       float vFinal;                          //Final velocity
    
       ofstream outFile;                      //output filestream
    
       //Enter input data
       cout << "Enter the initial position ==> ";
       cin >> xInitial;
       cout << xInitial << endl;
    
       cout << "Enter the initial velocity ==> ";
       cin >> vInitial;
       cout << vInitial << endl;
    
       cout << "Enter the time  ==> ";
       cin >> time;
       cout << time << endl;
    
       //Compute final position and velocity
       xFinal = xInitial - vInitial*time - 0.5*GRAVITY*time*time;
       vFinal = vInitial + GRAVITY * time; 
    
       //Send the results to a file
       outFile.open("myproj1.out");
    
       outFile.setf(ios::fixed, ios::floatfield);
       outFile.setf(ios::showpoint);
       outFile << endl << endl;
       outFile << "---------------------------------------------------" 
    	   << endl << endl;
    
       outFile << setw(25) << "The initial position = "
    	   << setw(10) << setprecision(2) << xInitial << " feet." << endl;
       outFile << setw(25) << "The initial velocity = " 
    	   << setw(10) << setprecision(2) << vInitial << " feet/sec." << endl;
    
       outFile << setw(25) << "The elapsed time = " 
    	   << setw(10) << setprecision(2) << time << " seconds." << endl;
       outFile << setw(25) << "The final position = "
    	   << setw(10) << setprecision(2) << xFinal << " feet." << endl;
       outFile << setw(25) << "The final velocity = "
    	   << setw(10) << setprecision(2) << vFinal << " feet/sec." << endl;
       outFile << endl;
       outFile << "---------------------------------------------------" 
    	   << endl << endl;
    
       outFile.close( );
    
       return 0;
    } //end main
    
    
    
    

    Home | | Syllabus | | Assignments | | Lectures | | Resources


    Constance Royden--croyden@mathcs.holycross.edu
    CSCI 131: Techniques of Programming
    Last Modified: February 10, 2014
    Page Expires: September 13, 2014