1. A program to calculate the area of a circle given the radius
as input.
Input (what the user might type in): radius
Output (what would be displayed to the user):
area
Solution:
Algorithm: area = PI * radius * radius
Variables and Types: The type of variable area would be double and the type of variable radius would be double. PI would be a symbolic constant equal to 3.14.
2. A program to convert inches (given as a whole number) to miles,
feet, and inches.
Input: inches (as a whole number)
Output: miles, feet, and inches
Solution:
Algorithm: (Hint: 1 mile is equal to 5280 feet and 1 foot
is equal to 12 inches)
Variables and Types:
3. A program that calculates the amount of US money in dollars
given the number of pennies, nickels, dimes, and quarters.
Input: number of pennies, nickels, dimes,
and quarters
Output: US dollar amount
Solution:
Algorithm: (Hint: 1 penny = .01 dollars, 1 nickel = .05
dollars, 1 dime = .10 dollars, 1 quarter = .25 dollars)
Variables and Types:
Solution:
Algorithm: (Hint: 1 mile is equal to 5280 feet and 1 foot is equal to 12 inches)
feet = inches / 12
inches = inches % 12
miles = feet / 5280
feet = feet % 5280
Variables and Types:
feet, inches, and miles would all be of type int in order to use the
% operator.
3. A program that calculates the amount of US money in dollars
given the number of pennies, nickels, dimes, and quarters.
Input: number of pennies, nickels, dimes,
and quarters
Output: US dollar amount
Solution:
Algorithm: (Hint: 1 penny = 1 cent, 1 nickel = 5 cents, 1 dime = 10 cents, 1 quarter = 25 cents)
total = num_pennies * .01 + num_nickels * .05 + num_dimes * .10 + num_quarters
* .25
Variables and Types:
num_pennies, num_nickels, num_dimes, and num_quarters would be of type int since it doesn't make sense to have a fractional piece of a coin. total would of type double since dollars can be fractional (i.e. $3.27). When writing the C program, one would use a cast for the multiplication of num_pennies and .01 since num_pennies is an integer and .01 is a double.
Alternatively, one could use the following algorithm (making sure all multiplications are done on pairs on integers)
total_cents = num_pennies * 1 + num_nickels * 5 + num_dimes * 10 + num_quarters
* 25
total_dollars = (double) total_cents / 100.00