Lab 2: Experiment 3


The Program

For this experiment, you will write code for real-number variables.

The keyword for the real-number data type in C++ is double. (There's also float, but it's half as precise.)

The difference between a real number and an integer is a decimal point: real numbers have a decimal point, integers do not.

Real Number Declarations

Consider the initialization of our integer variables in the original program:

int i = 3, j = 5;

Write declarations for double variables x and y, initializing them to 3.1 and 5.667, respectively. Do not get rid of any code. Compile and execute your code.

Question #2.3.1: What is the declaration you successfully added to your program?

You won't see any change in the execution since the declaration and initialization is purely internal. Once you can compile and execute the code without problems, add a statement to print out x and y. The original program had this statement to print i and j:

cout << "i is " << i << "\n"
     << "j is " << j << endl;
Your new statement for x and y should 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?

Let's play around with these declarations.

Real-Number Initialization

In the integer experiment, we tried initializing an integer variable with various types. Let's try this again for the double variables you just added to your program.

For the following questions, "I didn't get an error message or warning" might be perfectly acceptible.

Initialize x to an integer. 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 does it print for the new initialization?

Initialize x to a character. 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 does it print for the new initialization?

Initialize x to a string. 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 does it print for the new initialization?

Initialize x to be equal to cin. Compile your program.

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 does it print for the new initialization?

Observations

You probably didn't get any warning or error for initializing x 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: 3 becomes 3.0. C++ compilers are happy to add this decimal point for you.

In the previous experiment, though, the compiler 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.

Real-Number Literals

Integers are simple 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 similar to write: write some digits and put in (at most) one decimal point. However, if you want to represent really big or really small numbers, we'd need a simpler notation. For example, chemists like to measure amounts of an element in moles. One mole is equivalent to about 602 million trillion atoms---that's 602 with 21 zeros after it. Yikes!

A floating-point real literal uses scientific notation (also known as exponential notation or floating-point notation) to represent these extreme 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.02x1023 in scientific notation. It's just a math equation: take 6.02 and multiply it by 10 to the 23th power. This effectively shifts the decimal point over 23 positions to the right, adding zeros as needed.

However, we don't have the ability to write superscripts, and programmers are notoriously lazy. So C++ uses a short hand for floating-point notation: just the letter e for "exponent". So 602 million trillion can be written as 6.02e23 in C++.

Change your program so that x is initialized to 6.02e23 and y is initialized to 60.2e22.

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? Compare the actual output to your prediction.

It's possible to have negative real numbers and negative exponents. Negative exponents move the decimal point to the left, making the number a small fraction, that is, close to zero. A negative number just makes it negative.

Terminology

exponential notation, fixed-point, floating-point, floating-point notation, scientific notation
Back to the Lab Exercise  |  Forward to the Next Experiment
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.