College C++ problem...

This is a discussion on College C++ problem... within the C++ Programming forums, part of the General Programming Boards category; Hello guys, I'm having a tricky assignment this week in C++, I have completed it and I'm wondering if the ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    College C++ problem...

    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:

    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.
    Okay now that you have seen the assignment, here is my program's source code:

    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;
    }
    My main question is, is this correct?

    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

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Couple suggestions.

    1. Get rid of the global variables. Use the return values rather than directly using the global. Rather than

    Code:
    calculateDistance(uDistance); // modifies global total
    
    if (total <= 0.1) // etc
    Make a local variable total inside that block.

    Code:
    double total = calculateDistance(uDistance);
    
    if (total <= 0.1)
    2. Your calculateDistance function is actually returning a ratio... it's not doing what it's advertising.

    3. Don't use void main.

    http://homepages.tesco.net/~J.deBoyn...void-main.html

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    Hello,

    Thank you very much for looking over my program. Implementing total as a local variable makes sense. I've also done int main() as everyone recommends to do that here (hard to break habit lol)

    Having said that I'm trying to understand what you mean by what you stated in #2. You stated that the function was returning a ratio. Do you mean distance/uDistance?

    I'm attempting to fulfill the following requirement:

    the user first enters the distance to a target.... 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.
    I have set the user's input for distance as uDistance. How can I calculate when the projectile comes within .1% of the distance to the target? distance/uDistance right?

    Thank you very much again for your help, its very very appreciated.

    Traz

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    This is what you need for less than .1% error.

    Code:
    if (abs(1.0 - distance/uDistance) < .001) { // user entered good enough values
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Awkward problem in learning c++
    By TheUnknownFacto in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2007, 01:43 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21