C++ Problem Four data files (CAR1.DAT, CAR2.DAT, CAR3.DAT and CAR4.DAT ) are ava
ID: 3738704 • Letter: C
Question
C++ Problem
Four data files (CAR1.DAT, CAR2.DAT, CAR3.DAT and CAR4.DAT ) are available below. Each file contains track results with the following format:
The test vehicle number (line 1).
The test date (line 2).
The test driver (line 3).
Time, t, in seconds and distance, x, in feet (one pair of (t,x) values per line for all other lines)
Velocity can be defined as v = dx/dt and acceleration can be defined as a = dv/dt. Theses equations can be approximated by v = change in distance/change in time and a= change in velocity/change in time . For the data provided above, assume v = 0 and a = 0 at the first time value 0.0. Using the above approximations v1 = (5.5 – 0.0)/(0.2 – 0.0) = 5.5 ft/0.2 s = 27.5 ft/s and a1 =( 27.5 – 0.)0 / ( 0.2 – 0.0) = (27.5 ft/s)/0.2 s = 137.5 ft / s2 . The remaining values can be determined in a similar manner.
Program Requirements:
Write a C++ program that will accomplish the following:
The program should ask the user which data file is to be used. Read the filename as a string and open the file using a string variable. Do not use an array.
Calculate the velocity and acceleration. The output of the result should be sent to a data file. The name is specified by the user. The output includes
test vehicle number
test date
test driver
values for time, distance, velocity (ft/s) and acceleration (ft/s2) for each of the data points in the input data files ( 4 numbers per line)
send the values to a commas delimited file
open the file in Excel, graph time vs. acceleration and time vs. velocity
Test vehicle: Car #1
Test date: October 26, 2007
Driver: D. Myers
0.0 0.0
0.2 5.5
0.4 15.9
0.6 30.9
0.8 49.7
1.0 71.8
1.2 97.1
1.4 125.4
1.6 156.6
1.8 190.4
2.0 226.6
2.2 265.3
2.4 306.1
2.6 348.9
2.8 393.5
3.0 439.9
3.2 488.0
3.4 537.7
3.6 588.8
3.8 641.2
4.0 694.6
4.2 749.2
4.4 804.7
4.6 861.2
4.8 918.4
5.0 976.2
Test vehicle: Car #2
Test date: October 27, 2007
Driver: M. Snelling
0.0 0.0
0.2 6.0
0.4 17.6
0.6 34.3
0.8 55.9
1.0 81.9
1.2 111.9
1.4 145.6
1.6 182.4
1.8 222.4
2.0 265.6
2.2 311.4
2.4 359.6
2.6 409.5
2.8 460.9
3.0 513.4
3.2 566.5
3.4 619.8
3.6 672.9
3.8 725.9
4.0 778.5
4.2 831.1
4.4 883.5
4.6 935.8
4.8 988.0
5.0 1040.2
Test vehicle: Car #3
Test date: October 30, 2007
Driver: R. Maynard
0.00 0.0
0.25 0.9
0.50 3.7
0.75 9.1
1.00 18.1
1.25 31.5
1.50 50.2
1.75 74.9
2.00 106.5
2.25 145.2
2.50 191.5
2.75 246.0
3.00 307.4
3.25 374.4
3.50 445.5
3.75 518.5
4.00 592.2
4.25 665.8
4.50 739.3
4.75 812.5
5.00 885.7
Test vehicle: Car #4
Test date: November 11, 2007
Driver: D. Marsh
0.00 0.0
0.25 10.7
0.50 31.0
0.75 59.6
1.00 94.9
1.25 136.5
1.50 183.6
1.75 235.5
2.00 291.5
2.25 350.9
2.50 413.1
2.75 477.4
3.00 543.2
3.25 610.2
3.50 678.0
3.75 746.4
4.00 815.3
4.25 884.0
4.50 952.3
4.75 1020.4
5.00 1088.8
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include<algorithm>
using namespace std;
int main ()
{
string file_name,file_name_write;
string token1,token2;
string delimiter=" ";
//string delimiter_newline=" ";
double prev_distance=0.0;
double prev_time=0.0;
double prev_velocity=0.0;
cout<<"Enter the file you want to read ";
getline( cin, file_name );
cout<<"Enter the file you want to write to ";
getline( cin, file_name_write);
string line;
ifstream myfile;
size_t pos = 0;
double current_time,current_distance,current_velocity,current_accl;
myfile.open (file_name.c_str());
ofstream myfile_w;
myfile_w.open (file_name_write.c_str());
int count=1;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
if(count<=6)
{
count++;
continue;
}
cout << line << ' ';
if (!line.empty())
{
cout<<"space index:"<<line.find(" ")<<" ";
token1=line.substr(0,line.find(" "));
token2=line.substr(line.find(" ")+5);
current_time = std::atof(token1.c_str());
//cout<<"temp= "<<temp<<' ';
current_distance = std::atof(token2.c_str());
//cout<<"temp= "<<temp<<' ';
//token2.erase(remove_if(token2.begin(), token2.end(), isspace), token2.end());
//std::string::iterator end_pos = std::remove(token2.begin(), token2.end(), ' ');
//token2.erase(end_pos, token2.end());
cout<<"token1= "<<token1<<' ';
cout<<"token2= "<<token2<<' ';
current_velocity=(current_distance-prev_distance)/(current_time-prev_time);
current_accl=(current_velocity-prev_velocity)/(current_time-prev_time);
cout<<"vel: "<<current_velocity;
cout<<"accl: "<<current_accl;
myfile_w<<current_time<<","<<current_distance<<","<<current_velocity<<","<<current_accl<<" ";
//cout<<line.find(" ")<<' ';
//cout<<token.length();
}
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.