Question

In: Computer Science

Program specifics: Write a C++ program that does the following: a. Asks the user for the...

Program specifics:

Write a C++ program that does the following:

a. Asks the user for the distance to the pin and the depth of the green (both in yards). (Note: The pin is the hole in the green and the depth is the diameter of the green, assuming it is circular.)

b. Asks the user for an integer club number from 2 to 10, where 10 is the pitching wedge (this club lifts the ball out of rough, sand, etc). If the user enters an invalid club number, the program prints a warning and asks for the club number again.

c. Asks the user for a swing type, from 1 to 4, where 4 is a full swing, 3 is a three-quarters swing, 2 is a half swing, and 1 is a quarter swing. The program should check to make sure the swing is a valid number and if it is not, allow the user to try again. There is no requirement to limit the number of guesses of either the club type or swing strength,

d. Uses the following constants - a1, b1, a2, b2, a3, and b3—that are embedded into the following equations:

Golf Constants for the program

A              B

10.2        3.27

53.5        -1.75

19.6       4.89

clubangle(degrees) = a1 + b1*0.85*clubnumber; (see figure at right)

clublength(inches) = a2 + b2*1.05*clubnumber; (see figure at right)

clubspeed(yards/s) = 1.1 * (a3 + b3 * swingnumber) * (clublength(inches)/40)^2;

e. Determines the distance the ball travels, how far it lands from the hole that is on the green (the "pin"), and whether it hits the green. You can assume the ball travels perfectly straight, there is no wind resistance, and the pin is in the center of the green. To determine the distance the ball travels, use the following equation:

       distance = club speed^2 * sin(2*club angle in radians)/g

     (g = 32.2 ft/s2 is the acceleration of gravity in English units—make sure you convert your units correctly to get distance in yards).

f. The program reports to the screen, in a well-formatted table: the club used, the swing number, the distance the ball travels, the distance from the pin that the ball hits the ground (i.e., the error or accuracy), and "Yes" if the ball hits on the green, or else "No". Assume that the ball does not roll after hitting the ground (really wet ground).

g. The table must have appropriate labels.

h. If the ball does not hit the green, the program continues to ask the user for another club number and swing speed.

i. If the club number entered is 99, the program ends

Solutions

Expert Solution

/****************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
// Declaring constants
const double A1 = 10.2;
const double B1 = 3.27;
const double A2 = 53.5;
const double B2 = -1.75;
const double A3 = 19.6;
const double B3 = 4.89;
const int SENTINEL = 99;
double g = 32.2 * 0.333333;

// Declaring variables
double clubAngle, clublength, clubspeed, distance, pinDistance, depth;
int clubnumber, swingnumber;
string choice;


// setting the precision to two decimal places
std::cout << std::setprecision(2) << std::fixed;

// Getting the pin distance entered by the user
cout << "Enter the distance from the Pin (in yards):";
cin >> pinDistance;

// getting the green depth entered by the user
cout << "Enter the Green Depth :";
cin >> depth;

// Getting the valid input entered by the user
while (true)
{

while (true)
{
cout << "\nEnter Club Number :";
cin >> clubnumber;
if (clubnumber == 99)
{
exit(0);
}
else if (clubnumber < 2 || clubnumber > 10)
{
cout << "** Invalid.Must be between 2-10 **" << endl;
}
else
break;
}

// Getting the valid input entered by the user
while (true)
{
cout << "Enter Swing Number :";
cin >> swingnumber;
if (swingnumber < 1 || swingnumber > 4)
{
cout << "** Invalid.Must be between 1-4 **" << endl;
}
else
break;
}


// Calculating the club angle
clubAngle = (A1 + B1 * 0.85 * clubnumber) * (3.14159 / 180);

// Calculating the club length
clublength = A2 + B2 * 1.05 * clubnumber;

// Calculating the club speed
clubspeed = 1.1 * (A3 + B3 * swingnumber) * pow((clublength / 40), 2);
  
// Calculating the distance the ball travelled
distance = pow(clubspeed, 2) * sin(2 * clubAngle) / g;
  
// Displaying the report
cout << setw(35) << left << "Club Used :" << clubnumber << endl;
cout << setw(35) << left << "Swing Number :" << swingnumber << endl;
cout << setw(35) << left << "Distance the ball travelled :" << distance << " feet." << endl;
cout << "The distance from the pin that the ball hits the ground (in yards) : ";
cout << abs(pinDistance - distance) << endl;

if (abs(pinDistance - distance) <= depth)
{
choice = "Yes";
}
else
{
choice = "No";
}
cout << "Is the ball hits on the green :" << choice << endl;
}


return 0;
}

/****************************************************/

/****************************************************/

Output:

/****************************************************/


Related Solutions

Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
In C write a program that asks the user for the dimensions of 3 sides of...
In C write a program that asks the user for the dimensions of 3 sides of a triangle. Each side dimension must be between 1.00 and 100.00 including 1.00 and 100.00. If the side dimensions indeed can make a valid triangle then the program should use these values to determine the perimeter and the area of the valid triangle 1. Must use programmer define functions 2. Your program must have at least 4 functions, main and the following 3 functions:...
Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using SELECTION SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_SelectionSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Write a C program that performs the following: Asks the user to enter his full name...
Write a C program that performs the following: Asks the user to enter his full name as one entry. Asks the user to enter his older brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have any older brother). Asks the user to enter his younger brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have...
Using Python Write a program that does the following in order: 1.     Asks the user to enter...
Using Python Write a program that does the following in order: 1.     Asks the user to enter a name 2.     Asks the user to enter a number “gross income” 3.     Asks the user to enter a number “state tax rate” 4.     Calculates the “Federal Tax”, “FICA tax” and “State tax” 5.     Calculates the “estimated tax” and round the value to 2 decimal places 6.     Prints values for “name”, “gross income” and “estimated tax” The program should contain three additional variables to store the Federal tax, FICA...
2. Write a program C++ that asks the user for a number (not necessary to force...
2. Write a program C++ that asks the user for a number (not necessary to force any particular requirements). Write a function with the following signature: double square(double x) that returns the square of the user's number (x * x). 3. Write a C++ program that asks the user for an integer. Write a function that returns 1 of the number is even, and 0 if the number is odd. Use this function signature: int isEven(int x). 4. Write a...
Write a C program that loops and asks a user to enter a an alphanumeric phone...
Write a C program that loops and asks a user to enter a an alphanumeric phone number and converts it to a numeric one. No +1 at the beginning. You can put all code in one quiz1.c file or put all functions except main in phone.c and phone.h and include it in quiz1.c Submit your *.c and .h files or zipped project */ #pragma warning (disable: 4996) //windows #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> enum { MaxLine =...
Write a c++ program that asks the user for a date (Month and Day only) and...
Write a c++ program that asks the user for a date (Month and Day only) and displays the season in this day.The program asks then the user if he needs to enter another date, if he answers with ’Y’ or ’y’, the programwill take another date from the user and displays the season in this day.The program will keep repeating this until the user enters a character different than ’Y’ and ’y’.•Fall starts in September 21st•Winter starts in December 21st•Spring...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT