lab 3: area under a line

EN47/COMP9, Fall 2009

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

the problem

The goal of this lab is to write a program that computes the area under a number of line segments, keeping track of the average of the areas computed so far. The area under a line segment is really just the area of the trapezoid formed by the projection of the line segment onto the x-axis (as shown in Tuesday's lecture).

This assignment emphasizes the use of functions to increase the effectiveness of your code. You will write four functions:

  • void describe_program()
    A function that takes no arguments and describes what the program will do.
  • void give_directions()
    A function that takes no arguments and gives the user directions on how to specify a line segment.
  • float trapezoid(int base1, int base2, int height)
    A function that takes three int arguments (the two bases and the height) and returns a float (the area of a trapezoid).
  • int main()
    You should be very familiar with this function by now. Your program starts executing here, and you should use it to call your other functions to do the work.

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 under several line segments.

    Each line segment will be defined by its end points (left to right), 
    with each end point being specified by its integer coordinates. 
    This program will also keep track of the running average. 
    
    How many lines? 3

    When prompted by the line number, please give the x-coordinate and 
    y-coordinate of each of the two end points separated by a space, 
    one point per line, left point first. 

    Line #1?
    1 2
    3 6

    NUM AREA AVG
    1   8    8

    Line #2?
    2 3
    4 1

    NUM AREA AVG
    2   4    6

    Line #3?
    3 4
    4 7

    NUM AREA AVG
    3   5.5  5.83333

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 necessary variables.
  2. Initialize selected variables to zero.
  3. Describe what the program will do.
  4. Read in the number of lines. What should your program do if the number of lines is zero?
  5. Tell the user how to specify each line segment.
  6. For each line segment:

    1. Read in its coordinates.
    2. Find the area under this line segment.
    3. Calculate the total and average area of the lines seen so far.
    4. Print a report (using tabbed columns) of the current line number, the area under the line, and the average area so far.
  7. Report the total number of line segments 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 (mkdir lab3). cd into it. Create a file called lines.cpp (emacs lines.cpp &) and save the file.

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-lab03-template.txt might also be useful. It contains pre-formatted cout statements that you can copy into your describe_program() and give_directions() functions. You can download the file from the link above.

Some things to keep in mind:

  • In Emacs, use the TAB key to indent your code correctly. This will make your code much more readable and allow you to discover many errors yourself (without having to rely on confusing messages from the compiler).
  • This is the most complex program we've written so far. Be sure to include descriptive comments in your code (using "//"), and provide good directions for the user.
  • Once your program compiles correctly, try running it with the numbers shown in the example above. It should produce exactly the same numerical results.
  • Now run it with some different input, recording the input you gave it and the results your program reported. If the results you get don't match the correct results, you may need to debug your program.
  • When you are satisfied that your program is correct, hand it in using:

    provide comp9 lab3 lines.cpp

extra credit

Write an additional function called void print_banner() and use it to print a banner line before reading in each line segment. This function should use symbolic constants to determine the number of banner lines, the number of characters per line, and the character to print. For example, this banner consists of two lines of 50 dash characters:

    -------------------------------------------------- 
    -------------------------------------------------- 

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

provide comp9 lab3ec lines_extra.cpp