Help with free fall code (distance, velocity, etc.)

The goal here is to write a code that will calculate whether or not a water balloon can be dropped on someone depending on how far away they are, how fast they are walking, and comparing how long it will take them to reach the hit box vs how long for the balloon to drop.
An example output is meant to look like this:
How far away is your friend(feet)? 200
How fast are they walking(feet/sec)? 3
How high are you before dropping your ballon(feet)? 100
It will take 66.66 seconds for them to reach the balloon point
It will take 4.57 seconds for your balloon to travel to the ground
If you wait 62.09 seconds, you will hit them

This is all I have so far. I know it's not much, but I'm going to continue working on it and I'd just like some tips for how best to proceed so I don't waste time. Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>

using namespace std;

int main(){
  int velocity = 0;
  int distance = 0;
  int verDist;
  int speed;
  int horDist;
  int time1;
  cout <<"How far away is your friend(feet)?: ";
  cin >> verDist;
  cout <<"How fast is your friend walking(feet/sec)?: ";
  cin >> speed;
  cout <<"How high are you dropping the balloon from(feet)?: ";
  cin >> horDist;
}
Well, my first suggestion would be to work out the necessary physics equations. It's simple mechanics stuff, so that shouldn't be too difficult. Once that's done you simply put the equations into code to get the result.

You might need to #include <cmath> though for things such as the square root, for deriving things such as time.
I've been working on this code for a little while now and this is what I have. The current problem is it's not displaying my output statements at the end? So I don't know if the math is even right.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cmath>
#include <stdio.h>

using namespace std;

int main(){
  float velocity = 0;
  float timeElapsed = 0;
  float distance;
  float height;
  float walkSpeed;
  cout <<"How far away is your friend(feet)?: ";
  cin >> distance;
  cout <<"How fast is your friend walking(feet/sec)?: ";
  cin >> walkSpeed;
  cout <<"How high are you dropping the balloon from(feet)?: ";
  cin >> height;
  while(height > 0){
    timeElapsed += 0.001;
    height -= velocity/1000;
    velocity += 0.032 - velocity * 0.0012;
  }
  return timeElapsed;
  float timeToWalk = distance / walkSpeed;
  float waitTime = timeToWalk - timeElapsed;
  if(waitTime > 0.0){
    printf ("It will take %f.2 seconds for them to reach the balloon point.", timeToWalk);
    printf ("It will take %f.2 seconds for your balloon to travel to the ground.", timeElapsed);
    printf ("If you wait %f.2 seconds, you will hit them.", waitTime);
  }else
    printf ("It will take %f.2 seconds for them to reach the balloon point.", timeToWalk);
    printf ("It will take %f.2 seconds for your balloon to travel to the ground.", timeElapsed);
    printf ("If you wait %f.2 seconds, you will hit them.", waitTime);
    cout << "It is too late to drop your balloon";
  }

it's not displaying my output statements at the end
Line 24: return timeElapsed;
Returning in main exits the program.
Ah, I didn't know that before! Here is the final code I got; everything appears to be working now :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cmath>
#include <stdio.h>

using namespace std;

int main(){
  float velocity = 0;
  float timeElapsed = 0;
  float distance;
  float height;
  float walkSpeed;
  cout <<"How far away is your friend(feet)?: ";
  cin >> distance;
  cout <<"How fast is your friend walking(feet/sec)?: ";
  cin >> walkSpeed;
  cout <<"How high are you dropping the balloon from(feet)?: ";
  cin >> height;
  while(height > 0){
    timeElapsed += 0.001;
    height -= velocity/1000.0;
    velocity += 0.032 - velocity * 0.0012;
  }
  float timeToWalk = distance / walkSpeed;
  float waitTime = timeToWalk - timeElapsed;
  if(waitTime > 0.0){
    printf ("\nIt will take %.2f seconds for them to reach the balloon point.", timeToWalk);
    printf ("\nIt will take %.2f seconds for your balloon to travel to the ground.", timeElapsed);
    printf ("\nIf you wait %.2f seconds, you will hit them.\n", waitTime);
  }else{
    printf ("\nIt will take %.2f seconds for them to reach the balloon point.", timeToWalk);
    printf ("\nIt will take %.2f seconds for your balloon to travel to the ground.", timeElapsed);
    cout << "\nIt is too late to drop your balloon.\n";
  }
  }
Btw you're mixing C++ and C (printf) syntax. Put

cout << fixed << setprecision(2);

somewhere at the beginning of your main function and include iomanip.

Then you can print numbers with two decimals using cout:

cout << "It will take " << timeToWalk << " seconds for them to reach the balloon point." << endl;
Last edited on
My opinion is that you should develop the algorithm first, then write the code.
Topic archived. No new replies allowed.