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

Functions - Upset Fowls In this assignment, you’ll make a knock­off version of t

ID: 3875069 • Letter: F

Question

Functions - Upset Fowls

In this assignment, you’ll make a knock­off version of the Angry Birds game. The starter program is a working first draft of the game.

1. Correct the first FIXME by moving the intro text to a function named PrintIntro. Development suggestion: Verify the program has the same behavior before continuing.

2. Correct the second FIXME. Notice that the function GetUsrInpt will need to return two values: fowlAngle and fowlVel.

3. Correct the third FIXME. Notice that the function LaunchFowl only needs to return the value fowlLandingX, but needs the parameters fowlAngle and fowlVel.

4. Correct the fourth FIXME. Notice that the function DtrmnIfHit only needs to return the value didHitSwine, but needs the parameters fowlLandingX and swineX.

The main should now look like the following code and the program should behave the same as the first draft:
intmain(){

doublefowlAngle=0.0;//angleoflaunchoffowl(rad) doublefowlVel=0.0;//velocityoffowl(m/s) doubleswineX=0.0;//distancetoswine(m) doublefowlLandingX=0.0;//fowl’shoriz.dist.fromstart(m) booldidHitSwine=false;//didhittheswine?

srand(time(0)); swineX=(rand()%201)+50;

PrintIntro(); GetUsrInpt(swineX,fowlAngle,fowlVel); fowlLandingX=LaunchFowl(fowlAngle,fowlVel); didHitSwine=DtrmnIfHit(fowlLandingX,swineX)

return0; }

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.

****Example

Here is an example program execution (user input is highlighted here for clarity):

Welcome to Upset Fowl! The objective is to hit the Mean Swine.

The Mean Swine is 84 meters away.

Enter launch angle(deg):45

Enter launch velocity(m/s):30

Time 1 x= 0 y= 0

Time 2 x= 21 y= 16

Time 3 x= 42 y= 23

Time 4 x= 64 y= 20

Time 5 x= 85 y= 6

Time 6 x=106 y=-16

Missed'em...

The Mean Swine is 84 meters away.

Enter launch angle(deg):45

Enterlaunchvelocity(m/s):25

Time 1 x= 0 y= 0

Time 2 x= 18 y= 13

Time 3 x= 35 y= 16

Time 4 x= 53 y= 9

Time 5 x= 71 y= -8

Hit'em!!!

****starter 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;

}

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 of fowl (rad)

double fowlVel = 0.0; // velocity of fowl (m/s)

double fowlX = 0.0; // object's horiz. dist. from start (m)

double fowlLandingX = 0.0; // fowl’s horiz. dist. from start (m)

double swineX = 0.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?

  

srand(time(0));

swineX = (rand() % 201) + 50;

  

// FIXME Make into a function called PrintIntro

cout << "Welcome to Upset Fowl! ";

cout << "The objective is to hit the Mean Swine by launching an Upset Fowl. ";

// FIXME Make into a function called GetUsrInpt

cout << " The Mean Swine is " << swineX << " meters away. ";

cout << "Enter fowl launch angle (deg): ";

cin >> fowlAngle;

fowlAngle = DegToRad(fowlAngle); // convert to radians

cout << "Enter fowl launch velocity (m/s): ";

cin >> fowlVel;

  

// FIXME Make into a function called LaunchFowl

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

beforeSwineX = swineX - 30;

if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX)) {

cout << "Hit'em!!!" << endl;

didHitSwine = true;

} else {

cout << "Missed'em..." << endl;

didHitSwine = false;

}

return 0;

}

Explanation / Answer

Given below is the modified code which organizes code as functions. The program will show a random number for initial mean distance of Swine.

Please do rate the answer if it was helpful. Thank you.


#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;
}

void PrintIntro(){
cout << "Welcome to Upset Fowl! ";
cout << "The objective is to hit the Mean Swine by launching an Upset Fowl. ";
}

void GetUsrInpt(double &fowlAngle, double &fowlVel){
  
cout << "Enter fowl launch angle (deg): ";
cin >> fowlAngle;
  
fowlAngle = DegToRad(fowlAngle); // convert to radians
cout << "Enter fowl launch velocity (m/s): ";
cin >> fowlVel;
  
}


double LaunchFowl(double fowlAngle, double fowlVel){
double fowlX = 0.0; // object's horiz. dist. from start (m)
double fowlY = 0.0; // object's height above ground (m)
double t = 1.0; // time (s)

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){
bool didHitSwine = false; // did hit the swine?
double beforeSwineX = 0.0; // distance before swine that is acceptable as a hit (m)

beforeSwineX = swineX - 30;
  
if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX)) {
didHitSwine = true;
} else {
didHitSwine = false;
}
return didHitSwine;

}

int main() {
double fowlAngle = 0.0; // angle of launch of fowl (rad)
double fowlVel = 0.0; // velocity of fowl (m/s)
double fowlLandingX = 0.0; // fowl’s horiz. dist. from start (m)
double swineX = 0.0; // distance to swine (m)
  
srand(time(0));
swineX = (rand() % 201) + 50;
  
// FIXED Make into a function called PrintIntro
PrintIntro();
  
cout << " The Mean Swine is " << swineX << " meters away. ";

// FIXED Make into a function called GetUsrInpt
GetUsrInpt(fowlAngle, fowlVel);
  
  
// FIXED Make into a function called LaunchFowl
fowlLandingX = LaunchFowl(fowlAngle, fowlVel);
  
  
// FIXED Make into a function called DtrmnIfHit
if (DtrmnIfHit(fowlLandingX, swineX)) {
cout << "Hit'em!!!" << endl;
} else {
cout << "Missed'em..." << endl;
}
  
return 0;
  
}

output
======
Welcome to Upset Fowl!
The objective is to hit the Mean Swine by launching an Upset Fowl.

The Mean Swine is 73 meters away.
Enter fowl launch angle (deg): 45
Enter fowl launch velocity (m/s): 25
Time 1 x = 0 y = 0
Time 2 x = 18 y = 13
Time 3 x = 35 y = 16
Time 4 x = 53 y = 9
Time 5 x = 71 y = -8
Hit'em!!!