Hello guys,
I'm having a tricky assignment this week in C++, I have completed it and I'm wondering if the code is correct or not. Anyway here is the problem, bear with me this is extensive:
Okay now that you have seen the assignment, here is my program's source code:The distance to a landing point of a projectile, launched at an angle ang (in radians) with an initial velocity of vel (in feet per second), ignoring air resistance, is given by the formula
Distance= (vel 2 * sin(2*ang))/32.2
Write a C++ program that implements a game in which the user first enters the distance to a target. The user then enters the angle and velocity for launching a projectile. If the projectile comes within 0.1% of the distance to the target, the user wins the game. If the projectile doesn’t come close enough, the user is told how far off the projectile is and is allowed to try again. If there isn’t a winning input after five tries, the user loses the game.
To simply the input for the user, your program should allow the angle to be entered in degrees. The formula for converting degrees to radians is
Radians=(degrees*3.14159)/180
Each of the formulas in this problem must be implemented as a C++ value returning free function. Your program should prompt the user for input appropriately and label the output values.
NOTE: A function to calculate the sin of an angle in radians is in the include file math.h.
My main question is, is this correct?Code:#include <iostream.h> #include <math.h> // Declare objects in global scope to be used by all functions, vel (velocity) ang (degress) // and total to hold calculation of distance being less than or greater than 0.1 double vel, ang, total; // Constructor for function playGame void playGame(double uDistance); // Constructor for function convertRadians double convertRadians(double& radians); // Constructor for function calculateDistance double calculateDistance(double uDistance); // Begin program with main() statement of void type void main() { // Declare a uDistance variable for user defined distance double uDistance; char prompt; // Do the following steps do { // Introduce user to program cout << "************* Projectile Game *************" << endl; cout << "Welcome to the projectile game! Please enter a distance--"; // Get input from user for uDistance cin >> uDistance; // Play the game, use function playGame playGame(uDistance); // Ask user if they would want to play again cout << "Would you like to play again? Enter Y for yes, any other character otherwise." << endl; cin >> prompt; cout << endl; cout << endl; } while (prompt == 'Y' || prompt == 'y'); cin.get(); } void playGame(double uDistance) { // Begin for for (int tries = 1; tries <= 5; tries++) { // Ask user for angle input cout << "Please enter an angle--"; cin >> ang; // Convert angle value to radians convertRadians(ang); cout << "Please enter a velocity--"; cin >> vel; // Call upon calculateDistance to find if user wins or not calculateDistance(uDistance); if (total <= 0.1) { cout << "You won the game!" << endl; cout << "You came within " << total << " of the target! Congratulations!" << endl; break; // Break out of the for loop } else { cout << "You didn't win the game, please try again." << endl; cout << "You came within " << total << " of the target." << endl; cout << endl; } // End if } // End for } double convertRadians(double& radians) { radians = ((ang*3.14159)/180); return radians; } double calculateDistance(double uDistance) { double distance; double sinTotal = (2 * ang); distance = (((vel*vel) * sin(sinTotal)/32.2)); total = distance/uDistance; return total; }
I'm having some trouble getting ahold of the professor to check this out with him.
If I'm not correct, am I on track? I get really really strange output for some of my distance/angle/velocity combinations.
Thank you for your assistance.
Traz