lab 4: areas of irregularly-shaped quadrilaterals

EN47/COMP9, Fall 2009

Out: October 8, 3:00pm
Due: October 15, 3:00pm

the problem

In this lab, you will write a program to compute the area of a number of quadrilaterals. Your program should keep track of the maximum area, the minimum area, and the running average of all areas computed so far. The user will specify the quadrilaterals by the x-coordinates and y-coordinates of their vertices in clockwise order around the boundary.

As in the previous lab, you will write several functions:

  • void describe_program()
    takes no arguments and describes what the program does.
  • void give_directions()
    takes no arguments and gives directions on how to specify a line segment.
  • float trapezoid(int base1, int base2, int height)
    takes three arguments (the two bases and the height) and returns the area of that trapezoid. This can be the function you defined in Lab 3.
  • float min(float a, float b)
    takes two numbers and returns the smaller one.
  • float max(float a, float b)
    takes two numbers and returns the larger one.

The user interaction with your program should look something like below. The underlined text is entered by the user.

    This program will compute the areas of several 4-sided gardens
    given the integer coordinates of the vertices of each garden in
    clockwise order around the boundary of the quadrilateral.
    It will also keep the running average, max, and min garden size.

    How many gardens? 2

    For each of the quadrilateral gardens, please give the x-coordinate 
    and y-coordinate of each of the four vertices separated by a space, 
    one vertex per line

    Garden #1?
    1 3
    5 7
    11 1
    3 1

    NUM  AREA  MIN  MAX  AVG
    1    32    32   32   32

    Garden #2?
    5 7
    8 14
    14 5
    11 1

    NUM  AREA MIN  MAX  AVG
    2    55.5 32   55.5 43.75

    The area of 2 gardens was computed.

In the case of zero gardens, simply print "No area was computed" and end the program.

the algorithm

Here is one possible approach to this problem. You are welcome to use this as a guide in developing your solution.

  1. Declare variables
  2. Initialize selected variables to zero
  3. Instruct the user
  4. Get the number of gardens
  5. For each garden from the first to the last:
    1. Read in its coordinates
    2. Find the area by summing the areas under all 4 line segments of the quadrilateral garden
    3. Calculate the average area for the gardens seen so far
    4. Update the min-area and max-area for the gardens seen so far
    5. Print a report
  6. Report the total number of gardens processed

After reading the problem description and sample algorithm, work out your approach on paper first. You are encouraged and expected to collaborate with classmates on this part.

the program

When you are finally ready to start writing code, create a directory for today's lab and a file called quads.cpp. Feel free to copy your code from the last lab into this directory, if you think it would be useful to have as a reference. The file comp9-lab04-template.txt might also be useful. It contains pre-formatted statements that you can copy into your functions. You can download the file from the link above.

  • Indent your code and use descriptive comments to make your code more readable and easier to debug.
  • Once your program compiles correctly, run it with the numbers shown in the example. It should produce exactly the same numerical results.
  • Now try testing your code with different combinations of the following quadrilaterals.

    • Q1: (1,6) (2,8) (3,6) (2,4)
    • Q2: (5,6) (6,8) (8,5) (8,4)
    • Q3: (10,6) (10,8) (13,7) (13,6)
    • Q4: (1,1) (2,2) (6,3) (7,2)
    • Q5: (10,1) (9,3) (12,2) (11,1)

    Make up some of your own quads and check that the results of your program match those you compute by hand. In any of your tests, if you encounter any unexpected output, record the input you gave and the results your program reported. Submit a copy of this with your printout.

When you are satisfied that your program is correct, hand it in using:

provide comp9 lab4 quads.cpp

extra credit

Modify your program to produce the correct quadrilateral area regardless of whether the vertices are entered in clockwise or counter clockwise fashion. There are easy and hard ways of doing this! Try to find the easy way.

Write your code in a new file named quads_extra.cpp. When you are satisfied with your extra credit program, submit it using this command:

provide comp9 lab4ec quads_extra.cpp