in c++ Write a function that calculates how much kinetic energy an object has. T
ID: 3935127 • Letter: I
Question
in c++ Write a function that calculates how much kinetic energy an object has. The formula for kinetic energy is:
KE = (1/2) * m * v2
The function must conform to the following:
Name:
find_ke_value
Parameters:
double m - mass
double v - velocity
Return type:
double - The kinetic energy
Put this code in a file called physics.cpp. Do not include a main function.
This is my code I have and it outputs 0
#include <iostream>
#include <cmath>
using namespace std;
double find_ke_value(double m, double v)
{
double ke;
ke=(1/2)*m*(pow(v,2));
return ke;
}
Explanation / Answer
The only problem with your code is, you are doing a division 1/2. This is an integer division, and will result in 0. And obviously multiplying with 0 will result in the whole expression leading to 0. So, the solution for your problem is given herewith:
#include <iostream>
#include <cmath>
using namespace std;
double find_ke_value(double m, double v)
{
double ke;
ke=(1/2.0)*m*(pow(v,2));
return ke;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.