Home Practices Page 14 - Basic Data Types and Calculations

# Exercises - Practices

This article looks at some of the basic data types that are built into C++. If you're learning how to use C++, you will want to keep reading, since you'll be using these data types in all of your programs. It is taken from chapter two of the book Beginning ANSI C++: The Complete Language, by Ivor Horton (Apress, 2004; ISBN: 1590592271).

Rating:  / 14
September 08, 2005

SEARCH DEV SHED

TOOLS YOU CAN USE

The following exercises enable you to try out what you’ve learned in this chapter. If you get stuck, look back over the chapter for help. If you’re still stuck, you can download the solutions from the Downloads area of the Apress website ( http://www.apress.com ), but that really should be a last resort.

http://www.apress.com ), but that really should be a last resort.The following exercises enable you to try out what you’ve learned in this chapter. If you get stuck, look back over the chapter for help. If you’re still stuck, you can download the solutions from the Downloads area of the Apress website (http://www.apress.com), but that really should be a last resort.

Exercise 2-1. Write a program that will compute the area of a circle. The program should prompt for the radius of the circle to be entered from the keyboard, calculate the area using the formulaarea = pi * radius * radius, and then display the result.

Exercise 2-2. Using your solution for Exercise 2-1, improve the code so that the user can control the precision of the output by entering the number of digits required. (Hint: Use thesetprecision()manipulator.)

Exercise 2-3. Create a program that converts inches to feet-and-inches— for example, an input of 77 inches should produce an output of 6 feet and 5 inches. Prompt the user to enter an integer value corresponding to the number of inches, and then make the conversion and output the result. (Hint: Use aconstto store the inches-to-feet conversion rate; the modulus operator will be very helpful.)

Exercise 2-4. For your birthday you’ve been given a long tape measure and an instrument that allows you to determine angles—the angle between the horizontal and a line to the top of a tree, for instance. If you know the distance, d, you are from a tree, and the height, h, of your eye when peering into your angle-measuring device, you can calculate the height of the tree with this formula:

h+d*tan(angle)

Create a program to readhin inches,din feet and inches, andanglein degrees from the keyboard, and output the height of the tree in feet.

NOTE There is no need to chop down any trees to verify the accuracy of your program. Just check the solutions on the Apress website (http://www.apress.com/book/download.html).

Exercise 2-5. Here’s an exercise for puzzle fans. Write a program that will prompt the user to enter two different positive integers. Identify in the output the value of the larger integer and the value of the smaller integer. (This can be done with what you’ve learned in this chapter!)

 >>> More Practices Articles          >>> More By Apress Publishing