In this program, we consider a body of mass m. Write a program that initially as
ID: 3620747 • Letter: I
Question
In this program, we consider a body of mass m. Write a program that initially asks the user to enter the mass of the body in kilograms. Then, the program displays the following: K[inetic] P[otential] E[xit] Please select an option:_ Then, if the user selects: K (lowercase or uppercase): The program calls a user-defined function float kinetic (float mass) that takes the mass inserted by the user as argument and, internally to the function, asks the user to enter the velocity v of the body in meters per second. Then, the function calculates and returns the calculated kinetic energy of the body (E=1/2*m*v). Back to the main() function, the program uses the returned value to display the numeric result to the user using only 2 decimal digits. --------------------------------------------------------------------------------------------------------P (lower case or uppercase): The program calls a user-defined function float potential (float mass) that takes the mass inserted by the user as argument and, internally to the function, asks the user to enter the height h at which the body rests in meters. Then, the funciton calculates and returns the calculated potential energy of the body (E=m*g*h) where g is the acceleration due to gravity (9.18 m/s ). Back to the main() function, the program uses the returned value to display the numeric result to the user using only 2 decimal digits.
--------------------------------------------------------------------------------------------------------- E (lowercase or uppercase):
Note: Until the last option ('e' or 'E') is selected, the main program continuously loops back, asking the user to insert a new value for the body mass and a new option for the energy calculation. i am new to C++ programming, and do not understand how to write these types of codes.
Explanation / Answer
Before you look at this, your teacher has assigned you to return a float from both of your functions when he tells you to multiply by a double which is a no-no in c++. This program works I just changed all your floats to doubles. If you have any questions about anything I did please ask. #include <iostream> cout << "The kinetic energy of the mass is " << fixed << setprecision(2); cout << kinetic(mass) << " Joules." << endl; cout << "The potential energy of the mass is " << fixed << setprecision(2); cout << potential(mass) << " Joules." << endl; cout << "Please insert the velocity of the body in m/s:" << endl; cin >> velocity;Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.