# Simple Counting Program

Page 1 of 1

## 12 Replies - 2918 Views - Last Post: 15 January 2010 - 11:54 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=149840&amp;s=5fd8563529aedfe02c9b8a2f1e39c80d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 senseiz

Reputation: 0
• Posts: 5
• Joined: 14-January 10

# Simple Counting Program

Posted 14 January 2010 - 01:10 AM

Hello all I am very new at this and trying to work some stuff out, I am not asking for code just some hints on what I am doing wrong, and maybe where I should start focusing my attention to get better at all this. I am in my second C++ class and I should be able to do this type of stuff immediately, but I cant, so I really want to study and practice to get good and comfortable.

So here is my application for class, it is not due for a while, but I got started on it. It compiles without error it just takes the input and makes the first test output. The cout text output is just there for testing purposes, it should be replaced later with the graphical aspect of the data output.

Although the instructions are talking about making graphics to represent it, I want to get what is "under the hood" working first, then work on the graphic output.

Instructions:

Idea:
Plane is moving at a speed set by user input (in feet per second).
Application simulates a person jumping out. Falling at a rate of 32ft/sec.
The freefall time (in seconds) is inputted by the user, as well as the height of the plane (in feet).

Constants:

Deceleration factor - the rate at which the parachutist will decelerate in the vertical direction after the chute opens. It is typically very high - 100 ft/secsec. Until the chute opens, the parachutist's velocity increases by 32 ft/secsec. However, terminal velocity is reached at 174/ft/sec.

Drag Factor - This is the rate at which the parachutist will decelerate in the horizontal direction after the chute opens. It is typically 20-60 ft/secsec. Until the chute opens, we assume the chutist is moving at the speed of the airplane.

User inputted data:

speed of the plane (feet per second)
height of the plane (feet)
freefall time (seconds)

The simulation works by calculating the position of the parachutist each second between the time of the jump and landing on the ground. When the chutist jumps, the velocity increases (up to terminal) until the chute opens. You need to calculate the chutist' speed as well as horizontal and vertical position. When the chute opens, both the horizontal and vertical speeds decreases until the vertical speed is 17 ft/sec and the horizontal feed is 0 ft/sec.

The program should plot the descent of the parachutist using our simple graphics package. You can use a simple circle to represent the chutist. A single while loop is sufficient to handle the simulation. It would look something like this :

* each loop calculates the chutist's speed and position each second
* update chutist's speed
* update chutist's position
* indicate when the chute has opened - e.g. horizontal line.
* plot the chutist (every 3 or 4 seconds)

When the parachutist has landed, print out the chutist's landing velocity. This will indicate the success (or other) of the simulation.

EXTRA: Design a parachuteman using the graphics system and have him fall.

HERE IS MY CODE: Sorry if it is noobish, but I want to get better.

```
#include <iostream>
#include <cmath>
using namespace std;

int dropspeed = 0;
int planespeed = 0;
int freefalltime;
int time2;
int height;
int time=0;

int main()
{
cout << "Please enter the plane speed: " << endl;
cin >> planespeed;
cout << "Enter plane height: " << endl;
cin >> height;
cout << "Enter freefall time: " << endl;
cin >> freefalltime;

while (height >= 0)
{
while (time <= freefalltime)
{
while (dropspeed <= 174)
{

time2 = time;
dropspeed = dropspeed + 32;
height = height - dropspeed;
dropspeed = 174;
if (time%3==0 || time==0)
{
cout << "Plotting height = " << height << " || dropspeed = " << dropspeed << endl;
time++;
}
}

}

for (int time3 = time2;;)
{
while (dropspeed >= 17)
{
dropspeed = dropspeed - 100;
planespeed = planespeed - 20;
}
dropspeed = 17;
height = height - dropspeed;
time = time3;
if (time % 3 == 0 || time==0)
{

cout << "Plotting height = " << height << " || dropspeed = " << dropspeed << endl;
time3++;
}

}

}

}
```

I am kind of stuck here. It compiles but stops after the first output. I am sure I will figure it out, however I would appreciate if someone with much more experience than myself could give their input on what areas I should be studying and what I am doing wrong.

This post has been edited by senseiz: 14 January 2010 - 01:26 AM

Is This A Good Question/Topic? 0

## Replies To: Simple Counting Program

### #2 ice-t

Reputation: 7
• Posts: 104
• Joined: 26-May 09

## Re: Simple Counting Program

Posted 14 January 2010 - 01:34 AM

You are stuck in an infinite loop
```	 while (dropspeed <= 174)
{

time2 = time;
dropspeed = dropspeed + 32;
height = height - dropspeed;
dropspeed = 174;		  // HERE
if (time%3==0 || time==0)
{
cout << "Plotting height = " << height << " || dropspeed = " << dropspeed << endl;
time++;
}
}

```

You will always have dropspeed = 174 that while will go forever and only the 1st time u have time=0 so only 1 time the output appears.

### #3 senseiz

Reputation: 0
• Posts: 5
• Joined: 14-January 10

## Re: Simple Counting Program

Posted 14 January 2010 - 09:42 PM

Hi, thanks for the response.

Here is my modified program.

I am still working on it, please let me know of any other tips/info/guidelines.

```#include <iostream>
#include <cmath>
using namespace std;

int dropspeed = 0;
int planespeed = 0;
int freefalltime;
int height;
int time=0;

int main()
{
cout << "Please enter the plane speed (in ft/sec): " << endl;
cin >> planespeed;
cout << "Enter plane height (in ft): " << endl;
cin >> height;
while (height < 10000)
{cout << "Come on, do you really think a plane would be flying that low for a parachuter?" << endl << "Enter another number (10,000ft+): " << endl;
cin >>height;
}
cout << "Enter freefall time (in seconds): " << endl;
cin >> freefalltime;

while  (time <= freefalltime)
{
dropspeed = dropspeed + 32;

if (dropspeed >= 142)
{
dropspeed = 174;
cout << "Plotting height = " << height << " || dropspeed = " << dropspeed << endl;
height = height - dropspeed;
}

else
{

height = height - dropspeed;
cout << "Plotting height = " << height << " || dropspeed = " << dropspeed << endl;
}

time++;
if (height <= 0)
{cout << "You just slammed into the ground at " << dropspeed << "ft per second and you are dead" << endl << "Choose better numbers next time!" << endl;
return 0;}

else
{}
}

//Parachute opens now because freefall timer ran out and exits the above while loop

while  (height > 0)

{
if (dropspeed > 117)
{dropspeed = dropspeed - 100;}
else
{dropspeed = 17;}

if (planespeed >=21)
{
planespeed = planespeed - 20;
}
else {planespeed = 0;}
double check;
check = height-dropspeed;
if (0 >= check)
{height = 0;}
else
{
height = height - dropspeed;
}
cout << "Plotting height = " << height << " || dropspeed = " << dropspeed << endl << "driftspeed = " << planespeed <<endl;
}
}
```

This post has been edited by senseiz: 14 January 2010 - 09:45 PM

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Simple Counting Program

Posted 14 January 2010 - 10:08 PM

From where are the magic numbers of 17, 21, 117, 20, 32, 174, 142 coming? Do they represent something we don't know about? 32 is the approximate acceleration of a falling body in ft/s^2 if I remember my physics correctly.

### #5 senseiz

Reputation: 0
• Posts: 5
• Joined: 14-January 10

## Re: Simple Counting Program

Posted 15 January 2010 - 02:02 AM

JackOfAllTrades, on 14 Jan, 2010 - 09:08 PM, said:

From where are the magic numbers of 17, 21, 117, 20, 32, 174, 142 coming? Do they represent something we don't know about? 32 is the approximate acceleration of a falling body in ft/s^2 if I remember my physics correctly.

The instructions are posted in the 1st post.

### #6 Bench

• D.I.C Lover

Reputation: 944
• Posts: 2,464
• Joined: 20-August 07

## Re: Simple Counting Program

Posted 15 January 2010 - 05:35 AM

senseiz, on 15 Jan, 2010 - 09:02 AM, said:

JackOfAllTrades, on 14 Jan, 2010 - 09:08 PM, said:

From where are the magic numbers of 17, 21, 117, 20, 32, 174, 142 coming? Do they represent something we don't know about? 32 is the approximate acceleration of a falling body in ft/s^2 if I remember my physics correctly.

The instructions are posted in the 1st post.

I think you missed JackOfAllTrades' subtle hint which is Don't use 'magic numbers' - even if they are documented within your specification, they make code a bit more difficult to follow. Your assignment spec calls them constants, so you'd do well to treat them as such in your program too; You can even take some meaningful names from this:

Quote

Constants:

Deceleration factor - the rate at which the parachutist will decelerate in the vertical direction after the chute opens. It is typically very high - 100 ft/secsec. Until the chute opens, the parachutist's velocity increases by 32 ft/secsec. However, terminal velocity is reached at 174/ft/sec.
```int const terminal_velocity = 174;
int const acceleration = 32;
int const deceleration = 100;
```
using those constants within the logic of your program instead will make your code a little easier to understand for others reading it, and its a good practice to adopt as habit whenever you're dealing with 'global constants' in a problem.

This post has been edited by Bench: 15 January 2010 - 05:38 AM

### #7 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Simple Counting Program

Posted 15 January 2010 - 06:12 AM

My only suggestion would be that when you do declare your constants that you follow the common (not universal but nearly so) convention of giving your constant variables all uppercase names. This makes it obvious to anyone reading the code that they are constants and they shouldn't write any code that changes them.

Example:
```int const terminal_velocity = 174;
int const acceleration = 32;
int const deceleration = 100;

```

I would suggest this:
```int const TERMINAL_VELOCITY = 174;
int const ACCELERATION      = 32;
int const DECLERATION       = 100;

```
as a slight tweak to make your code just that little bit easier to write, read and maintain.

http://geosoft.no/de...g%20Conventions
http://en.wikibooks....Style#Constants

### #8 Bench

• D.I.C Lover

Reputation: 944
• Posts: 2,464
• Joined: 20-August 07

## Re: Simple Counting Program

Posted 15 January 2010 - 07:09 AM

janotte, on 15 Jan, 2010 - 01:12 PM, said:

I would suggest this:
```int const TERMINAL_VELOCITY = 174;
int const ACCELERATION      = 32;
int const DECLERATION       = 100;

```
as a slight tweak to make your code just that little bit easier to write, read and maintain.

That's a style issue, and personally I think using all-caps letters in variable names is a bit misleading (regardless of whether its a const variable or not), I usually expect ALLCAPS to be reserved for preprocessor names, i.e. #define statements and macros to make them stand out as something which is potentially unsafe - and the programmer needs to know because the compiler may or may not warn about potential problems. on the other hand, const variables are usually pretty safe. but whatever, YMMV.

edit I've just read the wikibooks link - I find the advice to be quite surprising and unusual for C++ (but not for C); even the standard libraries use lowercase names for const variables and enums; eg. std::string::npos and std::ctype<CharT>::space. I can't say that i've ever encountered much C++ code or libraries where upper case letters have been used for const variables. I think its one of those old styles which stems from the C language and a large number of legacy or C libraries, along with hungarian notation etc.

This post has been edited by Bench: 15 January 2010 - 07:28 AM

### #9 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Simple Counting Program

Posted 15 January 2010 - 07:21 AM

Yep definitely a good point that has its supporters.

Anyone reading along should read the second link in my posting for more support of Bench's position.

### #10 spintronic

Reputation: -32
• Posts: 94
• Joined: 25-December 09

## Re: Simple Counting Program

Posted 15 January 2010 - 07:22 AM

senseiz, on 15 Jan, 2010 - 01:02 AM, said:

JackOfAllTrades, on 14 Jan, 2010 - 09:08 PM, said:

From where are the magic numbers of 17, 21, 117, 20, 32, 174, 142 coming? Do they represent something we don't know about? 32 is the approximate acceleration of a falling body in ft/s^2 if I remember my physics correctly.

The instructions are posted in the 1st post.

I think their wrong.

accelleration of freefall = g = 9.8m/s^2

final velocity = -gt+v_0 (Where v_0 = initial velocity = 0,t=time in seconds)

(Simply convet to feet)

According to wiki;

This is the "textbook" case of the vertical motion of an object
falling a small distance close to the surface of a planet. It is
a good approximation in air as long as the force of gravity on
the object is much greater than the force of air resistance, or
equivalentlty the object's velocity is always much less than the
terminal velocity (see below).

v(t) = -gt+v_0
y(t) = -(1/2)gt^2+y_0

where
v_0 is the initial velocity (m/s).
v(t)is the vertical velocity with respect to time (m/s).
y_0 is the initial altitude (m).
y(t) is the altitude with respect to time (m).
t is time elapsed (s).
g is the acceleration due to gravity (9.81 m/s2 near the surface of the earth).

Simply apply the opposing force of the chute and recalculate the decelleration.

### #11 Bench

• D.I.C Lover

Reputation: 944
• Posts: 2,464
• Joined: 20-August 07

## Re: Simple Counting Program

Posted 15 January 2010 - 07:36 AM

spintronic, on 15 Jan, 2010 - 02:22 PM, said:

senseiz, on 15 Jan, 2010 - 01:02 AM, said:

JackOfAllTrades, on 14 Jan, 2010 - 09:08 PM, said:

From where are the magic numbers of 17, 21, 117, 20, 32, 174, 142 coming? Do they represent something we don't know about? 32 is the approximate acceleration of a falling body in ft/s^2 if I remember my physics correctly.

The instructions are posted in the 1st post.

I think their wrong.

accelleration of freefall = g = 9.8m/s^2
Look at the measurements of units being used. 9.8ms-2 translates almost exactly to 32fts-2
9.8 * 3.28 = 32.144

This post has been edited by Bench: 15 January 2010 - 07:38 AM

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Simple Counting Program

Posted 15 January 2010 - 08:11 AM

We here in the US, where the OP *may* be from, are still sometimes stuck outside of the metric system (although in all my university science classes we used metric).

### #13 spintronic

Reputation: -32
• Posts: 94
• Joined: 25-December 09

## Re: Simple Counting Program

Posted 15 January 2010 - 11:54 AM

Bench, on 15 Jan, 2010 - 06:36 AM, said:

spintronic, on 15 Jan, 2010 - 02:22 PM, said:

senseiz, on 15 Jan, 2010 - 01:02 AM, said:

JackOfAllTrades, on 14 Jan, 2010 - 09:08 PM, said:

From where are the magic numbers of 17, 21, 117, 20, 32, 174, 142 coming? Do they represent something we don't know about? 32 is the approximate acceleration of a falling body in ft/s^2 if I remember my physics correctly.

The instructions are posted in the 1st post.

I think their wrong.

accelleration of freefall = g = 9.8m/s^2
Look at the measurements of units being used. 9.8ms-2 translates almost exactly to 32fts-2
9.8 * 3.28 = 32.144

Yes, I already gathered that. But my comment "I think their wrong" was regarding the "instructions" for the assignment.

E.G.

Quote

Falling at a rate of 32ft/sec.

That should be "32ft/sec^2"

Quote

Drag Factor-This is the rate at which the parachutist will decelerate in the horizontal direction after the chute opens. It is typically 20-60 ft/secsec.
Until the chute opens, we assume the chutist is moving at the speed of the airplane.

You won't have any horizontal velocity when the chute opens.

And as "JackOfAllTrades" states, even though the U.S is outside the metric system.

When it comes to physics the SI Units of "N.m., m/s, & s" should be universal, for the collage/university he is attending.

One last thing, the easiest way to tackle this is using vectors. ;-)