In this assignment, you are asked to develop a computer program for launching a
ID: 3704874 • Letter: I
Question
In this assignment, you are asked to develop a computer program for launching a projectile ata target. The target will appear somewhere between 1000 feet to 1500 feet away from the launch site Your program will prompt the user to enter the angle and the velocity for launching the projectile. If, according to the user's inputs (velocity and angle), the shot comes within 10% of the distance to the target, the player wins the game. If the projectile does not come close enough, your program should tell the player: how far off the projectile is, whether it is too short or too far, and the player should be allowed to try again If there is not a winning input after five tries, then the player loses the game After one game is over, your program should ask if the player wants to play again. If he/she does, quit. dourn st wt new target dCinue the m un the layer wants toExplanation / Answer
#include <bits/stdc++.h>
using namespace std;
void welcome (void){ //for wlcome note
cout<< "Welcome to the game!!!"<<endl;
cout<<"here are the rules"<<endl<<"1. you have to enter angle and velocity of projectile for a given target distance. "<<endl<<"2. You will be give five chances if you guess it correct you ll win otherwise loose "<<endl;
}
int target(void){ //for determining the target distance using rand()
srand(time(0));
int distance = rand() % (1500 - 1000) + 1000;
cout<<"Your target is "<<distance<<" feet away."<<endl;
return distance;
}
std::pair<double,double> input (void){ //for input and checking its validity
std::pair<double,double> p;
cout<<"enter angle and velocity"<<endl;
cin>>p.first>>p.second;
if(p.first>0 && p.second>0)
cout<<"Congrat!!! input is a valid number."<<endl;
else {
cout<<"Oops!!! input is an invalid number."<<endl<<"enter values again"<<endl;
cin>>p.first>>p.second;
}
return p;
}
double convert(double a){ //for coverting degree into radian
double b;
b=(a*3.1415926)/180.0;
return b;
}
double calculate(double angle, double velocity ){ //for calculating guessed distance
double ans;
ans=(velocity*velocity*sin(2*angle))/32.2;
return ans;
}
bool check(double dist1 , double dist2){ //for checking the guessing distance is within 10% error
int high,low;
high=dist2+(dist2*0.1);
low= dist2-(dist2*0.1);
if(dist1>=low && dist1<=high)
return true;
else
return false;
}
int main() // main driver function
{
welcome(); //calling welcome() function
for(int i=0;i<5;i++){ //loop for 5 attempts
int dist=target();
std::pair<double,double> p = input(); //calling input function
double rad=convert(p.first); // calling convert function
double result=calculate(rad,p.second); //calling calculate function
if( check(result,dist)==1 ){ //checking player's closeness
cout<<"YOU WON!!!";
break;
}
else{
if(result>(dist+(dist*0.1))){
cout<<(result-dist)<<" feet far"<<endl<<"too far"<<endl;
cout<<"try again"<<endl;
}
else if(result<(dist-(dist*0.1))){
cout<<(dist-result)<<" feet short"<<endl<<"too short"<<endl;
cout<<"try again"<<endl;
}
if(i==4)
cout<<"you lost!!!"<<endl;
}
}
return 0;
} //end of program
thank you!!
please do upvote :)