Computer Science 2530
Spring 2019
Programming Assignment 1

Assigned: Thursday, January 17
Due: Friday, January 25, 11:59pm
Points: 300

Table of Contents

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

Because contracts are provided for you, the contract bonus is not available for this assignment.


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 does the following.

  1. First it reads three points in the plane 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 in the plane.

  2. 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 or change the intent of those functions.

Do not use arrays or call-by-reference for this assignment. Do not use the Standard Template Library or default parameters for any assignments.

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


A Template

You will need to write the following functions, following the plan in the next section. For each function, there is a contract and a heading. Copy the following into your program. Replace ***your name*** by your name and indicate how far apart the tab stops are.

// CSCI 2530
// Assignment: 1
// Author:     ***your name***
// File:       closest.cpp
// Tab stops:  ***

// This program reads three points (x1,y1), (x2,y2) and (x3,y3)
// in the plane from the standard input, in format
//
//   x1  y1
//   x2  y2
//   x3  y3
//
// Then it writes, to the standard output, the two of those points
// that are closest.  If there are two or more equally close pairs,
// then it shows all closest pairs.

#include <cstdio>
#include <cmath>
using namespace std;

// Echo writes points (x1,y1), (x2,y2) and (x3,y3), as the
// original three points.

void echo(double x1, double y1,
          double x2, double y2,
          double x3, double y3)
{
}

// Distance returns the distance between the points (x1,y1) and (x2,y2).

double distance(double x1, double y1, double x2, double y2)
{
  // stub:
  return 0;
}

// ShowClosest writes that points (x1,y1) and (x2, y2)
// are the closest points, and the distance between them
// is d.

void ShowClosest(double x1, double y1, double x2, double y2, double d)
{
}

// If (x1,y1) and (x2,y2) are a closest pair among points
// (x1,y1), (x2,y2) and (x3,y3), then 'consider' writes that
// they are closest points.
//
// Otherwise, 'consider' does not write anything.

void consider(double x1, double y1,
              double x2, double y2,
              double x3, double y3)
{
}

int main()
{
  return 0;
}

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. Start by getting files Makefile and dotest put them in that directory.


2. Open a file in a text editor.

Copy and paste the above template into the file. Add your name.

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.) If you will not use tabs, say none after tabs:. But be sure that, if you say you don't use tabs, you really don't use tabs.

Save the file as closest.cpp.


3. Define function 'echo' by filling in its body.

Make 'echo' show the three input points in a readable format. Be sure that the output clearly labels them as the input points.


4. Write part of 'main'.

Make 'main' read the 6 real numbers and show them on the standard output by calling 'echo'. Test your program Test it before continuing to the next step.


5. Define function 'distance'.

Make 'distance' do what its contract says it does.

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


6. Modify your 'main' function.

Make 'main' show the distance between (x1, y1) and (x2, y2). Test your program. Is the result correct? Do not countinue until it is correct.


7. Define 'showClosest'.

Make 'showClosest' show points (x1, y1) and (x2, y2) as the closest, and show d as the distance between them. 'ShowClosest' is not responsible for determining whether (x1, y1) and (x2, y2) are closest. Its job is just to write that they are.


8. Modify 'main' again.

Make 'main' show (x1, y1) and (x2, y2) as the closest points by calling showClosest. Don't be worried that those two points aren't necessarily closest.

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.


9. Define 'consider'.

Make 'consider' do what its contract says it does.


10. Modify 'main' again.

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 your program does not work, fix it.


11. Submit your program.


Compiling and Running Your Program on Xlogin

If you haven't already, 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 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. Check the results. Are they correct?

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. Indent well throughout. A very poorly indented program will receive a failing grade.

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

  4. No function body (not counting the heading) should have more than 16 noncomment lines.

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

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

  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.

    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.