Computer Science 2530
Fall 2017
Programming Assignment 1

Assigned: Friday, September 1
Due: Wednesday, September 13, 11:59pm

Table of Contents

  1. Purpose of this assignment
  2. Background
  3. The assignment
  4. A refinement plan and additional requirements
  5. Compiling and running your program on xlogin
  6. Issues to be aware of
  7. Submitting your work
  8. Late submissions
  9. Asking questions

Purpose of This Assignment

The purpose of this assignment is to familiarize you with basics of C++ and to give you an exposure to following the requirements of this course.


Background

Suppose p1 = (x1, y1) and p2 = (x2, y2) are two points in the plane. The distance between p1 and p2 is
(x1x2)2 + (y1y2)2.


The Assignment

Write a complete C++ program called closest.cpp that reads three points p1 = (x1, y1), p2 = (x2, y2) and p3 = (x3, y3) from the standard input in the following format.

  x1 y1
  x2 y2
  x3 y3
There are two real numbers per line, giving the x- and y-coordinate of a point.

The program should show, on the standard output,

  1. the three points that were read,
  2. the two of the three points that are closest together, and
  3. the distance between those two closest points.

For example, if the input is

  1.0 3.0
  1.5 7.2
  4.0 2.0

then the output should be

The three points are:
     1.000      3.000
     1.500      7.200
     4.000      2.000

The closest two points are:
     1.000      3.000
     4.000      2.000

The distance between those two points is:      3.162

If two or more points are at the same distance from one another, and that distance is the shortest distance, then the program should show the output for them all. For example, on input

  0.0 1.0
  0.0 2.0
  0.0 3.0

the program should write

The three points are:
     0.000      1.000
     0.000      2.000
     0.000      3.000

The closest two points are:
     0.000      1.000
     0.000      2.000

The distance between those two points is:      1.000

The closest two points are:
     0.000      2.000
     0.000      3.000

The distance between those two points is:      1.000

Use output format %10.3lf to write each real number. (The last two characters of the format are lower-case ell and lower case eff.)


A Refinement Plan and Additional Requirements

Follow this plan to write this program.

Development plan

1. Create a directory (folder) to hold assignment 1. Put all of the files for this assignment in that directory.

2. Open a new file.. Copy and paste the template into it. Add your name and the assignment number. If you will use tabs, say how far apart the tab stops are. (If you don't know, type a line of about 8 x's. Then, in the line beneath that line, type a tab. How many x's were skipped over? That is how far apart the tab stops are.) Save the file as closest.cpp.

3. Write a comment describing what the program is will do when it is finished. Include a clear description of the input format, plus an example of the input and the output that it will lead to.

4. Write a function to echo the input. This function takes 6 parameters, x1, y1, x2, y2, x3 and y3, all of type double. It shows them in a format suitable for showing what the input is. This function does not return anything, so its return-type is void. Write a contract just above the function definition telling what this function does.

5. Write a main function that just reads the 6 real numbers and shows them on the standard output by calling the function from step 4. Test it before continuing to the next step.

6. Write a function distance(x1, y1, x2, y2), where the parameters all have type double. Expression distance(x1, y1, x2, y2) returns the distance between points (x1, y1) and (x2, y2).

Write a contract for distance just above the definition.

Modify your main program by making it show the distance between the first input point and the second one. Test it. Is the result correct? Do not countinue until it is correct.

7. Write a function showOutput(x1, y1, x2, y2, d) that has a void return type and that shows points (x1, y1) and (x2, y2) as the closest two points, and d as the distance between them.

Write a contract for showOutput just above its definition.

Modify your main program by making it show the first two points as the closest two points. Test it. Is the result correct? Do not countinue until it is correct.

8. Write a function consider(x1, y1, x2, y2, x3, y3) that shows that points (x1, y1) and (x2, y2) are the closest two of (x1, y1), (x2, y2) and (x3, y3), provided that they are the closest two. Function consider must use function showOutput to do the writing. If (x1, y1) and (x2, y2) are not the closest, then consider should not write anything.

Write a contract for consider just above its definition.

9. Modify main. Remove the test prints from prior tests and add three calls to consider, one with (x1, y1) and (x2, y2) as the first two points, another with (x1, y1) and (x3, y3) as the first two points and a third with (x2, y2) and (x3, y3) as the first two points.

Test your program. If it does not work, fix it.

10. Submit your program.


Compiling and Running Your Program on Xlogin

Get file Makefile and put it in the same directory as your program. Then use the following commands.

make
Just compile closest.cpp and create an executable file called closest. If there are errors, they are reported. No news is good news.

make run
Run executable file closest (compiling it first if it needs compiling).

make clean
Remove all machine-generated files. After you do this, the next time you do a make, closest.cpp will be recompiled.

Issues to Be Aware of

The program is required to follow the coding standards for this course. Pay attention to the following.

  1. Follow the instructions. Do not write this as a single monolithic main function. The design is not optional. You are required to write and use the indicated functions. A program that has only a main function will receive a grade of 0.

  2. Pay attention to contracts. Read about the requirements for them. Documentation matters. Make sure that each function does what its contract says it does.

  3. Put a blank line before and after each function contract.

  4. Avoid margin comments. Some students have learned to write comments in the right margin of each line explaining what that line does. Please do not do that.

  5. Function consider should not compute the distance between the same two points more than once. (Two separate calls to consider can recompute distances, but there should be no duplicate calls within a single call tok consider.)

  6. Do not use any global or static variables.

  7. Do not change the value of any parameter. If x1 is a parameter to a function then the function must not contain

      x1 = …
    
    or any other kind of statement that changes the value of x1.

  8. The last character that your program writes on the standard output should be an end-of-line character ('\n').

  9. The body of every if-statement must be a compound statement. That is, it must be surrounded by braces.

  10. Each pair of matching braces must be in the same column, with the right brace immediately below the matching left brace, and with no characters on or to the left of a line segment between the braces. An if-statement would be something like the following.

      if(…)
      {
        …
      }
    

Submitting Your Work

To submit your program, log into xlogin and change to the directory contains your work. Run command

  ~abrahamsonk/2530/bin/submit 1 closest.cpp
You should get a reply that the submission was successful. Command
  ~abrahamsonk/2530/bin/submit 1
lists the names of the files that you have submitted for assignment 1.

You can do repeated submissions. New submissions will replace old ones.


Late Submissions

Late submissions will be accepted for 24 hours after the due date. If you miss a late submission deadline by a microsecond, your work will not be accepted.


Asking Questions

To ask a question about your program, first submit it, but use assignment name q1. For example, use command

  ~abrahamsonk/2530/bin/submit q1 closest.cpp
Then send me an email with your question. Do not expect me to read your mind. Tell me what your question(s) are. I will look at the file that you have submitted as q1. If you have another question later, resubmit your new file as assignment q1 and send another email.