Question

5.15 PROGRAM: Functions - Upset Fowls (C++) (1) Correct the first FIXME by moving the intro...

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):

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

Enter 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!!!

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!\n";
cout << "The objective is to hit the Mean Swine.\n";

// FIXME Make into a function called GetUsrInpt then call GetUsrInpt here
cout << "\nThe Mean Swine is " << swineX << " meters away.\n";
  
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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Working code implemented in C++ and appropriate comments provided for better understanding.

Source Code for main.cpp:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

//Global Constants
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!\n";
cout << "The objective is to hit the Mean Swine.\n";
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)
{
//code that gets from the user the fowl's launch angle and velocity
cout << "\nThe Mean Swine is " << swineX << " meters away.\n";

cout << "Enter launch angle (deg): ";
cin >> fowlAngle;
cout << endl;
fowlAngle = DegToRad(fowlAngle); // convert to radians

cout << "Enter launch velocity (m/s): ";
cin >> fowlVel;
cout << endl;
  
return;
}


// Given fowl launch angle and velocity
// Return horiz. landing position of fowl
double LaunchFowl(double fowlAngle, double fowlVel)
{
//code that calculates and returns the horiz. landing position of fowl
double fowlLandingX = 0.0;
double fowlX = 0.0;
double fowlY = 0.0;
double t = 1.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);


fowlLandingX = fowlX;

return fowlLandingX;
}


// 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

double beforeSwineX = 0;
bool didHitSwine = false;

beforeSwineX = swineX - 30;
if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX))
{
cout << "Hit'em!!!" << endl;
didHitSwine = true;
return didHitSwine;
}
else
{
cout << "Missed'em..." << endl;
didHitSwine = false;
return didHitSwine;
}


}

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;
int didHit = 0;

srand(20); //to ensure the correct output for grading
swineX = (rand() % 201) + 50;

  
//call PrintIntro here
PrintIntro();
  
for(int i = 0; i < tries; i++)
{
//call GetUsrInpt here
GetUsrInpt(swineX, fowlAngle, fowlVel);
  
//call LaunchFowl here
fowlLandingX = LaunchFowl(fowlAngle, fowlVel);
  
//call DtermnIfHit here
didHit = DtrmnIfHit(fowlLandingX, swineX);
  
if (didHit == 1)
{
return 0;
}
  

}

return 0;
}

Sample Output Screenshots:

> clang++-7 -pthread -std=c++17 -o main main.cpp 3./main Welcome to Upset Fowl! The objective is to hit the Mean Swine. The M


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
5.15 PROGRAM: Functions - Upset Fowls (C++) (1) Correct the first FIXME by moving the intro...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given the incomplete program below, write two functions, one called integer and another called decimal. You...

    Given the incomplete program below, write two functions, one called integer and another called decimal. You can use following incomplete program. Your assignment is to supply the necessary missing code: #include #include using namespace std; int integer (….) { //REPLACE THE … WITH THE REQUIRED PARAMETER (A DOUBLE) //WRITE HERE THE CODE TO EXTRACT THE INTEGER AND RETURN IT } double decimal (….) { //REPLACE THE … WITH THE REQUIRED PARAMETER //WRITE HERE THE CODE TO EXTRACT THE DECIMAL PORTION,...

  • using the source code at the bottom of this page, use the following instructions to make...

    using the source code at the bottom of this page, use the following instructions to make the appropriate modifications to the source code. Serendipity Booksellers Software Development Project— Part 7: A Problem-Solving Exercise For this chapter’s assignment, you are to add a series of arrays to the program. For the time being, these arrays will be used to hold the data in the inventory database. The functions that allow the user to add, change, and delete books in the store’s...

  • please provide full answer with comments this is just begining course of c++ so don't use...

    please provide full answer with comments this is just begining course of c++ so don't use advanced tequenicks I'll put main.cpp, polynomial.h, polynomial.cpp and Cimg.h at the bottom of pictures. If you help me with this will be greatly thankful thank you main.cpp #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include <iostream> using std::cout; using std::endl; int main() { pic10a::polynomial p1; p1.setCoeff(0, 1.2); p1.setCoeff(3, 2.2); p1.setCoeff(7, -9.0); p1.setCoeff(7, 0.0); //degree of polynomial is now 3 cout << p1 <<...

  • In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits&gt...

    In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits> #include <iostream> #include <string> // atoi #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ const unsigned int DEFAULT_SIZE = 179; // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() {...

  • C++ Create a program that finds the dot product of two vectors. I'm currently trying to...

    C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...

  • Question 19 4 pts Using the program below, please complete the program by adding 2 functions...

    Question 19 4 pts Using the program below, please complete the program by adding 2 functions as follow: 1. A function named getAverage that calculates and returns the Average. 2. A function named getMaximum that returns the maximum of the three numbers. Make sure to use the proper data types for all variables and functions. #include <iostream> using namespace std; //function getAverage //function getMaximum int main(int argc, char** argv) { int x, y, z; cout << "Enter three whole numbers:...

  • I did a program in computer science c++. It worked fine the first and second time...

    I did a program in computer science c++. It worked fine the first and second time when I ran the program, but suddenly it gave me an error. Then, after a few minutes, it started to run again and then the error showed up again. This is due tonight but I do not know what is wrong with it. PLEASE HELP ME! Also, the instruction for the assignment, the code, and the error that occurred in the program are below....

  • C++ Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money...

    C++ Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market, to attract customers as well as meet their specific needs. Two of the most commonly used accounts are savings and checking. Each of these accounts has various options. For example, you may have a savings account that requires no minimum balance but has a lower interest rate. Similarly, you may have a checking account that limits the number of checks you may write....

  • Write this program in c++:

    test.txtLab7.pdfHeres the main.cpp:#include "Widget.h"#include <vector>#include <iostream>#include <string>#include <iomanip>#include <fstream>using std::ifstream;using std::cout;using std::cin;using std::endl;using std::vector;using std::string;using std::setprecision;using std::setw;bool getWidget(ifstream& is, Widget& widget){ string name; int count; float unitCost; if (is.eof())  { return false; } is >> count; is >> unitCost; if (is.fail())  { return false; } is.ignore(); if (!getline(is, name))  { return false; } widget.setName(name); widget.setCount(count); widget.setUnitCost(unitCost); return true;}// place the definitions for other functions here// definition for function mainint main(){ // Declare the variables for main here  // Prompt the...

  • My if/else statement wont run the program that I am calling. The menu prints but once...

    My if/else statement wont run the program that I am calling. The menu prints but once I select a number the menu just reprints, the function called wont run. What can I do to fix this probelm? #include <iostream> #include "miltime.h" #include "time.h" #include "productionworker.h" #include "employee.h" #include "numdays.h" #include "circle.h" using namespace std; int main() { int select=1;     while (select>0) { cout << "Option 1:Circle Class\n"<< endl; cout << "Option 2:NumDay Class\n" << endl; cout <<"Option 3:Employee and Production...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT