LAB 02B - VARIABLE PRACTICE

Get Started

To get started, create an empty project called Lab02B and add a source file called main.cpp. Follow directions in Lab 01 if process is not memorized yet. Then cut/paste the following code, update the comment section, and build/execute the program. Make sure everything runs fine before moving on to the next step.

/* CSCI 261 LABXXX_OR_HWYYY: NAME_OF_ASSIGNMENT 
* Author: XXXX (_INSERT_YOUR_NAME_HERE_)
*
* Add more complete description here...
*/

#include <iostream>   // For cin, cout, etc.
using namespace std;  // For standard namespace 

int main() {

    /******** MODIFY OR INSERT CODE BELOW HERE ********/

    cout << "Ready to code!!" << endl; 

    /******** MODIFY OR INSERT CODE ABOVE HERE ********/

  return 0; // program ended fine 
  
}
        

Stating Facts to the Machine (declaring constants)

You are aware of several facts about the world, such as your GPA or your middle name. 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/s/s. 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/s/s. How do we define facts 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 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 picky. If you tried any of the following:

FREE_FALL_ACCELERATION int const = 10;
cONSt int FREE_FALL_ACCELERATION = 10;
int const FREE_FALL_ACCELERATION = 10;
const int FREE_FALL_ACCELERATION = 10
const int = 10;

Then you'd find that the computer would whine. Programs must adhere to specific rules. We call these 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 (in other words, the machine isn't smart enough to translate your Yoda-code). For constants, the proper syntax is always like this:

const int FREE_FALL_ACCELERATION = 10;

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. Why? Because then anyone reading your code will think "heh, that's a constant!" (Plus, you'll lose points if you don't follow this coding style.)

Instructions

Write a program to compute the area of a circle. If you don't recall the area of a circle from elementary school, then awww. (Good thing knowledge is always at your fingertips, with that thing they call the Internet.)

Once you have a circle's area equation in hand, you'll realize you should have three variables:

  1. area of type double
  2. radius of type double
  3. PI of type double
It makes sense to use a constant for one of these three variables, and an input statement for another. Figure these things out, and do it! (We suggest you use five digits of precision for the constant.)

After the calculation, print the circle's area. Use appropriate text in the output for understanding, e.g., "The area of a circle with radius 5.2 is 84.9486". Here is an example run of this program:

Enter the radius of your circle: 3.2
The area of a circle with radius 3.2 is 32.1699

Lastly, always, always, ALWAYS update the header comments at the top of your main.cpp file. And if you ever get stuck, remember that there is LOTS of help available.

Lab Submission

You will submit your solution to this lab with your second homework assignment. Detailed instructions for doing this are posted in Homework 02.