Calculate fallingDistance and kineticEnergy

Hi Everyone. I am new to C++ and in my first programming class. Our instructor has asked us to write a program that calculates the Falling Distance (using formula d=1/2*9.8*t^2) of an object based on a user input of the time and calculate the Kinetic Energy using formula (KE = 1/2mv^2)of an object based on a user input of the mass and velocity. I am completely lost. Here is what I have so far and it is absolutely terrible. Could someone help!? Thank you :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  #include <iostream>
#include <iomanip>
using namespace std;

// prototype for fallingDistance
void fallingDistance(double &, double);

const double g = 9.8;
double t;
double d;

// prototype for kineticEnergy
void kineticEnergy (int, int);
int Mass, Velo;


int main()
{
    // your code to read in user input for:
    // time, mass, and velocity.

    cout <<"\nThis program calculates an objects falling distance and the\n"
         << "amount of kinetic energy the object has.\n\n"
         << "Enter the object's time (in seconds): ";
    cin >> t;
    cout <<"Enter the object's Mass (in kilograms): ";
    cin >> Mass;
    cout <<"Enter the object's Velocity (in meters per second): ";
    cin >> Velo;
    
// function call to fallingDistance
    double d=0.5*g*t*t;
    cout <<"This object's falling distance is: "
            << fallingDistance(d,t) << " meters." <<endl;
    
// function call to kineticEnergy
    cout <<"The object's kinetic energy is: "
            <<kineticEnergy(Mass, Velo);
   
 // output the results 
    
    return 0;
}

// implementation of fallingDistance

// implementation of kineticEnergy 
It's a bit confusing. Do you need to calculate the kinetic energy of a completely unrelated mass? Not of the mass that was dropped for t seconds? I would have guessed that you needed to get a mass and a time from the user and then calculate the final position of the mass (assuming free fall from a resting position) and the final speed, and then use the calculated speed to calculate the kinetic energy (just a multiplication).

By the way, you don't need fallingDistance() on line 34 anymore. On line 32 you've already calculated the distance.
@helios -

The instructions are to include 2 functions within the program. The kinetic energy calculation is not based on the falling distance calculation but rather two separate calculations entirely.

So on line 34 it should read <<d<< "meters." <<endl;

?
Here is what I have so far with your suggestion correcting line 34.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <iomanip>
using namespace std;

// prototype for fallingDistance
void fallingDistance(double &, double);

const double g = 9.8;
double t;
double d;

// prototype for kineticEnergy
void kineticEnergy (int, int);
int Mass, Velo;
double ke;


int main()
{
    // your code to read in user input for:
    // time, mass, and velocity.
    cout <<"\nThis program calculates an objects falling distance and the\n"
         << "amount of kinetic energy the object has.\n\n"
         << "Enter the object's time (in seconds): ";
    cin >> t;
    cout <<"Enter the object's Mass (in kilograms): ";
    cin >> Mass;
    cout <<"Enter the object's Velocity (in meters per second): ";
    cin >> Velo;
    // function call to fallingDistance
    double d=0.5*g*t*t;
    cout <<"This object's falling distance is: "
            << d << " meters." <<endl;
    // function call to kineticEnergy
    
    double ke=0.5*Mass*Velo*Velo;
    cout <<"The object's kinetic energy is: "
            <<ke << endl;
    // output the results in a nice formatted manner
    
    return d;
}

// implementation of fallingDistance

// implementation of kineticEnergy
Looks like a good start! Just get it to compile ;D

Keep it as simple as possible. You only need the one variable in global space to represent acceleration of gravity on Earth. Constants are usually fully capitalized and usually more than one letter long. Don't try to return references just yet; just return a copy of the generated double.

Other tips:
- If you put the functions above main(), main() can see them and you don't need to prototype ;D
- Comment function parameters and what they'll return. Makes it very clear for reader or future you

Possible skeleton:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

// Acceleration of gravity on Earth in m/s²
const double G_ACCEL = 9.8;

// Returns a distance for a given time
//   t - Time in seconds
double FallingDistance(double t)
{
    return -1.0;
}

// Returns the kinetic energy of an object
//   m - Mass of object in kg
//   v - Velocity of object in m/s
double KineticEnergy(double m, double v)
{
    return -1.0;
}

int main()
{
    // Cool stuff in here
    return 0;
}

Possible output (I totally didn't fill the skeleton in and run it ;D):
This program calculates an object's falling distance and the
amount of kinetic energy the object has.

Enter the object's falling time (in seconds):  4.4

Enter the object's mass (in kilograms):  60.2
Enter the object's velocity (in meters per second):  5.7


The object's falling distance is 94.864 meters.
The object's kinetic energy is 977.949 joules.



The instructions are to include 2 functions within the program. The kinetic energy calculation is not based on the falling distance calculation but rather two separate calculations entirely.
Well, that doesn't really answer my question.
It just seems a bit weird that you're asked to calculate two completely unrelated things in the same program. It's like if a program asked for a voltage and resistance and calculated amperage, and then at the end it asked for a Fahrenheit temperature to convert to Celsius. It's strange.
@monae, for your latest post -- don't do any calculations in main(). Just send the parameters straight to the functions and immediately print out the responses. Calculating stuff is function's job.
@helios - i couldn't agree with you more that it is a completely strange request but that is the instructions lol.
@icy1

Thank you! I will give that a try now.
You should calculate 0.5gt2 in your function fallingDistance(t) - unless your teacher told you otherwise it would be more natural to make it a double-returning function to take one parameter (t) and return the distance d.

mass and velo should be declared in main(), not as global variables. They should also be doubles, not ints: they are physical quantities, aren't they.

Your kineticEnergy should take mass and velo (or possibly mass and t - see below) as double arguments and return the kinetic energy as a double.

You shouldn't be inputting velo, or you will be overspecifying the problem. For fall under gravity it is given by gt, and you have already input t. (Actually, you could work out kinetic energy from the loss in potential energy instead, mgd, so take your pick.)

Only g should be a global variable. t, d, Mass and Velo should be declared in main(), and in the argument list of any function that uses them.
Last edited on
Topic archived. No new replies allowed.