Thursday, October 17, 2013

C++ Tutorial #7: Doing Math With Variables

This is going to focus on a different variable from what you worked with last time: int, which stands for integer (which, if you block out memories of math class, is a whole real number that can be either positive or negative).

The Operator(s)

(Insert obligatory Marble Hornets reference)

Now, when you do math with variables, you'll have some operators to work with.  Operators are symbols like + and - that stand for various math operations.  You don't always write them in code as you would on a piece of paper, since the keyboard doesn't have all the typical symbols (there's no key for the standard "divide by" symbol you see in math books).  So here's a guide for using basic operators in programming:

+   Means "add", and looks the same as its standard symbol.
-    Means "subtract" and also looks the same as it does in math books.
*    Means "multiply".  It's the asterisk, not the x-key.
/    Means "divide".  It's a forward slash.
=   Means "equals".  You've noticed that it's generally used for declaring variables.  You can substitute the word "equals" for "is".  "This variable is five."  "This variable equals five."

There are more operators, but we don't need to discuss them right now.  These are the main ones you'll be using.

Outputting Variables in Programs

Say you do your math in a program and want to share your findings with any user who runs your creation.  You'll need to output it somehow, and thankfully there are not one, but two ways to incorporate the values of variables into your cout statements.

Let's say I have these lines:

int first = 5;
int second = 10;

You know that if you add those two, you'll get 15.  However, your user will not because the user probably won't be snooping through the code of your programs.  Therefore, you need to tell them using a cout statement.  And so enter our two methods:

Method #1: the third variable

One thing you can do is create a third variable whose value is the result of the math problem, and then insert that third variable into the cout statement.  So, the snippet of code would look like this:

int first = 5;
int second = 10;
int third = first + second;  //creating the third variable

cout << "The value of first + second = " << third << "." << endl;

The output would look like this:

The value of first + second = 15.

See how that works?  The computer knows to read third and look for the number that third stands for.

Method #2: inserting the problem into the cout statement

In most cases, the third variable method is helpful, but sometimes (and especially with smaller practice programs like this one) it's easier to just place the math problem directly into the cout statement, like so:

int first = 5;
int second = 10;

cout << "The value of first + second = << first + second << endl;

Looks slightly different, and there's no third variable, but the output is still the same because the computer takes the value of first, the value of second, adds them, and displays the resulting value.

The value of first + second = 15.

You end up in the same spot; it's just that in certain situations it might be easier to use one method than the other.  Be sure to practice both.

Practice Programs: Integer Variables

I'm not going to go over the other numeric variables at this point.  Right now, I just want you to understand how to work with the most basic numeric variable.  Thankfully, it's really easy to come up with practice programs for int variables, so once you see a couple of models here you should be able to practice them on your own quite easily.


In this program, the program already has the values of the width and the height of the rectangle.  It's pre-programmed in, so we don't need to ask the user for any information.  We take this pre-programmed information and perform a couple of calculations with it.  We then store the result of these calculations into two variables, area and perimeter, and when we want to output the information, we just put the variable names into the cout statements (as we did in the first outputting method with the third variable).

#include <iostream>

using namespace std;

int main()

     //these variables are already programmed in.
     int width = 5;
     int height = 8;
     int area = width * height;
     int perimeter = (2 * width) + (2 * height);

     cout << "The width of the rectangle is " << width << endl;
     cout << "The height of the rectangle is " << height << endl;
     cout << "The area of the rectangle is " << area << endl;
     cout << "The perimeter of the rectangle is " << perimeter << endl;

     return 0;


Type this out, save it in your programming folder as "Rectangle.cpp", compile it, and run it in Command Prompt.  The output should look like this:

The width of the rectangle is 5
The height of the rectangle is 8
The area of the rectangle is 40
The perimeter of the rectangle is 26


In this program, we'll ask the user to input their age and their grandmother's age, and we'll calculate the difference.  However, instead of putting the difference into its own variable, we'll put the math equation in the cout statement and output it directly from there.

#include <iostream>

using namespace std;

int main()
     int myage;
     int grandma;

     //get the user's age
     cout << "How old are you?";
     cin >> myage;
     cout << endl;

     //get the user's grandma's age
     cout << "How old is your grandma?";
     cin >> grandma;
     cout << endl;

     //Find the difference by putting the equation in the statement.
     cout << "The difference between your ages is " << grandma - myage
          << " years." << endl;

     return 0;


Note that the final cout statement is really long, so I had to break it up.  In a cout, every piece consists of the two less-than signs and some kind of component (either the statement in quotation marks, a variable name or an endl).  You can break up those little pieces if you have to (compilers generally don't like long lines in code), just make sure the piece is complete (so you have the << and the component), and be sure to indent so that it looks neat.  For example:

cout << blah blah blah really long line
     << start next part here, with signs lined up nicely

Now, back to our Granny code: this is what the output looks like.

How old are you?  18
How old is your grandma?  83
The difference between your ages is 65 years.

That's quite enough for now.  Next time we'll talk more about char and string variables.

No comments:

Post a Comment