The kinetic energy of a car in motion is given by k = (1/2) mv2 Where k = kineti
ID: 3576371 • Letter: T
Question
The kinetic energy of a car in motion is given by k = (1/2) mv2 Where k = kinetic energy m = mass of the car v = velocity of the car Write C++ program that calls a function named KE (double m, double v) to calculate the kinetic energy of car1 and car2 that moving with velocities v1 and v2 respectively and find out which car has more kinetic energy. Input specifications: read m1, m2, v1 and v2 from file named "speed.dat" Output specifications: print a message on file "named "message.dat" that tells user which car has more kinetic energy, for example, "car1 has more kinetic energy than car2". If they have, equal kinetic energies then print, "Both cars have the same kinetic energy".Explanation / Answer
speed.dat (save this file under D Drive .Then the path of the file pointing to it is D:\speed.dat)
30 3.87
45 2.91
____________________________
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
//Declaring variables
double velocity,mass;
double ke[2];
int i;
//defines an input stream for the data file
ifstream dataIn;
//Defines an output stream for the data file
ofstream dataOut;
//Opening the input file
dataIn.open("D:\speed.dat");
/* if the input file is not found in the
* specified path then display error message
*/
if(dataIn.fail())
{
//Dispalying the error message
cout<<"** File Not Found **"<<endl;
}
else
{
i=0;
//Getting the data from the file
while(dataIn>>mass>>velocity)
{
/* Calculating the kinetic energy
* and populate the result in the array
*/
ke[i]=0.5*mass*velocity;
i++;
}
//creating and Opening the output file
dataOut.open("D:\message.dat");
//Comparing the kinetic energies
if(ke[0]>ke[1])
dataOut<<"Car1 has more Kinetic Energy than Car2"<<endl;
else if(ke[0]<ke[1])
dataOut<<"Car1 has less Kinetic Energy than Car2"<<endl;
else
dataOut<<"Both cars have same kinetic energy"<<endl;
}
//Closing the intput file
dataIn.close();
//Closing the output file.
dataOut.close();
return 0;
}
_______________________
message.dat (We can find this file under D Drive.As we specified the path of the output file as D:\message.dat)
Car1 has less Kinetic Energy than Car2
________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.