Class 2: Variables and Memory
Variables A variable is a value that is stored in memory It can be numeric or a character C++ needs to be told what type it is before it can store it in memory It also needs a name the identifier so that it can be referred to
Numeric Types int num_apples = 3; double pi = 3.14159; A numeric type can be either an integer type (int) or a floating point type (double). There are other integer and floating point types, but int and double are the most commonly used.
Declaration of Variables Syntax: Examples type identifier; type identifier = value; type identifier1, identifier2, etc. int grade1; int grade2 = 93; int grade3 = 92, grade4, grade5 = 71;
What the declaration does Tells the computer what kind of variable you are creating It gets placed at the bottom of the memory stack
Identifiers Identifiers must be composed of letters, digits and underscores. They cannot begin with a digit They cannot be reserved words in C++ Style: Identifiers should be meaningful, self-documenting Example: int abcde int 1abcde int abcde1 int abc_de int abc-de int double; valid invalid valid valid invalid invalid
Example A program is to calculate the wind chill for a temperature of 72.5 F and a wind speed of 20 mph. The formula for wind chill is as follows (where T is the temperature and V is the wind speed): 35.74 + 0.6215T 35.75V 0.16 + 0.4275TV 0.16
Windchill Program
Observations 1. The values for wind speed and temperature are hard coded in the program. It would be an improvement if they could be entered by the user. 2. C++ has libraries of useful functions like pow. The header file which declares the function must be #included at the top of the program. 3. To display the value of a variable to the screen, use cout << variablename << endl
Standard Input and Output int num_trees; cout << enter number of trees << endl; cin >> num_trees; cout << The user has entered << num_trees << endl;
Windchill program: data taken from user
The assignment operator (=): changing the value of a variable In math, we use = in two ways as an action: let n = 2 as a claim: if n = 3, then... In C++, there are two different operators for this Assignment: n = 2; Equality: if ( n == 3 )
Example: what happens on the memory stack int i = 1; int j; j = i; i = 2*j; j = i + 1; i = 3*i;
Floating point versus integer arithmetic Mod operator: i % j = the remainder of i when divided by j they must be integer types Ex: 17%2 1 Division operator: i/j gives the value of i divided by j if both are integer types, the result is an integer, which the fractional part truncated if either is a floating point type, the result is also a floating point number Ex: 17/2 8 17.0/2 8.5 17/2.0 8.5 17.0/2.0 8.5
Example Write a program that gets a 5 digit number from the user. If the number is not a 5 digit number, it exits with a message telling the user. If it is a 5 digit number, it displays the sum of all the digits. New C++ needed: if (A B) { } else { } exit(-1); a conditional statement, means or exit the program, -1 indicates an error has occurred.
Type casting a double can be cast to an int, or an int to a double Examples: int(2.5) 2 double(2) 2.0 2/5 0 double(2)/5 0.4 2.5 int(2.5) 0.5
Assignment #1 a. Write a program that gets the month (as a value between 1 and 12) and the day of the month from the user for two dates (in the same year). If the month or day is not valid, it gives a message to the user and exits. Otherwise, it displays a message, either that both dates are the same day of the week, or that they are different days of the week. Your program should work for non leap years.