5.15 PROGRAM: Functions - Upset Fowls (C++) (1) Correct the first FIXME by movin
ID: 3762280 • Letter: 5
Question
5.15 PROGRAM: Functions - Upset Fowls (C++)
(1) Correct the first FIXME by moving the intro text to the function stub named PrintIntro and then calling the PrintIntro function in main. Development suggestion: Verify the program has the same behavior before continuing.
(2) Correct the second FIXME by completing the function stub GetUsrInpt and then calling this function in main. Notice that the function GetUsrInpt will need to return two values: fowlAngle and fowlVel.
(3) Correct the third FIXME by completing the function stub LaunchFowl and calling this function in main. Notice that the function LaunchFowl only needs to return the value fowlLandingX, but needs the parameters fowlAngle and fowlVel.
(4) Correct the fourth FIXME by completing the function stub DtrmnIfHit and calling this function in main. Notice that the function DtrmnIfHit only needs to return the value didHitSwine, but needs the parameters fowlLandingX and swineX.
(5) Modify the program to continue playing the game until the swine is hit. Add a loop in main that contains the functions GetUsrInpt, LaunchFowl, and DtrmnIfHit.
(6) Modify the program to give the user at most 4 tries to hit the swine. If the swine is hit, then stop the loop.
Here is an example program execution (user input is highlighted here for clarity):
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const double pi = 3.14159265;
const double grav = 9.8; // Earth gravity (m/s^2)
// Given time, angle, velocity, and gravity
// Update x and y values
void Trajectory(double t, double a, double v,
double& x, double& y) {
x = v * t * cos(a);
y = v * t * sin(a) - 0.5 * grav * t * t;
return;
}
// convert degree value to radians
double DegToRad(double deg) {
return ((deg * pi) / 180.0);
}
// print time, x, and y values
void PrintUpdate(double t, double x, double y) {
cout << "Time " << fixed << setprecision(0)
<< setw(3) << t << " x = " << setw(3)
<< x << " y = " << setw(3) << y << endl;
return;
}
// Prints game's intro message
void PrintIntro() {
// FIXME Add code that outputs intro message
return;
}
// Given swine's current horiz. position
// Get user's desired launch angle and velocity for fowl
void GetUsrInpt(double swineX, double &fowlAngle, double &fowlVel) {
// FIXME Add code that gets from the user the fowl's launch angle and velocity
return;
}
// Given fowl launch angle and velocity
// Return horiz. landing position of fowl
double LaunchFowl(double fowlAngle, double fowlVel) {
// FIXME Add code that calculates and returns the horiz. landing position of fowl
return 0.0;
}
// Given fowl's horiz. landing position and swine's horiz. position
// Return whether fowl hit swine or not
bool DtrmnIfHit(double fowlLandingX, double swineX) {
// FIXME Add code that returns true if fowl hit swine or false if not
return false;
}
int main() {
double t = 1.0; // time (s)
double fowlY = 0.0; // object's height above ground (m)
double fowlAngle = 0.0; // angle of launch (rad)
double fowlVel = 0.0; // velocity (m/s)
double fowlX = 0.0;
double fowlLandingX = 0.0; // object's horiz. dist. from start (m)
double swineX = 40.0; // distance to swine (m)
double beforeSwineX = 0.0; // distance before swine that is acceptable as a hit (m)
bool didHitSwine = false; // did hit the swine?
int tries = 4;
srand(20); //to ensure the correct output for grading
swineX = (rand() % 201) + 50;
// FIXME Make into a function called PrintIntro and then call PrintIntro here
cout << "Welcome to Upset Fowl! ";
cout << "The objective is to hit the Mean Swine. ";
// FIXME Make into a function called GetUsrInpt then call GetUsrInpt here
cout << " The Mean Swine is " << swineX << " meters away. ";
cout << "Enter fowl launch angle (deg): ";
cin >> fowlAngle;
cout << endl;
fowlAngle = DegToRad(fowlAngle); // convert to radians
cout << "Enter fowl launch velocity (m/s): ";
cin >> fowlVel;
cout << endl;
// FIXME Make into a function called LaunchFowl and then call LaunchFowl here
do {
PrintUpdate(t, fowlX, fowlY);
Trajectory(t, fowlAngle, fowlVel, fowlX, fowlY);
t=t+1.0;
} while ( fowlY > 0.0 ); // while above ground
PrintUpdate(t, fowlX, fowlY);
fowlLandingX = fowlX;
// FIXME Make into a function called DtrmnIfHit and then call DtermnIfHit here
beforeSwineX = swineX - 30;
if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX)) {
cout << "Hit'em!!!" << endl;
didHitSwine = true;
} else {
cout << "Missed'em..." << endl;
didHitSwine = false;
}
return 0;
}
Answer in C++. Thanks
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const double pi = 3.14159265;
const double grav = 9.8; // Earth gravity (m/s^2)
// Given time, angle, velocity, and gravity
// Update x and y values
void Trajectory(double t, double a, double v,
double& x, double& y) {
x = v * t * cos(a);
y = v * t * sin(a) - 0.5 * grav * t * t;
return;
}
// convert degree value to radians
double DegToRad(double deg) {
return ((deg * pi) / 180.0);
}
// print time, x, and y values
void PrintUpdate(double t, double x, double y) {
cout << "Time " << fixed << setprecision(0)
<< setw(3) << t << " x = " << setw(3)
<< x << " y = " << setw(3) << y << endl;
return;
}
// Prints game's intro message
void PrintIntro() {
cout<<"Welcome to Upset Fowl! ";
cout<<"The objective is to hit the Mean Swine. ";
return;
}
// Given swine's current horiz. position
// Get user's desired launch angle and velocity for fowl
void GetUsrInpt(double swineX, double &fowlAngle, double &fowlVel) {
cout << " The Mean Swine is " << swineX << " meters away. ";
cout << "Enter fowl launch angle (deg): ";
cin >> fowlAngle;
cout << endl;
fowlAngle = DegToRad(fowlAngle); // convert to radians
cout << "Enter fowl launch velocity (m/s): ";
cin >> fowlVel;
cout << endl;
return;
}
double LaunchFowl(double fowlAngle, double fowlVel) {
double t = 1.0; // time (s)
double fowlY = 0.0; // object's height above ground (m)
double fowlX = 0.0;
do {
PrintUpdate(t, fowlX, fowlY);
Trajectory(t, fowlAngle, fowlVel, fowlX, fowlY);
t=t+1.0;
} while ( fowlY > 0.0 ); // while above ground
PrintUpdate(t, fowlX, fowlY);
return fowlX;
}
bool DtrmnIfHit(double fowlLandingX, double swineX) {
double beforeSwineX = swineX - 30;
return (fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX);
}
int main() {
double fowlAngle = 0.0; // angle of launch (rad)
double fowlVel = 0.0; // velocity (m/s)
double fowlLandingX = 0.0; // object's horiz. dist. from start (m)
double swineX = 40.0; // distance to swine (m)
double beforeSwineX = 0.0; // distance before swine that is acceptable as a hit (m)
bool didHitSwine = false; // did hit the swine?
int tries = 4;
srand(20); //to ensure the correct output for grading
swineX = (rand() % 201) + 50;
PrintIntro();
while (tries != 0) {
GetUsrInpt(swineX, fowlAngle, fowlVel);
fowlLandingX = LaunchFowl(fowlAngle, fowlVel);
if (DtrmnIfHit(fowlLandingX, swineX)) {
cout << "Hit'em!!!" << endl;
didHitSwine = true;
} else {
cout << "Missed'em..." << endl;
didHitSwine = false;
}
if (didHitSwine) {
break;
} else {
--tries;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.