Homework 3: UDF-Powered Aircraft Velocity and Acceleration
This assignment is designed to exercise your understanding of operators and expressions.
Concepts
This assignment is drawn from your textbook but is provided here. Be sure to have read that first.
Operators & Expressions
Stating facts about the world is nice, but if that's all that could be done with a computer we would all fall asleep. It's no secret that computers are used to compute or "calculate" things: by combining variables with operators, we create expressions.
Programming operators is actually a deep topic, but since we're focused mainly on numeric datatypes, the rule is very simple: numeric operators in C++ are very similar to what you already know about algebra. This is a simplification, but as a rule of thumb, operators in programming work just like they do in algebra:
int fingers = 5 / 1; // fingers is 5
int cookies = age * fingers; // cookies is 105
cookies = age - fingers; // now cookies is 16
You should also be aware of how parenthesis are used to enforce operator precedence in complex expressions. Again, similar to algebra:
Now, there is a special phrase we'd like you to remember: expressions are evaluated and yield a value. It's so important we'd like you to read this aloud: expressions are evaluated and yield a value, expressions are evaluated and yield a value, expressions are evaluated and yield a value. What the heck does that mean? It means that when you are reading a program, every time you see an expression you should imagine the value of that expression in place of the actual expression. This is about teaching you a way of thinking that is important to programming. Let's look at an example:
The computer sees the expression 17 + 4
, evaluates (simplifies or calculates) it, and yields a value. What value? In this case, 21. If you imagine that the value 21 replaces 17 + 4
, the above line is similar to saying:
Let's take a look at another example using variable in the expression:
int fingers = 5;
int cookies = age * fingers;
So, age
is 21 and fingers
is 5. What is the value of cookies
? It's age * fingers
. What is the value of age
? What is the value of fingers
? What is age * fingers
? The expression age * fingers
gets evaluated and yields the value 105. That value is then assigned to cookies
.
Expressions are evaluated and yield a value.
There, we've written it really huge. This idea is important.
We think you'll get the hang of this pretty quickly -- it just takes practice. But, there are two more issues you should be aware of: performing operations with different datatypes, and thinking procedurally.
Operations with different types
If we asked you, "What is four divided by three?" you would say "one and a third" or "one point three" or "go away!" And you'd be right, because your mind already converts the integers into floating point numbers. The computer, however, is not so smart:
answer = 4 / 3; // answer is 1. Huh!?
That's right: answer
is equal to 1. Why? There are two reasons. The first reason is that we told the computer that answer
is an integer, so even if we explicitly assigned 1.3 to answer, answer would still equal 1.
Since answer has an int
datatype, the computer truncates (ignores) everything to the right of the decimal point.
The second reason, and the real reason why the first example above causes 1 to be assigned to answer
, is because expressions using one datatype always yield a value of that same datatype. In other words, an int divided by an int yields an int. A double divided by a double yields a double. Let's take another look:
answer = 4 / 3;
double volume_coefficient;
volume_coefficient = 4.0 / 3.0;
What is the value of the expression 4 / 3
? The value is 1, because an int
divided by an int
yields an int
. What is the value of the expression 4.0 / 3.0
? The value is 1.3, because a double
divided by a double
is a double
.
See, we told you computers were very picky. Good thing you never have to date one, it would be a very tumultuous relationship. Now, given the two cases above, we will leave you with the following mind-bender.
cats = 4.0 / 3.0;
double dogs;
dogs = 4 / 3;
int hotdogs;
hotdogs = 4.0 / 3;
double hotcats;
hotcats = 4.0 / 3;
What are the values of cats, dogs, hotdogs and catdogs? Why?
Programs are procedural, so think procedurally
There's one more important thing to understand about expressions and programs. Computer programs in C++ are procedural. That is, you should imagine that the computer reads the program one line at a time and carries out each statement. Let us start with a common misunderstanding:
int width;
int height;
int volume = base * width * height;
base = 2;
width = 2;
height = 2;
cout << volume;
What gets printed to the screen? Hmmm, what is volume? You might think, "Dude, it's base times width times height, which is eight" but you would be partially wrong. Remember, expressions get evaluated and yield a value. At the fourth line above, what is base * width * height
? We don't know, because no values have been assigned to those variables yet. So some unknown number gets assigned to volume
, then we assign values to base
, width
and height
, then we print volume
.
C++ is not a declarative language. You can't declare variables as if they were "formulas" or "functions" and expect them to be calclated later. Furthermore, the computer reads and evaluates your code just like you might read a book, one line at a time. It's not smart enough to go back and say "Oh, I know what you really mean, you wanted me to wait to calculate volume until after some other variables were assigned some values."
The key lesson here is to understand that C++ programs are procedural, and as soon as an expression is read, it gets evaluated and yields a value. Again, what happens on the fourth line above? volume
is assigned the value of whatever base * width * height
is. What is base * width * height
as of line 4? It's unknown. Hence, volume
is assigned an unknown value.
Here's an example that would work:
int width;
int height;
base = 2;
width = 2;
height = 2;
int volume = base * width * height;
cout << volume;
Ahhh, much better. The expression in the seventh line gets evaluated and yields 8, which is then assigned to volume
.
Just so you know, there are declarative programming languages out there. C++ is not one of them, although the nerdiest of nerds might argue with that because there are some declarative aspects. Regardless, in a few weeks we'll be learning about something really cool, functions, which will give you some tremendous declarative-like power and make your programs closer to English rather than computerese.
Instructions
Open the solution file in apps\homework\03_aircraft
.
Hit Ctrl-F5
to "build and run" the provided program. It should compile and run without error. (If it doesn't, ask for assistance.) Furthermore, the input and output should match the example at the bottom of page 81 in your textbook.
Using the project explorer (the left panel of the Visual Studio interface), open the file called main.cpp
.
Update the comment header in main.cpp
with your full name. Just so you know, this is the last time we'll remind you to do this but it's expected of you in every assignment.
You are provided a code listing similar to what is displayed on page 81 of your textbook. Your goal is to modify this program in two steps:
- Modify the program so that the input values are entered in minutes instead of seconds. Remember that the equations will still assume that the time values are in seconds (so what else has to change?).
- Modify the program so that the output values are printed in feet per second (for velocity) and feet per second^2 (for acceleration). Recall that 1 meter = 39.37 inches.
An interaction example of the modified program follows:
Enter the new time value in minutes: 1
Velocity = 693.654 ft/second
Acceleration = 0.750 ft/second^2
There is one major stylistic problem with this program: it contains "magic numbers" that exist inline as part of the velocity and acceleration calculations. Your refactored program should extract these "magic numbers" into declared constants. Since we're not aircraft experts, and the textbook does not specify what these numeric coefficients are, we have to improvise. Choose meaningful constant names that are succinct.
Note that the listing differs from what is in the book. This is intentional.
Notice how pow()
is used.
Notice how the output is formatted with setprecision()
to a fixed decimal point at 3 decimal places.
Just notice those things, don't worry about them or memorize them.
Hints
- Read the program first, and make sure you understand what is happening. Consult your lecture notes and the textbook.
- Convert the time units first and run the program. Is the output still correct? If so, move on to the next modification.
- Conversion constants are, yes, constant. How should you declare these?
- Note that 1 m ~= 3.280839 ft
Requirements and Rubric
At minimum, your program must print the exact prompt
and print the exact output
Acceleration = A ft/second^2
Where V is the calculated velocity and A is the calculated acceleration.
Anything else your program "says" is up to you.
This work is worth 22 points.
Requirement | Points | Notes |
---|---|---|
Place your name in the comment header in main.cpp | 1 | |
Constants used for all formula coefficients | 2 | |
Constants used for conversion values | 2 | |
Correct datatypes used (doubles!) | 4 | |
Correct input prompt | 1 | |
Correct output prompt | 1 | |
Comment that declares velocity and acceleration formulae | 2 | |
Correctly calculates velocity and acceleration in ft/s and ft/s^2 | 8 | |
Correct submission of src directory as a .zip file. | 1 |
Concepts Exercised: operators, expressions, variables, constants, I/O
© 2011 Yong Joseph Bakos.