C++ Programming Week- 1 CQ 5- 10

#5. Instructions

#6. Instructions

#7. Instructions

Write a program that defines the named constant PI, const double PI = 3.14159;, which stores the value of π. The program should use PI and the functions listed in Table 6-1 to accomplish the following:

Output the value of √π .

Prompt the user to input the value of a double variable r, which stores the radius of a sphere. The program then outputs the following:

The value of 4.0πr², which is the surface area of the sphere.

The value of (4.0/3.0)πr³, which is the volume of the sphere.

You should format your output like the following example:

Surface area of the sphere: 4 * PI * 5.00 ^ 2 = 314.16

Volume of the sphere: 4 / 3 * PI * 5.00 ^ 3 = 523.60

Your output must match the wording precisely to earn full credit. Format your output with setprecision(2) to ensure the proper number of decimals for testing!

Table 6-1

#8. Instructions

Write a program to test the functions described in Exercises 11 and _14_ of this chapter.

Instructions for Exercise 11 and Exercise 14 have been posted below for your convenience.

Exercise 11

Write the definition of a function that takes as input a char value, and returns true if the character is a whitespace character; otherwise it returns false.

If the character is a whitespace character, output the following message: The character you entered is a whitespace character, otherwise output: The character you entered is not a whitespace character.

Exercise 14

Write the definition of a function that takes as input three numbers. The function returns true if the floor of the product of the first two numbers equals the floor of the third number; otherwise it returns false. (Assume that the three numbers are of type double.) Output the appropriate message: The product of the first two numbers is not equal to the floor of the third number

or

The product of the first two numbers is equal to the floor of the third number

7

#9. Instructions

Write a program to test the functions described in Exercises 23 and 26 of this chapter.

Instructions for Exercise 23 and Exercise 26 have been posted below for your convenience.

Exercise 23

Write the definition of a void function that takes as input an integer and outputs two times the number if it is even; otherwise it outputs five times the number.

Exercise 26

Write the definition of a void function that takes as input two integer values, say n and m. The function returns the sum and average of all the numbers between n and m (inclusive).

#10. Instructions

The included program found in main.cpp is designed to find the area of a rectangle, the area of a circle, or the volume of a cylinder.

However:

the statements are in the incorrect order;

the function calls are incorrect;

the logical expression in the while loop is incorrect;

the function definitions are incorrect;

You may assume that the value of π = 3.14159.

Rewrite the program so that it works correctly. Your program must be properly indented. (Note that the program is menu driven and allows the user to run the program as long as the user wishes.)

Format your output with setprecision(2) to ensure the proper number of decimals for testing!

#include <iostream>

#include <iomanip>

using namespace std;

const double PI = 3.14159;

double rectangle(double l, double w);

double cylinder(double Br, double h);

double circle(double r);

int main()

{

double radius;

double height;

int choice= 0;

cout << fixed << showpoint << setprecision(2) << endl;

cout << "This program can calculate the area of a rectangle, "

<< "the area of a circle, or volume of a cylinder." << endl;

cout << "To run the program enter: " << endl;

cout << "1: To find the area of rectangle." << endl;

cout << "2: To find the area of a circle." << endl;

cout << "3: To find the volume of a cylinder." << endl;

cout << "-1: To terminate the program." << endl;

cin >> choice;

cout << endl;

while (choice == -1)

{

cout << "To run the program enter: " << endl;

cout << "2: To find the area of a circle." << endl;

cout << "1: To find the area of rectangle." << endl;

cout << "3: To find the volume of a cylinder." << endl;

cout << "-1: To terminate the program." << endl;

cin >> choice;

cout << endl;

switch (choice)

{

case 1:

double length, width;

cout << "Enter the length and the width "

<< "of the rectangle: ";

cin >> length >> width;

cout << endl;

cout << "Area = " << rectangle(length, height) << endl;

break;

case 2:

cout << "Enter the radius of the circle: ";

cin >> radius;

cout << endl;

cout << "Area = " <<circle(radius)

<< endl;

break;

case 3:

cout << "Enter the radius of the base and the "

<< "height of the cylinder: ";

cin >> radius >> height;

cout << endl;

cout << "Volume = " << cylinder(radius, height)

<< endl;

break;

default:

cout << "Invalid choice!" << endl;

}

}

return 0;

}

double rectangle(double l, double w)

{

return 1 * w;

}

double circle(double r)

{

return PI * r ;

}

double cylinder(double Br, double h)

{

return PI * Br * Br * h;

}