header

Homework 3

Output Requirements

Output the radius, diameter, circumference, and area of a circle. Each value is to be displayed on a separate line and labeled and formatted as illustrated below.

Lab 1 Example

Input Requirements

Ask the user to enter the value of the radius.

Process Requirements

Calculate the diameter (2r), the circumference (2πr), and the area (πr2) of the corresponding circle. You may assume that the user will enter a positive value for the radius.

Miscellaneous Requirements

Instructions

Your program should display the instructions for using the program as illustrated in the sample run shown above.

Store π as a Constant

Define π as a named double constant (PI) with a value of 3.14159265358979. To declare a constant, insert the keyword "const" in front of the variable declaration and initialize the value of the constant:

const double PI = 3.14159265358979;

Typically, constants are declared before the main function (while variables are declared within the main function.) The value assigned to a constant can never be changed; that is why it is said to be constant. By common convention, constant identifiers are all upper-case.

Format the Numeric Output

The numeric output is to be displayed in a fixed point format to 5 decimal places. This can be done using the manipulators fixed and setprecision. To use these manipulators you must include the iomanip class:

#include <iomanip>

The fixed manipulator tells the output stream to format decimal numbers using a fixed point format. The setprecision manipulator tells the output stream to use the specified number of decimal places. For example, the following line will cause the standard output stream to display decimal values using a fixed point format to five decimal places:

cout << fixed << setprecision(5);

This line of code should be the first line of code in the output section of your program. Both of these manipulators are persistent in that their effects are permanent until it is changed by another manipulator. (This is not true for all manipulators, as you'll see.)

The setw manipulator is used to specify the field width to be used for the next item inserted into the output stream. For example, the statement

cout << setw(10)<< varId;

specifies that the value of varId is to be padded to a width of exactly 10 characters by adding additional space characters as needed. The value is first converted to a text representation. Then space characters are added to the beginning of the string until there are exactly 10 characters in the string. Finally, the padded string is inserted into the output stream.

Since the space characters are added to the beginning of the string, the resulting output is said to be right-justified. That is, the item will be as far to the right as possible within the specified 10-character string.

Unlike the fixed and setprecision manipulators, the setw manipulator is not persistent. It applies only to the next item inserted into the stream.

Exponentiation

There is no exponentiation operator in C++. Consequently, there is no way to write r2. You'll have to use the definition of the square and multiply r by itself.

Save your program file as "Hmwk03.cpp" and send it to me as an email attachment.