kinetic energy

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
48
49
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

// Prototypes
double kineticEnergy(double mass , double meters);
double getKE();

// Main Function
int main()
{
	double mass , meters , KE , Energy;

	cout << "This will calculate how much kinetic energy an object has.\n\n";

	cout.setf(ios_base::fixed , ios_base::floatfield);
	cout.precision(2);
	
	KE = getKE();
	Energy = kineticEnergy(mass , meters);

	cout << "The object with a mass weight and velocity of " << Energy << " kilograms and meters." << KE << endl;
	
	_getch();

	return 0;
}
// Get Kinetic Energy Function
double getKE()
{
	double mass , meters;

	cout << "Enter the objects weight in kilograms: ";
	cin >> mass;
	cout << "Enter the objects velocity in meters: ";
	cin >> meters;

	cout << "\n\n";

	return mass;
	return meters;
}
// Kinetic Energy Function
double kineticEnergy(double mass , double meters)
{
	return (1.0 / 2.0) * (mass * meters) * 2;
}


Im having a problem at line 22, when i go to compile it get 2 errors saying
unitialized local varibales mass and meters used.

I need the kinetic energy function to return that result to where its called in the function, any ideas on how to fix this.
closed account (zb0S216C)
A function can only return one piece of data. When two "return" statements are contiguous, the second "return" is ignored completely. To overcome this restriction, you have a few options:

1) declare two "double" formal parameters of type "double &" and assign the values to them.
2) declare a structure [with two "double" members] and return an instance of it; using "mass" and "meters" to initialise the instance. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct PairOfDoubles
{
    PairOfDoubles(double const mass_init, double const meters_init)
        : mass(mass_init), meters(meters_init)
    { }

    double mass;
    double meters;
};

PairOfDoubles a_function( )
{
    // ...
    return(PairOfDoubles(1.0, 3.0));
}

Wazzak
Last edited on
Or you could just combine the functions into one.

1
2
3
4
5
6
7
8
9
10
11
12
13
double kineticEnergy()
{
   double mass, meters;

    cout << "Enter the objects weight in kilograms: ";
    cin >> mass;
    cout << "Enter the objects velocity in meters: ";
    cin >> meters;

    cout << "\n\n";

    return (1.0 / 2.0) * (mass * meters) * 2;
}
Isn't that the wrong formula for K.E. ?
It should be ½mv²
where m is the mass, v is the velocity (in metres per second).
that can be expressed as 0.5 * m * v * v
the error is line 22 in my code, its in the main function at Energy = kineticEnergy(mass , meters). its just a different way i did for the formula

in my formula mass is the mass weight and meters is the velocity

but anyways the kineticenergy function doesnt seem to give me any problems its just that line in the main function
Last edited on
Sorry, I know my remarks are off-topic in a C++ discussion, but it did look to me as though "squared" had been misinterpreted as "multiply by two".

(1.0 / 2.0) * (mass * meters) * 2;
and (1.0 / 2.0) * (mass * meters * meters);
will give identical results for the case where meters == 2
(or meters == 0)

It will still compile and execute either way, but of course the results will be different.

Edit:
In fact this expression (1.0 / 2.0) * (mass * meters) * 2 will simplify, the 1/2 and *2 cancel out. That just leaves mass * meters which is the formula for a different quantity, linear momentum.

But, momentum != kinetic energy.

Last edited on
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
48
49
50
51
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

// Prototypes
double kineticEnergy(double mass , double meters);
double getKE(); //returns one value

// Main Function
int main()
{
	double mass , meters , KE , Energy; //(*) these variables

	cout << "This will calculate how much kinetic energy an object has.\n\n";

	cout.setf(ios_base::fixed , ios_base::floatfield);
	cout.precision(2);
	
	KE = getKE();
	Energy = kineticEnergy(mass , meters);

	cout << "The object with a mass weight and velocity of " << Energy << " kilograms and meters." << KE << endl;
	
	_getch();

	return 0;
} // (*) die here

// Get Kinetic Energy Function
double getKE()
{
	double mass , meters; //these are different that the ones on (*)

	cout << "Enter the objects weight in kilograms: ";
	cin >> mass;
	cout << "Enter the objects velocity in meters: ";
	cin >> meters;

	cout << "\n\n";

	return mass;
	return meters; //this line is never executed
}

// Kinetic Energy Function
double kineticEnergy(double mass , double meters)
{
	return (1.0 / 2.0) * (mass * meters) * 2; //this equation is incorrect
}
so how would i get the kinetic energy function to return the value, when i call it, it tell me that mass and meters are uninitialized.

sorry, its just im not understanding that part.

oh and ok chervil, thanks for noticing that.
Last edited on
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
int main()
{
   double mass , meters , KE , Energy;

   cout << "This will calculate how much kinetic energy an object has.\n\n";

   cout.setf(ios_base::fixed , ios_base::floatfield);
   cout.precision(2);

   getKE(mass, meters); //stores mass & meters values to vars in main.
   Energy = kineticEnergy(mass, meters); //passes new values.
  
   //+ cout statement here

   _getch();

   return 0;
} 

// Get Kinetic Energy Function
double getKE(double & mass, double & meters) // pass by reference
{
	double mass , meters; 

	cout << "Enter the objects weight in kilograms: ";
	cin >> mass;
	cout << "Enter the objects velocity in meters: ";
	cin >> meters;

	cout << "\n\n";

	return mass;
	return meters;
}

// Kinetic Energy Function
double kineticEnergy(double mass , double meters)
{
	return (1.0 / 2.0) * (mass * meters) * 2; //this equation is incorrect
}
Last edited on
Thank you thejjjunk it makes sense now and it works now, may i ask what the & symbols do in double getKE( double & mass , double & meters)
It means you are passing by reference. When you don't have them, you are passing by value. When passing by value, new copies of the variables are made in the new function, and the values of the original variables don't change. When you pass by reference, you are sending a 'reference' to the variables themselves, so whatever operations you perform on those variables in said function will affect the values of the variables.
Topic archived. No new replies allowed.