Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CSCI 2530 assignment, please help! The purpose of this assignment is to familiarize you with basics of C++ and to give you an exposure to

CSCI 2530 assignment, please help!

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 (x1 x2)2 + (y1 y2)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.

 x1y1x2y2x3y3

There are two real numbers per line, giving the x- and y-coordinate of a point.

The program should show, on the standard output,

the three points that were read,

the two of the three points that are closest together, and

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

Avoid long lines. Limit lines to about 80 characters.

Put a blank line before and after each function contract.

Indent well throughout.

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.

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

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.

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

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

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.

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

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() {  } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Database And Knowledge Base Systems

Authors: S Krishna

1st Edition

9810206208, 978-9810206208

More Books

Students also viewed these Databases questions

Question

What is strategy execution?

Answered: 1 week ago

Question

Describe the sources of long term financing.

Answered: 1 week ago