Tuesday, January 3, 2017

Programming Challenge 6.6 - Kinetic Energy

/* Kinetic Energy - In physics, an object that is in motion is said to
   have kinetic energy. The following formula can be used to determine
   a moving object's kinetic energy:
 
   * KE = 1/2 mv²

   The variables in the formula are as follows:

   * KE: is the kinetic energy
   * m:  is the object's mass in kilograms
   * v:  is the object's velocity in meters per second

   This program uses a function named kineticEnergy().

   The function is demonstrated in this program which asks the user
   to enter values for mass and velocity. */

#include "Utility.h"

/* Prototype: Kinetic energy */
double kineticEnergy(double, double);

int main()
{
    /* Variables: Object mass, Object velocity */
    double objMass = 0.0,
           objVelocity = 0.0,
           objKineticEnergy = 0.0;

    /* Introduction */
    cout << "\t\tKinetic Energie Function Demo\n\n"
         << "All moving objects have kinetic energy. The kinetic\n"
         << "energy an object has depends on its:\n\n"
         << "* Mass\n"
         << "* Speed\n\n"
         << "This program uses a function that let's you determine\n"
         << "the kinetic energy (in Joules) of a moving object, by\n"
         << "using a custom function that does the calculation.\n\n";

    /* Get: Object mass, Object velocity */
    cout << "Enter the object's mass (kg): ";
    cin >> objMass;

    /* Validation: While 0 or negative is entered for the object's
       mass, a message is displayed, and the user is asked to repeat
       entering the data */
    while (objMass <= 0)
    {
        cout << "\nAn object's mass can not be 0 or negative.\n"
             << "Enter the object's mass (kg): ";
        cin >> objMass;
    }

    cout << "Enter the object's velocity (m/s): ";
    cin >> objVelocity;

    /* Validation: While 0 or negative is entered for the object's
    velocity, a message is displayed, and the user is asked to repeat
    entering the data */
    while (objVelocity <= 0)
    {
        cout << "\nAn object's velocity can not be 0 or negative.\n"
             << "Enter the object's velocity (mp/s): ";
        cin >> objVelocity;
    }

    /* Call: kineticEnergy */
    objKineticEnergy = kineticEnergy(objMass, objVelocity);

    /* Display: The object's mass, velocity, and total
       kinetic energy */
    cout << "\nA moving object with the following parameters:\n\n"
         << "Mass: " << objMass << " kg\n"
         << "Velocity " << objVelocity << " m/s\n\n"
         << "Has a total kinetic energy of: "
         << objKineticEnergy << " J.\n";

    pauseSystem();
    return 0;
}

/* **********************************************************
   Definition: kineticEnergy

   This function accepts an object's mass (in kilograms) and
   velocity (in meters per second) as arguments. The function
   returns the amount of kinetic energy that the object has.
   ********************************************************** */

double kineticEnergy(double calcMass, double calcVelocity)
{
    /* Variable: Total Object Energy */
    double objTotalEnergy = 0.0;

    /* Calculation: The object's total energy */
    objTotalEnergy = calcMass * pow(calcVelocity, 2) / 2;

    /* Return: The objects total energy */
    return objTotalEnergy;
}

No comments:

Post a Comment