Computer Science 2530
Fall 2018
Programming Assignment 1

Assigned: Friday, August 31
Due: Monday, September 10, 11:59pm

Table of Contents

  1. Purpose of this assignment
  2. Background
  3. The assignment
  4. Additional requirements
  5. A refinement plan
  6. Compiling and running your program on xlogin
  7. Issues to be aware of
  8. Submitting your work
  9. Late submissions
  10. 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.

Read the entire assignment before you start writing anything.

Be sure to follow the assignment. Particular functions are called for. Write those functions.


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.)


Additional Requirements

The next section lists functions that you must define. You are required to define exactly those functions. Do not add or remove parameters.

Do not use arrays or call-by-reference for this assignment.

A program that wholly ignores the instructions will receive a grade of 0, even it it works correctly.


A Refinement Plan

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 in a text editor.

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 will do when it is finished.

Write the comment just below the heading information that is part of the template. 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 definition of a function to echo the input.

This function must take exactly 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 accomplishes. Be sure that the contract says how each parameter affects what this function accomplishes. Refer to each parameter by its name, never by a pronoun or phrase such as "the number."

Do not explain the function heading, or list the types of the parameters, in the contract. Be sure that the contract is not concerned with how this function works, or with where its parameters come from, but is only concerned with what this function accomplishes and how its parameters affect it.

5. Write a main function.

You will find a heading for main in the template. Make main read the 6 real numbers and show them on the standard output by calling the function from step 4. Test it before continuing to the next step.

6. Write a definition of function distance(x1, y1, x2, y2).

This function must take exactly 4 parameters, all of type double. Expression distance(x1, y1, x2, y2) must return the distance between points (x1, y1) and (x2, y2) in the plane.

Write a contract for 'distance' just above its definition saying what 'distance' accomplishes. Be sure that the contract says how each parameter affects what this function accomplishes. Refer to each parameter by its name, never by a pronoun or phrase such as "the number." To say what the function returns, use word "return", not "find" or "get" or any other word.

Do not explain the function heading, or list the types of the parameters, in the contract. Be sure that the contract is not concerned with how this function works, or with where its parameters come from, but is only concerned with what this function accomplishes and how its parameters affect it.

Note. Any time any function other than 'distance' needs to know the distance between two points, it must use 'distance' to get that information.

7. Modify your main function.

Make main 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.

8. Write a definition of function showOutput(x1, y1, x2, y2, d).

ShowOutput must take exactly 5 parameters, all of type double, and must have a void return-type. It must write points (x1, y1) and (x2, y2) as the closest two points, and d as the distance between them.

It is not showOutput's responsibility to determine whether (x1, y1) and (x2, y2) are the closest points. It can't possibly do that, since it does not know what x3 and y3 are. ShowOutput's responsibility is only to write that (x1, y1) and (x2, y2) are the closest two points, in a form that matches the required output format.

Write a contract for 'showOutput' just above its definition that says what showOutput accomplishes. Be sure that the contract says how each parameter affects what this function accomplishes. Refer to each parameter by its name, never by a pronoun or phrase such as "the number."

Do not explain the function heading, or list the types of the parameters, in the contract. Be sure that the contract is not concerned with how this function works, or with where its parameters come from, but is only concerned with what this function accomplishes and how its parameters affect it.

9. Modify your main function.

Make main show the first two points as the closest two points. Test your program. Does it show the first two points as the closest? Do not countinue until it is works correctly, according to what it is designed to do at this step.

10. Write a definition of function consider(x1, y1, x2, y2, x3, y3).

This function must take exactly 6 parameters, all of type double. It considers one possibility: are points (x1, y1) and (x2, y2) the closest two points of (x1, y1), (x2, y2) and (x3, y3)? If they are, then it should write that (x1, y1) and (x2, y2) are the closest two points (by calling 'showOutput'). If (x1, y1) and (x2, y2) are not the closest two points, 'consider' must not write anything.

Write a contract for 'consider' just above its definition. The contract must say exactly what 'consider' accomplishes, and how each of its parameters affect what it accomplishes, without saying how 'consider' works or where its parameters come from. To say what the function returns, use word "return", not "find" or "get" or any other word.

11. 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. (See make test below.) If it does not work, fix it.

10. Submit your program.


Compiling and Running Your Program on Xlogin

Get files Makefile and dotest and put them in the same directory as your program. Be sure that Makefile is called Makefile, not Makefile.txt or Makefile.cpp or MAKEFILE. File dotest must be called, exactly, "dotest".

After getting Makefile and dotest, do command

  ls
and make sure that those files are listed. Then do command
  chmod u+x dotest
to make dotest executable. Then you can use the following commands in a terminal window.

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 test
Run executable file closest (compiling it first if it needs compiling) on some examples that are provided. This does automated testing.

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. Avoid long lines. Limit lines to about 80 characters.

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

  3. Indent well throughout.

  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. No function body (not counting the heading) should have more than 16 noncomment lines.

  6. Read what 'consider' is supposed to do, and make it do that. Do not try to improve on it. This is partially an exercise to see whether you can follow instructions.

  7. 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 to 'consider'.)

  8. Do not use any global or static variables. All variables must either be parameters or created inside a function.

  9. 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.

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

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

    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 matching 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.