Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ____ __ __
- / / \ / \
- / / /
- / \___/ \___/
- Date:11/14/17
- Name:
- Project Description: Project #3; This program is designed to read
- in information from a driver's scorecard to include his name, car
- number, the date, time, and distance traveled. Using the time and
- distance the drivers velocity and acceleration at the given times
- will be calculated.
- Inputs: The driver's time and distances will be read into the
- program as inputs. Those values will then be sent through
- a function to calculate the outputs.
- Outputs: The outputs include the drivers velocity and acceleration
- found based upon the inputs of time and distance.
- */
- //*************************************************************
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- #include <iomanip>
- using namespace std; //introduces namespace std
- //Prototypes
- double to_mph (double);
- double v_calc (double, double, double, double);
- double a_ftss(double, double, double, double);
- int main ( void )
- {
- //Declare variables
- ifstream infile;
- ofstream output;
- string file_name, date,driver, car_num, data_out;
- double x, y, v1_x, v2_x, accel_x;
- vector <double> data, time, distance, v1_fts={0}, v2_mph={0}, accel={0};
- int size=0;
- //Prompt user to choose file needed to read data from input file
- cout<<"What file would you like to use?: ";
- cout<<"(Use the following format: filename.txt)" <<endl;
- cin>>file_name;
- cout<<"What would you like the file for your results to be named?:"
- <<"(Use the following format: filename.txt)" <<endl;
- cin>>data_out;
- //Opens input data file
- infile.open(file_name);
- //Reads in the first three lines of input file and sets them equal to
- //strings
- getline(infile, car_num);
- getline(infile, date);
- getline(infile, driver);
- while(!infile.eof())
- {
- /*Reads numeric values into variables x and y where x-values
- generate a vector for time and y-values generate a vector for
- distance while also calculating the size of the two vectors*/
- infile>>x>>y;
- size++;
- //Adds x and y values to the end of respective vectors as long
- //as input values are being given
- time.push_back(x);
- distance.push_back(y);
- }
- for(int i=1; i<size; i++)
- {
- //Calls function v_calc to calulate the velocity in ft/s
- //and creates vector v1_fts using velocity values in ft/s
- v1_x=v_calc(distance[i], distance[i-1], time[i], time[i-1]);
- v1_fts.push_back(v1_x);
- //Calls function to_mph to calulate the velocity in mph
- //and creates vector v2_mph using velocity values in mph
- v2_x=to_mph(v1_x);
- v2_mph.push_back(v2_x);
- //Calls function a_ftss to calculate acceleration
- //and creates vector accel using acceleration values
- accel_x=a_ftss(v1_fts[i], v1_fts[i-1], time[i], time[i-1] );
- accel.push_back(accel_x);
- }
- //Creates and opens outputfile
- output.open(data_out);
- //Writes output info to the output file
- output<<car_num<<endl <<date<<endl <<driver<<endl
- //Calls vector function accumulate to find sum of all elements in
- //vector and divides by the number of elements to yield the average
- <<"Average Velocity: " <<fixed<<setprecision(1)
- <<accumulate(v2_mph.begin(),v2_mph.end(),0)/v2_mph.size()
- <<" " <<"mph" <<endl
- //Calls vector function max_element to find the largest velocity
- <<"Max Velocity: "
- <<*max_element(v2_mph.begin(),v2_mph.end())
- <<" "<<"mph" <<endl
- <<"Average Acceleration: "
- <<accumulate(accel.begin(),accel.end(),0)/accel.size()
- <<" " <<"ft/s/2" <<endl
- <<"Max Acceleration: "
- <<*max_element(accel.begin(),accel.end())
- <<" " <<"ft/s/2" <<endl;
- //Places the outputs for time, distance, velocity in ft/s and mph,
- //and acceleration in the output file 5 values per line
- for(int i=0; i<size; i++)
- {
- output<<time[i] <<"," <<distance[i] <<"," <<v1_fts[i]
- <<"," <<v2_mph[i]<<","<<accel[i] <<endl;
- }
- //Closes input file
- infile.close();
- output.close();
- return 0;
- }
- //Function calculates velocity based on the data from the input file
- double v_calc (double distance2, double distance1, double time2 , double time1)
- {
- double v=0;
- v=(distance2-distance1)/(time2-time1);
- return v;
- }
- //Function converts velocity from ft/s to mph
- double to_mph (double value)
- {
- double mph=0;
- mph=value*3600/5280;
- return mph;
- }
- //Function calculates acceleration
- double a_ftss(double v1_fts2, double v1_fts1, double time2 , double time1)
- {
- double a=0;
- a=(v1_fts2-v1_fts1)/(time2-time1);
- return a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement