Homework 2: Sphere Volume
Concepts
Given an example code listing, modify it as an exercise in working with variables, operators, expressions and I/O.
Stating Facts to the Machine (declaring variables)
In your last assignment you learned about "teaching the computer" about constants. When we do that, it looks something like this:
If a program used that constant, could the number of friends ever change? Nope. How boring (and sad) is that?! Let's teach the computer about facts in the world that do change, or "vary." It's no surprise that we call these variables.
This is like saying, "Hey computer, there's an integer I want to tell you about called friends
and its initial value is zero."
Do you notice how similar variable declarations are to constant declarations? The only difference is that the const
keyword isn't used, and we use a lower-case name for the variable name (friends
instead of FRIENDS
).
Variable declaration vs. assignment vs. initialization
As stated in class, you will learn very quickly that the computer is powerful, but it is dumb and extremely picky. In addition, you'll find that in software engineering (and all engineering, really) we are very specific about our terminology. You will also find that terminology is often up for debate, often resulting in dramatic and somewhat nerdy discussions. The three terms you need to be aware of are declaration, assignment and initialization and we want you to adhere to the following meanings (we can nerd it out later if you disagree).
Let's take a look at that variable declaration from above again:
There's actually two things happening in that line of code above: variable declaration and assignment. When you do this in one line of code, we will call that initialization. Sometimes we might instruct you to "declare and initialize an integer called friends
with the value 3" and in those cases we really just mean initialize.
So, what does it mean to just declare a variable? That means you just declare it, but you don't give it an initial value.
That's declaration. It's like saying "Computer, be aware of an integer called friends, mmmkay?" and that's it. We've declared what the thing called friends
is -- it's an integer -- but we haven't told the computer what that integer's value is yet. It's a little like meeting someone for the first time: you know that the person has a name, but you don't know what their name is until they tell you.
What about assignment? We use the term assignment when you "give" an already declared variable a value.
friends = 0; // assignment
friends = 2; // yay
How not to declare variables
Every time you want to tell the computer about a variable, you have to tell it what kind of variable it is as well as the variable's name. Like this:
int enemies = 0;
If you declare a variable, you have to provide the datatype and the name. You should have learned about common datatypes in class and from your reading, so we won't talk about that here. However, here are some bad examples:
rapper lilBowWow; // Uh, I don't know what a rapper is*
int enemies = ; // Is equal to what? Puppies? Oranges?
int = 3; // Uh, what is the integer's name?
* Later we will show you how to teach the computer about your own datatypes, like cars, planets and even rappers.
So, you can declare variables and optionally initialize them with a value, but you must always provide the datatype and the name. Always like this:
double volume = 8.675309;
char middleInitial = 'Y';
One last thing. Do you know why we have to tell the computer the datatype of the variable? It's because when you declare a variable of a particular type, the computer allocates (sets aside or reserves) some memory to eventually store the value of that variable. If you tell the computer, "Computer, here is an integer we will call friends," and then you try to assign a cat or a motorcycle to that integer variable, it won't "fit." (We'll show you later how to convert or "cast" datatypes, but for now, just believe us: you can't store a cat or a motorcycle into a integer variable. It hurts.)
For this assignment, we want you to focus on variable declarations, initializations and assignments. But wait! What about operators and that cin
and cout
stuff? We'll discuss that in the next assignment. We think you'll be able to figure out what to do since we're providing you some code to start with. So, let's dive in!
Instructions
Open the solution (02_spehereVolume.sln
) file in apps\homework\02_sphereVolume
. If you are still unsure of the steps, look here for guidance.
Hit Ctrl-F5
to "build and run" the provided program. It should compile and run without error. (If it doesn't, ask for assistance.)
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. (Get in the habit of doing this -- it's exactly like putting your name on traditional paper homework, or signing a painting.)
In class, your instructor demonstrated a sample program that accepts user input used to calculate the volume of a square prism (a cube). Now it's your turn. Implement a program that prompts the user for the radius of a sphere, accepts the user's input, and outputs the volume of the sphere. An interaction example follows:
Enter the radius: 3
The volume of the sphere is 113.097 units cubed. Have a nice day!
Hint: Try using the values 4 and 3 to calculate 4 / 3
in your volume expression. Do you get the right volume? Try 4.0 and 3.0 -- did you get the right volume? Hmmm, why is there a difference? We'll explain "why" in the next class. Oh the suspense!
Requirements and Rubric
At minimum, your program must print the exact prompt
and print the exact output
Where V is a numeric value equal to 4/3π r3.
Anything else your program "says" is up to you. Be nice.
This work is worth 16 points.
Requirement | Points | Notes |
---|---|---|
Place your name in the comment header in main.cpp | 1 | |
Constant used for PI | 2 | |
4.0 and 3.0 used (not 4 and 3) to calculate 4/3 | 2 | |
Correct datatypes used (double ) |
2 | |
Correct input prompt | 1 | |
Correct output prompt | 1 | |
Comment that declares volume formula | 2 | |
Correctly calculates volume of sphere | 4 | |
Correct submission of src directory as a .zip file. | 1 |
Concepts Exercised: talking to the machine, declaring facts, I/O, expressions
© 2011 Yong Joseph Bakos.