Worksheet Four
Variables
1. What do most programming languages use to name and store data ?
2. What do you call the number or other type of data held in a variable ?
3. How are variables implemented by the compiler ?
4. Identify the valid identifiers from the following:
x x1 x_1 1_abc 12 3x %change data-1 count data2 Big_Bonus sum RATE ABC1237
prog.cpp myfirst.c
5. Is C++ case sensitive ? Will there be a difference between a variable named R and r ?
6. Can you use int as an identifier ? Why ?
7. Can you use cout as an identifier ? Why ?
8. Give the declaration for two variables called feet and inches. Both variables are of type int
and both are initialized to zero in the declaration. Use both initialization alternatives.
9. Give the declaration for two variables called count and distance. count is of type int and is
initialized to zero. distance is of type double is initialized to 1.5.
10. Give a C++ statement that will change the value of the variable sum to the sum of the values
in variables n1 and n2. The variables are all of type int.
11. Give a C++ statement that will increase the value of the variable length by 8.3. The variable
length is of type double.
12. Give a C++ statement that will change the value of the variable product to it’s old value
multiplied by the value of the variable n. The variables are all of type int.
13. Write a program that contains statements that output the value of five or six variables that
have been declared, but not initialized. Compile and run the program. What is the output?
Explain.
14. Give a good variable name for each of the following:
a. A variable to hold the speed of an automobile
b. A variable to hold the pay rate for an hourly employee
c. A variable to hold the highest score in an exam
Input and Output
1. Give an output statement that will produce the following message on the screen:
The answer to the question of
Life, the Universe, and Everything is 42.
2. Give an input statement that will fill the variable the_number (of type int) with a number
typed in at the keyboard. Precede the input statement with a prompt statement asking the
user to enter a whole number.
3. What statements should you include in your program to ensure that, when a number of type
double is output, it will be output in ordinary notation with three digits after the decimal
point ?
4. Write a complete C++ program that writes the phrase “Hello World” to the screen. The
program does nothing else.
5. Write a complete C++ program that reads in two whole numbers and outputs their sum. Be
sure to prompt for input, echo input, and label all output.
6. Give an output statement that produces the new-line character and a tab character.
7. Write a short program that declares and initializes double variables one,two,three,four and
five to the values 1.000, 1.414, 1.732, 2.000, and 2.236 respectively. Then write output
statements to generate the following:
N Square Root
1 1.000
2 1.414
3 1.732
4 2.000
5 2.236