C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 29th, 2005, 02:43 AM
yumaslim yumaslim is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 3 yumaslim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 54 sec
Reputation Power: 0
No idea what I'm doing

I have to write a program in C that computes the duration of a projectile's flight and it's height above the ground when it reaches the target.
Problem constant
G 32.17 (gravitational constant)
Problem Inputs
double theta (input angle)
double distance (distance to target)
double velocity (projectile velocity)
Problem outputs
double time (time of flight)
double height (height at impact)
relevant formulas
time=distance/ velocity x cos(theta)
height= velocity x sin(theta) x time - (g x time^2)/2

That is all of the info given in the book. I have no idea what to do with this info. I could really use some help with this. I know what all of the inputs and equations mean, I just don't know how to program.

Reply With Quote
  #2  
Old September 29th, 2005, 03:06 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Seems rather simple to me.
Use the time formula to determine how long it takes the projectile to get to the target, then use the time value in the height formula to determine what's it's height at the moment of inpact.

This is all basic mathematics/physics.
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Reply With Quote
  #3  
Old September 29th, 2005, 03:39 AM
yumaslim yumaslim is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 3 yumaslim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 54 sec
Reputation Power: 0
I understand the physics of it and could do it by hand, but I'm doing this for a programming class. It's supposed to be a beginner class, but I think I'm the only one that has never programmed before. I really need help with how to write the program.

Reply With Quote
  #4  
Old September 29th, 2005, 05:59 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Ok, here's a program that does what you want.
I've tried to explain as much as possible what I'm doing, try to learn something from it.

cpp Code:
Original - cpp Code
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define PI 3.14159265
  4. #define G 32.17
  5. // The above are the libraries I've used, (stdio for basic screen input/output, and math for sine, cosine and power functions)
  6. // below them are 2 constants, Pi (to convert degrees to radians) and G (gravity, what planet is this?)
  7.  
  8. // functions to calculate everything
  9. double get_time(double distance, double velocity, double theta)
  10. {
  11.     // doing the calculations is rather simple, just use normal semantics
  12.     // note that the cos (and sin) functions expect radians, not degrees, so they have to be converted (360 degrees = 2 Pi radians)
  13.     return (distance / (velocity * cos(theta * PI / 180)));
  14. }
  15.  
  16. double get_height(double velocity, double theta, double time)
  17. {
  18.     // note the fact that to-the-power-of is a function!
  19.     return ( velocity * sin(theta * PI / 180) * time - (G * pow(time, 2) / 2));
  20. }
  21.  
  22. int main(void)
  23. {
  24.     // declarations for all the variables I'm going to use.
  25.     // I'm using doubles, which is a large size floating point value.
  26.     double theta, distance, velocity, time, height;
  27.  
  28.     // get inputs
  29.     printf("Give theta (input angle): ");      // printf puts something on the screen
  30.     scanf("%lf", &theta);            // scanf reads user input, in this case a double (%lf).
  31.     printf("Give distance to target: ");        // see also the comments later on.
  32.     scanf("%lf", &distance);
  33.     printf("Give projectile velocity: ");
  34.     scanf("%lf", &velocity);
  35.  
  36.     // calculate results
  37.     time = get_time(distance, velocity, theta)// these are the functions I defined above
  38.     height = get_height(velocity, theta, time);
  39.  
  40.     // output results
  41.     printf("Time of impact is %.2lf\n", time)// these printf's contain a parameter (marked by the %-sign) that is replaced by the variable specified later, in this case time.
  42.     printf("Height of impact is %.2lf\n", height)// you can include any number of these parameters, as long as you supply a value for it to be replaced with.
  43.                             // in this case, I've used %lf, which tells the computer to expect a long float (double). If I wanted an integer, I would have used %d, %s for a string.
  44.                             // Use a book or google to find a complete list.
  45.                             // the .2 between the %-sign and the lf means it will round the value at 2 decimals.
  46.  
  47.     // exit
  48.     return 0;      // 0 indicates success, anything else is an error value.
  49. }
Comments on this post
MichaelSoft agrees: Are you a teacher yourself? ;-)
Icon agrees: I wish the code I have to use/review had so many lines of comment, good coding practice
B-Con agrees!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > No idea what I'm doing


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT