Next: A simple C++ program Up: Algorithms Previous: Review Questions

Exercises

  1. A program is required which will read in the breadth and height of a rectangle and which will output the area and the length of the perimeter of the rectangle. Write an algorithm for this problem.
  2. The requirement specified in question 1 is extended to include the requirement that if the entered breadth and height are equal then the output should be `the area and perimeter of the square are ...' whereas if they are not equal then the output should be `the area and perimeter of the rectangle are ...'. Alter the algorithm that you produced for question 1 to take account of this new requirement.
  3. A series of positive numbers are to be entered from the keyboard with the end of the series indicated by a negative number. The computer should output the sum of the positive numbers. Write an algorithm for this task. Use a while type of loop with the condition `number just entered is positive'. Think carefully about what initialisations are required before entering the while loop. Do a desk check of your algorithm with a small data set, say 3 positive numbers then a negative number. What would your algorithm do if the user entered a negative number first? Is what your algorithm would do in this circumstance sensible?
  4. Extend the algorithm in the previous question so that it also counts the number of numbers entered in the series.
  5. Another form of repetition statement is a repeat loop. This has the form:
    repeat
      statement 1.
          .
      statement n.
    until condition
    
    which executes the statements contained between repeat and until until the condition becomes true. For example to allow a user of a program the chance to re-run it with a new set of data a repeat loop might be used as follows:
    repeat
      enter and process data.
      ask if user wishes to process more data.
      read reply.
    until reply is no
    
    Now extend your algorithm for question 2 so that it repeats entering the dimensions of rectangles and calculating the results. After each calculation the user should be asked if they wish to continue.



Next: A simple C++ program Up: Algorithms Previous: Review Questions