CSCI 160 basic selection (if/else) exercises

Here are some practice problems relevant to our class work. Most of the problems are of the form "write and debug a C++ program that does ....".

Some of the examples I will go through in class, and I'll happily look at your solutions with you in office hours, but I will not be posting sample solutions. (It does you way more good to work through, debug, and test the programs than to simply look at someone else's solution.)

  1. Write a program to read in and sort three integers.

  2. Write a program to read in five numbers and print the smallest, largest, total sum, and the average of the five.

  3. Suppose a person pays no tax on the first $18,000 they earn, 17.5% tax on the next $24,000, and 35% tax on the remainder. Write a program to read in a person's income and compute their taxes.

  4. For a triangle whose sides are lengths L1, L2, and L3, the formula for the area of the triangle is the square root of (N*(N-L1)*(N-L2)*(N-L3)) where N is (L1+L2+L3)/2. Write a program to read in the lengths of the three sides of a triangle and compute its area.

  5. Suppose a person's phone contract says Write a program to read in the number of text messages a person sent, the number of minutes of local calls they made, and the minutes of roaming calls they made, and compute their phone bill.

  6. Write a program that can guess a number in the range 1-15 within four tries (assuming the other player tells them higher/lower on each incorrect guess).

  7. Suppose letter grades are assigned as 85% or better is an A, 72% or better is a B, 60% or better is a C, 50% or better is a D, less than 50% is an F. Write a program to read in an assignment mark (e.g. 73.5) and compute the letter grade.

  8. Write a program that reads in two times (hours and minutes) and displays the number of hours and minutes between them. (E.g. 5 45 and 6 15 would have 0 hours and 30 minutes between them.)