Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me to understand how the void functions plays a part in the sec

ID: 3866410 • Letter: C

Question

Can someone help me to understand how the void functions plays a part in the second program? What is the difference between the 2 programs if they both produce the same output?

Program #1

#include <iostream> // Required for cin, cout

#include <iomanip> // Required for set(w)

#include <cmath>    // Required for trigonometric functions

using namespace std;

int main ()

{

     // Declare and initialize objects.

    const double g = 32.17; // gravitational constant

    double theta;           // input - angle of elevation (radians)

    double distance;        // input - distance to target (feet)

    double velocity;        // input - projectile velocity (ft./sec)

    double time;            // input – time of flight (seconds)

    double height;          // input – height at impact (feet)

    // Get user input.

  cout << “ What is the angle of elevation in radians? “ << endl;

   cin >> theta;

   cout << “ What is the velocity in feet/sec? “ << endl;

   cin >> velocity;

   cout << “ What is the distance to target in feet? “ << endl;

   cin   >> distance;

    // Set formats

    cout.setf(ios::fixed);

    cout.precision(2);

   time = distance / (velocity x cos(theta));

   height = velocity x sin(theta) x time – (g x time^2)/2);

    // Print projectile duration and height upon impact.

   cout << “ The projectiles flight took “ << time << “ seconds “ <<

    << “and was at “ << height << “ feet ” << “ upon impact “ << endl;

   // Exit program

   return 0;

}

Program #2

#include <iostream> // Required for cin, cout
#include <iomanip> // Required for set(w)
#include <cmath> // Required for trigonometric functions

using namespace std;

void displayTimeAndHeight
( double theta,
double distance,
double velocity );

int main()
{
// Declare and initialize objects
double theta; // input -- angle of elevation (in radians)
double distance; // input -- distance to target (in feet)
double velocity; // input -- projectile velocity (in ft/sec)

// Get user input
cout << "What is the angle of elevation in radians?" << endl;
cin >> theta;

cout << "What is velocity in ft/sec?" << endl;
cin >> velocity;

cout << "What is the distance to target in feet?" << endl;
cin >> distance;

//function calling
displayTimeAndHeight(theta,distance,velocity);

//Exit program
return 0;

}

//prints the time of flight and height at impact of the projectile
void displayTimeAndHeight(double theta, double distance, double velocity)
{
const double g = 32.17; //gravitational constant
double time; //output -- time of flight(in seconds)
double height; //output -- height at impact(in feet)

time = distance / (velocity*cos(theta));
height = velocity*sin(theta)*time - ((g*time*time)/2);

// Set formats
cout.setf(ios::fixed);
cout.precision(2);

// Print projectile duration and height upon impact
cout << "The projectiles flight took " << time << " seconds " << "and was at "
<< height << " feet " << "upon impact" << endl;

}

Explanation / Answer

In the second program, we see the main() of program #1 is divded into two parts, main and displayTimeAndHeight.

Let us see the program #2:

There is a prototype declaration:void displayTimeAndHeight
( double theta,
double distance,
double velocity );

This function has three parameters: theta,distance and velocity of type double. And does not return any value.

In main(), the doulbe variables theta,distance and velocity are declared.

And the values for the same are accepted from the users.

Then there is a call to function :void displayTimeAndHeight( theta,distance,velocity );, with the values of theta,distance and velocity.

Here , the declaration of ,

const double g = 32.17; //gravitational constant
double time; //output -- time of flight(in seconds)
double height; //output -- height at impact(in feet)

are made here locally inside the function , instead of main() in program #1

Also, the calculations,

time = distance / (velocity*cos(theta));

height = velocity*sin(theta)*time - ((g*time*time)/2);

are done here, instead of main() in program #1.

Lastly with format setting , the message is printed with the following lines of code,

// Set formats
cout.setf(ios::fixed);
cout.precision(2);

// Print projectile duration and height upon impact
cout << "The projectiles flight took " << time << " seconds " << "and was at "
<< height << " feet " << "upon impact" << endl;

instead of main(), like in Program #1.

Thus, we noticed, second program does exactly , what the first program does.The only difference is , second program is divided in functions(procedures) ,making it more easy to understand.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote