created 08/09/99


Chapter 18 Programming Exercises



Exercise 1 --- Miles per Gallon

Write a program that calculates miles per gallon for a list of cars. The data for each car consists of initial odometer reading, final odometer reading, and number of gallons of gas. The user signals that there are no more cars by entering a negative initial odometer reading.

Miles Per Gallon Program
Initial miles:
15000
Final miles:
15250
Gallons
10
Miles per Gallon: 25.0

Initial miles:
107000
Final miles:
107450
Gallons
15
Miles per Gallon: 30.0

Initial miles:
-1
bye
Click here to go back to the main menu.

Exercise 2 --- In-range Adder

Write a program that asks the user for the low and high integer in a range of integers. The program then asks the user for integers to be added up. The program computes two sums:

The user signals the end of input with a 0.

In-range Adder
Low end of range:
20
High end of range:
50
Enter data:
21
Enter data:
60
Enter data:
49
Enter data:
30
Enter data:
91
Enter data:
0
Sum of in range values: 100
Sum of out of range values: 151
Click here to go back to the main menu.

Exercise 3 --- Shipping Cost Calculator

A mail order company charges $3.00 for handling, free shipping for orders 10 pounds or less, plus $0.25 for each pound over 10. Write a program that repeatedly asks the user for the weight of an order, then writes out the shipping charge. The program stops when a weight of zero or less is entered.

Weight of Order:
5
Shipping Cost: $3.00

Weight of Order
20
Shipping Cost: $5.50

Weight of Order
0

bye
Click here to go back to the main menu.

Exercise 4 --- Area of Rectangles

A computer aided design program expects users to enter the coordinates two corners for each of several of rectangles (see diagram.) The sides of the rectangles are assumed to be parallel to the X and Y axes. The coordinates of each corner is entered as a pair of integers, first the X coordinate and then the Y coordinate. The origin of the coordinate system (0,0) is in the upper left, so Y increases going downward, and X increases to the right.

For each rectangle, the program calculates and writes out the height, the width, and the area of the rectangle. The two corners entered for each rectangle must be diagonally opposite (upper left and lower right, or upper right and lower left), but which choice is made for each rectangle is up to the user. The user can enter the corners in any order. Height and width are always positive (the program will have to adjust its calculations so that this is true.)

The program ends gracefully when the user enters corners which cannot be those of a rectangle (either the height is zero, the width is zero, or both.)

Computer Aided Design Program
First corner X coordinate:
100
First corner Y coordinate:
100
Second corner X coordinate:
250
Second corner Y coordinate
200
Width:  150  Height: 100   Area: 15000

First corner X coordinate:
250
First corner Y coordinate:
200
Second corner X coordinate:
100
Second corner Y coordinate
100
Width:  150  Height: 100   Area: 15000

First corner X coordinate:
100
First corner Y coordinate:
200
Second corner X coordinate:
250
Second corner Y coordinate
100
Width:  150  Height: 100   Area: 15000

First corner X coordinate:
100
First corner Y coordinate:
100
Second corner X coordinate:
100
Second corner Y coordinate
100
Width:  0  Height: 0   Area: 0

finished
Click here to go back to the main menu.

End of exercises.