Saturday, November 3, 2012

CPP 03 - Variables and Math Operators



Everything in your computer is a stored as a number (binary digits to be precise, but don't be bother by that now). I know it's weird since your Facebook's page, your music, or movie you are watching on your computer do not look like numbers at all. They are. Trust me on this.

When you write programs you will need to store a lot of information, such as text (we will call these pieces of text 'strings of characters', or simply 'strings'. Also, we will keep a lot of actual numbers. But how to store these strings or numbers in computer's memory to be able to retrieve them and do some manipulation? The answer is to keep them in a so called 'variable'. You can think of a variable as a sort of a box or container in computer's memory where your data will be kept. 

In order to use a variable the C++ compiler must know what type of data you will store in this box. Let's introduce a few of data types (more on these in a later lesson):
  • int - will store whole numbers (e.g. 10, -500, 1234, 0,  etc.)
  • float - will store fractions (e.g. 1.45, -34.1, 3.14159, etc.)
  • char - will store characters (e.g. c, r, %, 5, 13, @, etc.)
Notice
Numbers can also be treated as characters (each character on your keyboard has a special number in so called ASCII codes). Then, compiler does not treat them literally, as quantity but as a number they represented in ASCII codes.

Since the code you write will be run from top to bottom (for now), C++ compiler must know the type of variables and there names before your program begins to use them. We will always declare them at the top of our program.

Another thing I'd like you to learn in this lesson is the basic math operators. Here they are:
  • + is used for addition
  • - is used for subtraction
  • * is used for multiplication
  • / is used for division
  • % is so called modulus providing a remainder of dividing two integers (8 % 3 = 2, since 8 divided by 3 is 2, remainder 2).

Now, we're ready to write some code (do not type the numbers on the left). Save it as ex003.cpp, compile and run.

Pic. 3.1 - The Code ex003.cpp

Explanation
Refer to the picture 3.1 containing the code.

Line 1
This line contains a comment which is a piece of text that the compiler ignores. You should comment on every piece of code so you can help the reader (including yourself) understand what you are doing. There are two types of comments used in C++. First is the one below:

/* here goes your text
    which can span many
    lines
*/

Second type of comment cannot span multiple lines and looks like this

// here comes your comment
It is a good practice for a beginner to start writing program using just comments in their native languages and then fill in the C++ statements.

Line 3
#include <iostream>
This directive will inform the compiler that iostream library (a big chunk of code written by somebody else) will be added to your code so that you can use a lot of statements such as 'cout <<' and your compiler understands what to do.

Line 5
using namespace std;
Just assume you need this for the compiler to do its magic (I cannot explain everything now).

Line 7
main () {
This is so called main function. All programs in C++ must start in the function main (more on functions in the upcoming lessons). The left brace '{' is a starting point of our code. The function and the code must end with the right brace '}' which is our line 15.

Line 9

int  number_of_feet = 30;   // variable storing the number of feet

This line defines (it means declares the variable and puts some content in it) the variable called 'number_of_feet' and assigns it a value of 30. This means that compiler will allocate a piece of computer's memory to store the value and will mark where it is with the name 'number_of_feet', so you can easily refer to it later in your program. Let's break it down even more:
  • int - type of variable (what your box in the memory will store: here integer which means that no fractions are allowed).
  • number_of_feet - the name of the variable (so that you can refer to the content of that box in the memory later).
  • = - will take what is on its right side (here: 30) and assigns (stores) in the memory labeled with the name on its left side (here the piece of memory is labeled with the name, kind of address called: number_of_feet).
  • // - Comment of the programmer as to what this line is supposed to do.
Line 10
float ratio = 0.3048;       // number used to convert feet-to-meters
This line defines a type of 'float' variable called ratio and assigns a value of 0.3048.
The difference between the variable definition and declaration is that the former creates this sort of a labeled box in the memory and immediately stores something in it, and the latter only reserves the memory and does not put any content in it (creates this box in the memory to store the type of data you want but it now contains some random garbage). Example:

int my_first_number = 1; // Definition 
int my_second_number;  // Declaration 

Line 12
cout << number_of_feet << " of feet is " << number_of_feet * ratio << endl;
This line uses instruction 'cout <<' to send to your screen what follows. Pay attention here. 
  • cout << number_of_feet - will send to screen the content of the memory labeled with this variable name. Notice that there is no semicolon ';' after the variable name.
  • << " of feet is " - text between quotes (including spaces) will be displayed right after the content of the memory (variable) storing your number of feet
  • << number_of_feet * ratio - now, your computer will multiply what is stored in both variables (in our case 30 * 0.3048) and will display the result to the screen
  • << endl; - finally system will hit enter creating new line (your cursor goes to new line) and the whole statement ends with semicolon ';' as per C++ requirement.
Line 14
Function 'main()' will return code 0 (all is good) to your operating system when the codes in the function ends (code is between { } - lines 7 through 15)

Line 15
}
This right brace says this is the end of the block of code in function main().

Homework

Exercise 3.1
Write the code that converts 42 degrees of Celsius to its equivalent in Fahrenheit. When saved, compiled and run, the result should look similar to mine in the picture below.
Formula you should use is: Celsius * 9/5 + 32.
The result should be: 42 Degrees of Celsius = 107.6 Degrees of Fahrenheit.

Pic. 3.2 - Homework3.1 Result.