Back to DFS's C Page


OAC Problem Set IV

Structures

You are to write a program which reads four (4) pairs of coordinates from a file and determines whether the points they designate can be connected to form a rectangle. The points will be stored in the file in order around the perimeter.

Here are two sets of test data:

This set DOES define a rectangle. Save it as rect1.dat.

-2.0 0
2.0 4.0
4.0 2.0
0 -2.0

This set does NOT define a rectangle. Save it as rect2.dat.

-2.0 1.0
2.0 4.0
4.0 2.0
0 -2.0

To receive full marks, you will need to handle a file presented to you at the end of class.

  1. If you decide you need to use a square root function, you will need to put this compiler directive in your source file.
    #include <math.h>
    
    To properly compile and link your program, you have to specify that you want the math library used. You do this by including the
    -lm
    on the command line.
    gcc -lm -g -o rectcheck rectcheck.c
    
  2. To make testing your program easier, you may want to use a command line argument for the data file to be read in. Check the Command Line Basics page. The code will need to be modified slightly since you will be using only one argument besides the program name.
  3. There is more than one algorithm that can be used. At the beginning of your file, include a description of the algorithm you have chosen.
  4. The second sample program on the Structures page illustrates how to open a file and read data into an array of structures. It also provides a function which returns the slope of a line segment given the coordinates of its endpoints. The code may be of some assistance.
  5. Sample runs for the data provided above should be similar to the following:
    vir:~$ rectcheck rect1.dat 
    The points
    (-2.00, 0.00)
    (2.00, 4.00)
    (4.00, 2.00)
    (0.00, -2.00)
    DO define a rectangle.
    vir:~$ rectcheck rect2.dat 
    The points
    (-2.00, 1.00)
    (2.00, 4.00)
    (4.00, 2.00)
    (0.00, -2.00)
    do NOT define a rectangle.
    
    


Created 11 Jan 99
© DFStermole 1999