Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. /*     ____   __     __
  2.         /   /   \  /   \
  3.        /   /      /
  4.       /    \___/  \___/
  5.  
  6. Date:11/14/17
  7.  
  8. Name:
  9.  
  10. Project Description: Project #3; This program is designed to read
  11. in information from a driver's scorecard to include his name, car
  12. number, the date, time, and distance traveled. Using the time and
  13. distance the drivers velocity and acceleration at the given times
  14. will be calculated.
  15.  
  16. Inputs: The driver's time and distances will be read into the
  17. program as inputs. Those values will then be sent through
  18. a function to calculate the outputs.
  19.  
  20. Outputs: The outputs include the drivers velocity and acceleration
  21. found based upon the inputs of time and distance.
  22. */
  23. //*************************************************************
  24.  
  25. #include <iostream>
  26. #include <string>
  27. #include <cstring>
  28. #include <fstream>
  29. #include <vector>
  30. #include <algorithm>
  31. #include <numeric>
  32. #include <iomanip>
  33.  
  34. using namespace std; //introduces namespace std
  35.  
  36. //Prototypes
  37. double to_mph (double);
  38. double v_calc (double, double, double, double);
  39. double a_ftss(double, double, double, double);
  40.  
  41. int main ( void )
  42. {
  43. //Declare variables
  44.     ifstream infile;
  45.     ofstream output;
  46.     string file_name, date,driver, car_num, data_out;
  47.     double x, y, v1_x, v2_x, accel_x;
  48.     vector <double> data, time, distance, v1_fts={0}, v2_mph={0}, accel={0};
  49.     int size=0;
  50.    
  51. //Prompt user to choose file needed to read data from input file
  52.     cout<<"What file would you like to use?: ";
  53.     cout<<"(Use the following format: filename.txt)" <<endl;
  54.     cin>>file_name;
  55.  
  56.     cout<<"What would you like the file for your results to be named?:"
  57.         <<"(Use the following format: filename.txt)" <<endl;
  58.     cin>>data_out;
  59.        
  60. //Opens input data file
  61.     infile.open(file_name);
  62.    
  63. //Reads in the first three lines of input file and sets them equal to
  64. //strings  
  65.     getline(infile, car_num);
  66.     getline(infile, date);
  67.     getline(infile, driver);
  68.                
  69.     while(!infile.eof())
  70.         {  
  71. /*Reads numeric values into variables x and y where x-values
  72. generate a vector for time and y-values generate a vector for
  73. distance while also calculating the size of the two vectors*/      
  74.                     infile>>x>>y;
  75.                     size++;
  76.                    
  77. //Adds x and y values to  the end of respective vectors as long
  78. //as input values are being given
  79.                     time.push_back(x);
  80.                     distance.push_back(y);         
  81.         }  
  82.            
  83.             for(int i=1; i<size; i++)
  84.                 {
  85. //Calls function v_calc to calulate the velocity in ft/s                   
  86. //and creates vector v1_fts using velocity values in ft/s
  87.             v1_x=v_calc(distance[i], distance[i-1], time[i], time[i-1]);   
  88.             v1_fts.push_back(v1_x);
  89.            
  90. //Calls function to_mph to calulate the velocity in mph
  91. //and creates vector v2_mph using velocity values in mph
  92.             v2_x=to_mph(v1_x); 
  93.             v2_mph.push_back(v2_x);
  94.                            
  95. //Calls function a_ftss to calculate acceleration
  96. //and creates vector accel using acceleration values               
  97.             accel_x=a_ftss(v1_fts[i], v1_fts[i-1], time[i], time[i-1] );
  98.             accel.push_back(accel_x);
  99.                 }      
  100.                
  101. //Creates and opens outputfile
  102.     output.open(data_out);
  103.  
  104. //Writes output info to the output file
  105.     output<<car_num<<endl <<date<<endl <<driver<<endl
  106.    
  107. //Calls vector function accumulate to find sum of all elements in
  108. //vector and divides by the number of elements to yield the average    
  109.         <<"Average Velocity: " <<fixed<<setprecision(1)
  110.         <<accumulate(v2_mph.begin(),v2_mph.end(),0)/v2_mph.size()
  111.         <<" " <<"mph" <<endl
  112.        
  113. //Calls vector function max_element to find the largest velocity   
  114.         <<"Max Velocity: "
  115.         <<*max_element(v2_mph.begin(),v2_mph.end())
  116.         <<" "<<"mph" <<endl
  117.  
  118.         <<"Average Acceleration: "
  119.         <<accumulate(accel.begin(),accel.end(),0)/accel.size()
  120.         <<" " <<"ft/s/2" <<endl
  121.  
  122.         <<"Max Acceleration: "
  123.         <<*max_element(accel.begin(),accel.end())
  124.         <<" " <<"ft/s/2" <<endl;
  125.  
  126. //Places the outputs for time, distance, velocity in ft/s and mph,
  127. //and acceleration in the output file 5 values per line                    
  128.     for(int i=0; i<size; i++)
  129.         {
  130.             output<<time[i] <<"," <<distance[i] <<"," <<v1_fts[i]
  131.                 <<"," <<v2_mph[i]<<","<<accel[i] <<endl;   
  132.         }
  133.  
  134. //Closes input file
  135.     infile.close();
  136.     output.close();
  137.     return 0;
  138. }
  139.  
  140. //Function calculates velocity based on the data from the input file
  141. double v_calc (double distance2, double distance1, double time2 , double time1)
  142.     {
  143.         double v=0;
  144.         v=(distance2-distance1)/(time2-time1);
  145.         return v;
  146.     }
  147.  
  148. //Function converts velocity from ft/s to mph  
  149. double to_mph (double value)
  150.     {  
  151.         double mph=0;
  152.        
  153.         mph=value*3600/5280;
  154.        
  155.         return mph;
  156.     }
  157.  
  158. //Function calculates acceleration
  159. double a_ftss(double v1_fts2, double v1_fts1, double time2 , double time1) 
  160.     {
  161.         double a=0;
  162.         a=(v1_fts2-v1_fts1)/(time2-time1);
  163.         return a;
  164.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement