Activity 3-1 - Variables and Assignments




Warm Up 3.1
Before going through this lab, you need to read Chapter 2 of the text. Start the lab by answering the following questions. Insert the answers in the following table and save it a file WarmUp.doc(x)

Table of Answers

Question

Part

Answer

1

a

 

 

b

 

c

 

d

 

2

a

 

 

b

 

c

 

3

a

 

 

b

 

c

 

 

d

 

 

e

 

f

 

4

 

 

5

 

 

1) What is the answer to the following arithmetic operations?
            a) (1/5) * 3 =
            b) (8/2) * 4 =
            c) (1.0/5.0) * 3 =
            d) Is there any difference between a) and c)?

2) Convert the following mathematical expressions to C++ expressions:
            a) 4x
            b) (2x + y)/3y
            c) (2x - 3y)/(z -1)

3) Translate the following English statements to C++ expressions.
    a) Assign 2.7 to variable a
    b) Check to see if variable a is larger than 3.0
    c) Check to see if a is larger than 3 OR it is 0
    d) Check to see if a is larger than or equal to 3 but less than 8
    e) Check to see if a is not 5
    f)  Check to see if a is not larger than 9

4) Suppose age and ticket are both integer values, how do you state the following: "if you are 12 years or older the ticket is $16, otherwise the ticket is $8."

5) When the square root of b2 - 4ac is less than zero, how do you display the message "square root of a negative value is not defined"?

Variables and Assignments

"All variables must be declared before they are used in a C++ program."

All variables used in a C++ program must be declared.  The declaration of a variable can be done in several different ways; 1) right at the beginning of the main function, 2) right before its use in the program, and 3) right before the main function.  As it was shown in the sample C++ program in the previous lab, we declared variables of type integer and float right at the beginning of the main function.  In general, the syntax for variable declaration is:
Type_name  Variable_Name_1, Variable_Name_2;

Example:
int  number_of_cases, bottles_per_case;

Here is the list of the most common variable types used in C++:
 

Variable Name
Variable Name
short (short int) - 2 bytes 
short x; or short x = 34;
long - 4 bytes
long x; or long x = 34;
int - 4 bytes
int x; or int x = 34;
float - 4 bytes, 7 digits precision
float x; or float x = 34.56;
double -  8 bytes, 15 digits precision
double x; or double x = 34.56;
long double - 10 bytes, 19 digits precision 
char - holds any single character on the keyboard
char c; or char c = 'a';
bool - True (1), False (0)
bool x; or bool x = TRUE;

Every variable is identified by a name that is referred to as identifier.  An identifier must start with either a letter or the underscore symbol.  The remaining characters must all be letters, digits, or the underscore symbol.

In general, every variable should have a type.  An attempt to assign a value of a different type other than the original assigned type to a variable is referred to as "type mismatch".  One has to be very careful that not all compilers will allow type mismatch, i.e., may produce an error when such attempts are made.

Arithmetic Operators and Expressions
To perform mathematical calculations, we combine variables and/or numbers using arithmetic operators such as: + for addition, - for subtraction, * for multiplication, / for division, and % to find the remainder of a divisionThe arithmetic operators can be used with numbers of type int, long, double, float, long double, short, or with a combination of different types. When you use arithmetic operators, you need to be careful that if the correct variable types are not used, the result can be very different from what you were expecting.  For example: 7.0/2 = 3.5, 7/2.0 = 3.5, however, 7/2 = 3 (not 3.5). The reason for such a significant difference is because the last one is actually using integer division.  Perhaps the most significant problem will be seen in cases where the numerator is smaller than the denominator.  In such cases the integer division will result in 0.  An example of such a case is 5/9 = 0.

There are some shorthand notations that combine the assignment operator ( = ) and an arithmetic operator. The general form for this notation is:
Variable Operator = Expression, which is equivalent to:  Variable = Variable Operator (Expression).

The expression can be another variable, a constant, or a more complicated arithmetic expression.  Here are some examples:
 

Shorthand Notation
Equivalent
total += 2;  total = total+2;
amount +tax - rebate amount amount + (tax - rebate)

Similarly, we may use -=, *=, /=, and %=.

Exercise 31-1
Assume that each variable contains the corresponding value. For example, variable twenty contains 20, variable ten contains 10. Determine the answer to the following statements:
    Question 3.1     three -= 4*5;
    Question 3.2     forty %= 16/2;
    Question 3.3     eightyeight /= 82%4;