Worksheet #1

Straight-line Programs


Instructions

Open the program SumOfTwoInts.cpp and start up CodeWarrior. This program, as an example, adds two integers and prints the result.

Run SumOfTwoInts.cpp to see that it works.

Replace the body of this program (the contents of the main function) with statements that will do each of the following. In each case, after making the change, run the program to see that it works.

Exercises

  1. Write a program that will read three integers and print the sum.
  2. Write a program that will read three integers and print the product.
  3. Write a program that will read the length and width of a rectangle (as floats) and print the area.
  4. Write a program that will read the radius of a circle and print its area. Use 3.14159 as the value of pi.
  5. Write a program that will read five numbers (floats) and print their average.
  6. Write a program that will read a first and a last name (two strings) and print them in reverse order, separated by a comma.
  7. Write a program that will read a number and print its square root. Test it with negative numbers as well as positive numbers.
  8. Write a program that will read a decimal number of hours (float) and print the whole number of minutes contained in those hours. For example, if the input is 1.125 hours, then the output will be 67 minutes.

Answers

  1. SumOfThreeInts.cpp
  2. ProductOfThreeInts.cpp
  3. RectangleArea.cpp
  4. CircleArea.cpp
  5. Average.cpp
  6. ReverseNames.cpp
  7. SquareRoot.cpp
  8. HoursToMins.cpp