** TEE- Time C++ Four data files (CAR1.DAT, CAR2.DAT, CAR3.DAT, and CAR4.DAT) ar
ID: 3857429 • Letter: #
Question
** TEE- Time
C++
Four data files (CAR1.DAT, CAR2.DAT, CAR3.DAT, and CAR4.DAT) are available on the instructor's web page. Each file contains track results for a test vehicle. The results are in 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) As an example, the contents of CAR1.DAT are shown below: 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 14 125.4 1.6 156.6 1.8190.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.2749.2 4.4 804.7 4.6 861.2 4.8 918.4 5.0 976.2Explanation / Answer
# include<iostream>
# include<conio.h>
# include<stdio.h>
#include <fstream>
#include <stdlib.h>
using namespace std;
void VelocityInfs(double distance[], double time[], double velocity[], int size)// Compute velocity in f/s
{
for (int i = 1; i < size; i++)
{
velocity[i] = (distance[i] - distance[i - 1]) / (time[i] - time[i - 1]);
}
}
void VelocityInmph(double distance[], double time[], double velocity[], int size)// Compute velocity in mph
{
for (int i = 1; i < size; i++)
{
velocity[i] = (distance[i] - distance[i - 1]) / (time[i] - time[i - 1]);
velocity[i] *= 0.682;
}
}
void acceleration(double time[], double velocity[], double acceleration[], int size)// Compute acceleration
{
for (int i = 1; i < size; i++)
{
acceleration[i] = (velocity[i] - velocity[i - 1]) / (time[i] - time[i - 1]);
}
}
double ArrayMean(double array[], int size)
{
double sum=0.0;
for (int i = 0; i < size; i++)
{
sum += array[i];
}
return sum / size;
}
double ArrayMax(double array[], int size)
{
double max;
for (int i = 0; i < size; i++)
{
if (array[i] > max)
max = array[i];
}
return max;
}
int main()
{
string fileName;
cout << "Enter file name: ";
cin >> fileName;
std::ifstream f(fileName.c_str());
int lineCount;
string vehicleNumber;
string date;
string driverName;
double distance[30];
double time[30];
time[0] = 0.0;
distance[0] = 0.0;
string line;
int ind = 0;
cout << "filename " << fileName << " " << f.good() << """ ";
while(getline(f, line))
{
// cout << line << " ";
lineCount++;
if (lineCount == 1)// Read Vehicle number
{
vehicleNumber = line.substr(line.find(" "), line.length());
}
else if (lineCount == 2)// Read date;
{
date = line.substr(line.find(" "), line.length());//Store date name
}
else if (lineCount == 3)
{
driverName = line.substr(line.find(" "), line.length());// Store driver name
}
else
{
int pos = line.find(" ");
string t = line.substr(0, pos);
string d = line.substr(pos, line.length());
time[ind]= atof(t.c_str());// Store time in array
distance[ind++] = atof(d.c_str()); // Strore distance in array
}
}
cout << vehicleNumber << " " << date << " " << driverName << " ";
double velocityInfs[30];
double velocityInmph[30];
double accel[30];
VelocityInfs(distance, time, velocityInfs, ind);// Fill velocityInfs array
VelocityInmph(distance, time, velocityInmph, ind);//Fill velocityInmph array
acceleration(time, velocityInfs, accel, ind);//Fill acceleration array
cout << ArrayMean(velocityInfs, ind) << " " << ArrayMean(velocityInmph, ind) << " " << ArrayMean(accel, ind) << " ";
cout << ArrayMax(velocityInfs, ind) << " " << ArrayMax(velocityInmph, ind) << " " << ArrayMax(accel, ind) << " ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.