< previous page page_588 next page >

Page 588
11. Using a loop control variable today of the type declared in Exercise 8, write a For loop that prints out all five values in the domain of the type. To print each value, invoke the function of Exercise 10.
12. Below is a function that is supposed to return the average of two integers, rounded to the nearest integer.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
int Avg( /* in */ int1,
         /* in */ int2 )
{
    return float(int1) / float(int2);
}
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Sometimes this function returns an incorrect result. Describe what the problem is in terms of type promotion or demotion, and fix the problem.
Programming Problems
1. Read in the lengths of the sides of a triangle and determine whether the triangle is an isosceles triangle (two sides are equal), an equilateral triangle (three sides are equal), or a scalene triangle (no sides are equal). Use an enumeration type whose enumerators are ISOSCELES, EQUILATERAL, and SCALENE.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
The lengths of the sides of the triangle are to be entered as integer values. For each set of sides, print out the kind of triangle or an error message saying that the three sides do not make a triangle. (For a triangle to exist, any two sides together must be longer than the remaining side.) Continue analyzing triangles until endof-file occurs.
2. Write a C++ program that reads a single character from A through Z and produces output in the shape of a pyramid composed of the letters up to and including the letter that is input. The top letter in the pyramid should be A, and on each level, the next letter in the alphabet should fall between the letter that was introduced in the level above it. For example, if the input is E, the output looks like the following:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
3. Read in a floating point number character by character, ignoring any characters other than digits and a decimal point. Convert the valid characters into a single floating point number, and print the result. Your algorithm should convert the whole number part to an integer and the fractional part to an integer, and combine the two integers as follows:
0588-01.gif
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
For example, 3A4.21P6 would be converted into 34 and 216, and the result would be the sum
0588-02.gif

 
< previous page page_588 next page >