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

***DEV C++*** MUST INCLUDE: -Narrative -Comments -Formatting -Compiles/Executes

ID: 3753303 • Letter: #

Question

***DEV C++***

MUST INCLUDE:

-Narrative

-Comments

-Formatting

-Compiles/Executes

-Input/Computation/Output

-UDF's (User Defined Functions)

-Error Traps

Problem statement:

Interestingly, golf involves a lot of physics. This program will determine how far a ball will travel when struck with a given club and with a given swing strength.

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. 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. Check to make sure the swing is a valid number.

d. Reads from a data file. The file contains six numbers— a1, b1, a2, b2, a3, andb3—that are constants for the following equations:

Golf Constants for data file golf.txt

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

One possible output for the program would look like this:

d. Reads from a data file. The file contains six numbers— a1, b1, a2, b2, a3, andb3—that are constants for the following equations:

Golf Constants for data file golf.txt

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;

189 yard Time-5.940 sec 45 yards sec

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

//This method calculates actual distance
double getactualDistance(double clubspeed,double clubangleRadian, double g) {
double actualDistance;
actualDistance =pow(clubspeed,2)*sin(2*clubangleRadian)/g;
return actualDistance;
}

//This method will calculate club angle in radians
double getclubangle(double a1,double b1, int clubNumber) {  
double clubangle,clubangleRadian;
clubangle=a1+b1*0.85*(double)clubNumber;
clubangleRadian=clubangle*0.0174533;
return clubangleRadian;
}

//This method will return clublength in inches
double getclublength(double a2,double b2, int clubNumber) {
double clublength;
clublength=a2+b2*1.05*(double)clubNumber;
return clublength;
}

//This method will return club speed in yards per second
double getclubspeed(double a3,double b3, int swing, double clublength) {
double clubspeed;
clubspeed=1.1*(a3+b3*(double)swing)*pow((clublength/40),2);
return clubspeed;
}

//This method will return the clubNumber
int getClubNumber() {
int clubNumber;
while(1) {
cout<<"Input a club number from 2 to 10 <10=wedge> or 99 to quit:";
cin>>clubNumber;
if(clubNumber==99) return 99;
if(clubNumber>10 || clubNumber<2) cout<<"Invalid entry of club number,Please enter value in range only"<<endl;
else break;
}
return clubNumber;  
}

//This method will return the swing
int getSwing() {
int swing;
while(1) {
cout<<"Input the swing from 1 to 4 (quarter to full):";
cin>>swing;
if(swing<1 || swing>4) cout<<"Invalid entry of swing number,Please enter value in range only"<<endl;
else break;
}
return swing;
}

//Main method of the program, which is the driver method
int main()
{
int distance,depth,clubNumber,swing;
cout<<"Input the distance to the pin in yards:";
cin>>distance;
cout<<"Input the depth of the green in yards:";
cin>>depth;
string result="";
while(result!="Yes") {//Run the loop till the balls hits the green portion
clubNumber=getClubNumber();
if(clubNumber==99) return 0;
swing=getSwing();
char str[255];
double a1,a2,a3,b1,b2,b3;
ifstream in("golf.txt");//Open the txt file
int count=0,i;
while(in) {
in.getline(str, 255);
if(count==1) {
string s1,s2;
i=0;
while(str[i]!=' ') {
s1=s1+str[i];
i++;
}
a1=stod(s1);
i++;
while(str[i]!='') {
s2=s2+str[i];
i++;
}
b1=stod(s2);
}
else if(count==2) {
string s1,s2;
i=0;
while(str[i]!=' ') {
s1=s1+str[i];
i++;
}
a2=stod(s1);
i++;
while(str[i]!='') {
s2=s2+str[i];
i++;
}
b2=stod(s2);
}
else if(count==3) {
string s1,s2;
i=0;
while(str[i]!=' ') {
s1=s1+str[i];
i++;
}
a3=stod(s1);
i++;
while(str[i]!='') {
s2=s2+str[i];
i++;
}
b3=stod(s2);
}
count++;
}
in.close();//Close the file
  
double clubangle,clublength,clubspeed;
double clubangleRadian,g=10.733333;
clubangleRadian=getclubangle(a1,b1,clubNumber);
clublength=getclublength(a2,b2,clubNumber);
clubspeed=getclubspeed(a3,b3,swing,clublength);
  
  
double actualDistance=getactualDistance(clubspeed,clubangleRadian,g);//Find actual distance
int roundedDistance=round(actualDistance);//Round the calculated distance
  
int error=abs(distance-roundedDistance);
if((2*error)>depth) result="No";
else result="Yes";
  
cout<<" ";
cout<<" club swing distance(yds) error(yds) hit green?"<<endl;
cout<<" "<<clubNumber<<" "<<swing<<" "<<roundedDistance<<" "<<error<<" "<<result<<endl;
}  
return 0;
}