Sample Program to Practice

Below are a few sample programs for practice. I would stongly recommand that you complete them, or at least try to complete them. Each program might use for loops, while loops, and/or if statements.

Suppose a ball is dropped at a fixed height (say 100 meter), and each time it bounces to half its original height. Write a program that displays all heights until the ball is within 0.001 meters of the ground.

Write a program that computes the average of exam scores for this class (there are 30 registered students).

Write a program that computes the average of exam scores for any class.

Write a function "getScale" that asks the user to enter a character and that returns exactly one of the following letters: c for centimeter, i for inches, f for feet, m for meter, or k for kilometer. Make sure to also create a test program th at checks if the function works properly.

What is wrong with the following while loops (and how does the correct one line like):

int counter = 1;
while (counter < 100)
   cout << counter <<"\n";
int counter = 1;
while (counter < 100)
   cout << counter << "\n";

Write a function that counts the number of digits in an integer value. Make sure to test whether the function works properly.

Suppose that a gas company charges the following rates, based on consumption:

Gas used Rate
First 70 cubic meters $5.00 fixed cost
Next 100 cubic meters 5.0 cents per cubic meter
Next 230 cubic meters 2.5 centers per cubic meter
Above 400 cubic meters 1.5 cent per cubic meter

Write a program that computes the charges for a given amount of gas usage. The program should continue to work until the user enters a negative number.

Create functions necessary for a text-based menu system. As a suggestions, you might try the following functions:

several functions handleChoice1, handleChoice2, etc.