create a new code from this code by using functions and globals [if you can redu
ID: 3917050 • Letter: C
Question
create a new code from this code by using functions and globals [if you can reduce the size of "int main" that would be helpful]
#include<stdio.h>
int main( )
{
float m, h, p;
float g = 9.80665;
//m = mass in kg; g = gravity in m/sec^2; h=height in meters
// p = Gravitational Potential Energy
printf ( " Enter values for mass and height (no commas) ");
scanf ( "%f %f", &m, &h ) ;
p=m*g*h;//
printf ( " The result for PE(grav) is %f Joules",p );
return 0;
}
Explanation / Answer
#include #define g 9.80665 void read_mh(float *m, float *h) { //m = mass in kg; g = gravity in m/sec^2; h=height in meters // p = Gravitational Potential Energy printf(" Enter values for mass and height (no commas) "); scanf("%f %f", m, h); } void show_pe(float m, float h) { float p; p = m * g * h; // printf(" The result for PE(grav) is %f Joules", p); } int main() { float m, h; read_mh(&m, &h); show_pe(m, h); return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.