Homework 1: Hello World!
Concepts
Focus on one main programming concept in this assignment: stating "facts that don't change" to the machine.
Stating Facts to the Machine (declaring constants)
Think about yourself for a moment. What do you know about the world? From the moment you wake up until the moment you go to sleep, your mind receives a variety of input and you form a certain perspective about the world. You are aware of certain facts, such as your pet's name or the current time. Some of these facts change and some don't.
Imagine you're standing on top of a building with a bucket of water balloons. When you drop a water balloon from a building, you witness that water balloon accelerate toward the ground according to the laws of gravity (aka free fall). Being a totally genius Mines student, you know that acceleration of free fall on Earth is approximately 9.8 m/s2. This value is "constant" -- it will never change (at least, as long as you're on Earth). Hence, we call these sorts of things constants.
If we were to simulate dropping water balloons as a computer program, it should be obvious that we would need to somehow inform the machine that the acceleration of the balloons is about 10 m/s2. How can we define facts about the world, that do not change, to the machine? We "declare constants."
This is like telling the computer, "Hey computer, there's something I'd like you to know about. It's called FREE_FALL_ACCELERATION, which is an integer and is equal to ten. Mmmkay? Don't forget it, and don't ever let it change while the program is running. It's a constant."
Alternatively, this is like saying "The constant integer FREE_FALL_ACCELERATION is equal to ten."
You will realize very quickly that despite the versatility of computers, they are incredibly anal. If you tried any of the following:
cONSt int FREE_FALL_ACCELERATION = 10;
int const FREE_FALL_ACCELERATION = 10;
const int FREE_FALL_ACCELERATION = 10
const int = 10;
You'd find that the computer will whine at you. Programs must adhere to specific rules. We call those rules "syntax." You follow the syntax of English every day. If your friend called you up and asked, "Pizza hey order! let's" you would think they were Yoda, but more importantly you'd notice that your friend isn't following English syntax.
When you declare constants, you must follow proper syntax (on its own, the machine isn't smart enough to translate your Yoda-code). Always like this:
We'll talk more about that int thing (datatypes) and variable declarations in general next week. For now, just recognize this syntax pattern, especially noticing the use of const.
One last thing. See how the constant name is in capital letters? Always use ALL_CAPS for your constant names.
Instructions
You should first set up your development environment with your lecturer or TA. Also, you should have been walked through the basic elements of the Visual Studio user interface in class. During this exercise, you've seen the compilation and running of a simple application that prints "hello world" to the screen.
Now, open the solution file in apps\homework\01_helloWorld. If you are unsure of the steps, watch this video 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.
See all that green text at the top? That's a "header comment." Update the header comment in main.cpp with your full name.
Using the project explorer, open the file called constants.cpp.
Read the comments and notice the constants. Change the constants according to the comments: make the program use your first name, change the color of the banner, and tidy it up by changing the left margin of the text so it appears centered on the screen.
You have just changed the "facts of reality" from the perspective of the machine by changing the constants. Do the constants ever change during the lifetime of the running program?
Requirements and Rubric
This work is worth 10 points. (Note: be sure you know how to submit your homework properly)
| Requirement | Points | Notes |
|---|---|---|
| Place your name in the comment header in main.cpp | 2 | |
| Name constant changed | 2 | |
| Color constant(s) changed | 2 | |
| Position changed | 2 | |
| Correct submission of src directory as a .zip file. | 2 |
Concepts Exercised: talking to the machine, stating facts, leveraging libraries, graphics & animation, fun
© 2011 Yong Joseph Bakos.