For this experiment, you will write code for real-number variables.
The keyword for the real-number data type in C++ is
float, but it's half as precise. Also, by
default, real numbers will be stored in double precision in memory.)
The difference between a real number and an integer is a decimal point: real numbers have a decimal point, integers do not.
Consider the initialization of our integer variables in the original program:
int count = 3, number = 5;
Write declarations for
width, initializing them to
Do not get rid of any code. Compile and execute your code.
Question #2.3.1: What declarations did you add to the program?
You won't see any change in the execution because these declarations and initializations only allocate memory locations for these variables and store the specified values in them.
Once you can compile and execute
the code without problems, add a statement to display
width. The original program had this statement to print
cout << "count is " << count << "\n" << "number is " << number << endl;Your new statement for
widthshould look quite similar, replacing the integer variables with the real-number variables and changing the labels appropriately.
Now compile and execute the program. Make sure it prints the proper labels and values for the variables.
Question #2.3.2: What is the output statement you just added to your program?
Now, let's experiment with these declarations.
In the integer experiment, we tried initializing an integer variable
with various types. Let's try this again for the
variables you just added to your program.
For the following questions, "I didn't get an error message or warning" might be perfectly acceptible.
length to an integer and compile your program.
Question #2.3.3: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what output is produced when the program is executed?
length to a character and compile your program.
Question #2.3.4: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what output is produced when the program is executed?
length to a string and compile your program.
Question #2.3.5: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what output is produced when the program is executed?
cin and compile your
Question #2.3.6: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what output is produced when the program is executed?
You probably didn't get any warning or error for initializing
length to be an integer. Keep in mind that a real number, a
double, may have a decimal point in it. It's not required
to. Any number without a decimal point can easily have one added:
C++ compilers will (implicitly) supply this decimal point.
In the previous experiment, however, the compiler most likely
complained when you
tried initializing an
int variable with a floating-point
number. This is because an
int cannot have a decimal
point it in. The compatibility between these two types is one-way.
Integers are easy to write: just a bunch of digits, possibly with a negative sign on the front.
Simple real numbers, known as fixed-point real literals, are also easy to write: write some digits and put in (at most) one decimal point. However, if you want to represent really large or really small numbers, we'd need a simpler notation. For example, chemists measure amounts in moles. One mole is the equivalent of approximately 602 million trillion atoms — that's 602 followed by 21 zeros!
A floating-point real literal uses scientific notation (also known as exponential notation or floating-point notation) to represent these very large or very small numbers. Scientific notation uses 10 to a power to shift the decimal point in a number. So, for example, the number 602 million trillion can be written 6.02 x 1023 in scientific notation, which represents 6.02 multiplied by 10 to the 23th power. This effectively shifts the decimal point over 23 positions to the right, adding zeros as needed.
However, C++ does not provide superscript notation; instead, it uses a
short hand notation: the letter
E for "exponent." which is followed by the exponent.
Thus, 602 million trillion can be written as
or . . . in C++.
Change your program so that
length is initialized to
width is initialized to
Question #2.3.7: Predict: what will your program display when you execute it?
Compile and execute your program to test your prediction.
Question #2.3.8: What did your program actually display? Did this agree with your prediction?
Negative real numbers and negative exponents are also allowed —
-1.23456E3 for -1234.56,
for 0.00123456, and
-1.23456E-3 for -0.00123456.