TriFuncSpec

advertisement
CS 1044 Program 4
Fall 2009
Functions, Decision Logic, while loops
This programming assignment uses many of the ideas presented in the course notes up till now, so you are advised to read
those carefully. Read and follow the following program specification carefully. Your score on this project will be a
weighted average of the score you receive for runtime testing and the score you receive for following the instructions in the
Programming Standards section below.
The Program Specification:
Classifying Triangles
This programming project will consider some terms from plane geometry. Every triangle falls into exactly one of the
following three categories:
acute
right
obtuse
no angle is greater than or equal to a right angle
one angle is a right angle
one angle is greater than a right angle
Every triangle also falls into exactly one of the following two categories:
isosceles
scalene
two or more sides are equal (includes equilateral)
no two sides are equal
So, there are six possible classifications of a given triangle (any one of the first three, combined with either of the last two).
Your program must be able to determine which of those six classifications fits a triangle, given the lengths of its three sides.
In addition, your program must find the area of a triangle, given the three sides. Finally, your program must calculate the
largest angle in the triangle. For each triangle processed, you must print a line of output showing the classification, the
three sides, the area and the largest angle. See the sample output file below for an example.
Your program must also be able to tell whether three given numbers could be the (lengths of the) sides of a triangle. Given
three numbers that cannot form a triangle, your program should simply stop processing those values and proceed to the next
line of input. No error messages should be printed.
Input file description and sample:
Your program must read its input from a file named sides.dat — use of another input file name will result in massive
deductions. Each line of the input file will contain three integer values, separated by whitespace. Note that this may include
negative values and zero. For instance:
3 4
3 4
2 -3
5 5
3 4
1 2
4 4
4 4
3 4
5
8
1
5
0
3
6
5
6
Note that you must not make any assumptions about the number of lines of data in the input file. Your program must be
written so that it will detect the end of data and terminate correctly at that time. You may however assume that if a line of
data is present in the input file that it will contain exactly 3 integer values that can be represented by the range of the C++
int data type. The sides are given as integers in order to reduce problems with numerical accuracy.
Due: Check Course Web site
Page 1 of 5
CS 1044 Program 4
Fall 2009
What to Calculate:
Your program must read each line of input data, as described above, and calculate the value of the given expression. In
order to achieve the best possible accuracy, you are required to use variables of type double when calculating the area
and angle. When determining whether the triangle is acute, right, obtuse, scalene, and/or isosceles, you should use purely
integer calculations. Otherwise, the unavoidable roundoff errors could produce incorrect conclusions.
Output description and sample:
Your program must write all output data to a file named triangle.dat — use of any other output file name will result
in a massive deduction of points. The sample output file shown below corresponds to the sample input data given above:
Zaki Malik
Triangle Classification
Type
Sides
Area
Angle
---------------------------------------------------------Scalene
Right
3
4
5
6.00
1.57
Isosceles
Acute
5
5
5
10.83
1.05
Isosceles
Obtuse
4
4
6
7.94
1.70
Isosceles
Acute
4
4
5
7.81
1.35
Scalene
Obtuse
3
4
6
5.33
2.05
---------------------------------------------------------Maximum angle:
2.05
Maximum area:
10.83
You are not required to use this exact horizontal spacing, but your output must satisfy the following requirements:












The first line of the output file must contain your name.
The second line of the output file must contain the given title.
The third line of the output file must be blank.
You must use the specified column labels, in the fourth line of the output file.
The area and angle must be calculated using doubles.
All doubles must be formatted to show exactly 2 digits following the decimal point.
All ints must be formatted as integers, without a decimal point.
Allow sufficient space for each value your program prints; the angle values will always be between 0.00 and
3.15; the area values could be as large as 10000.00. Be careful not to run things together.
You must include divider lines as shown.
You must arrange your output in neatly aligned columns.
You must use the same ordering of the columns as shown here.
Do not insert any additional lines of output; do have a newline at the end of each line.
Programming Standards:
You'll be expected to observe good programming and documentation standards. All the discussions in class about
formatting, structure, and commenting your code will be enforced. A copy of Elements of Programming Style is included
with the course notes and is available on-line from the course Web site. Some specific requirements follow.
Documentation:
 You must include header comments specifying the compiler and operating system used and the date completed.
 Your header comment must describe what your program does; don't just plagiarize language from this spec.
 You must include a comment explaining the purpose of every variable or named constant you use in your program.
 You must use meaningful identifier names that suggest the meaning or purpose of the constant, variable, function,
etc.
Due: Check Course Web site
Page 2 of 5
CS 1044 Program 4



Fall 2009
Precede every major block of your code with a comment explaining its purpose. You don't have to describe how it
works unless you do something so sneaky it deserves special recognition.
Each user-defined function must have a valid C++ prototype and the definition of each user-defined function
(except main) must be preceded by a block comment, explaining in one sentence what the function does, and
listing each parameter to the function and explaining its purpose.
You must use indentation and blank lines to make control structures like loops and if-else statements more
readable. The code from Project 1 is a good guide.
Outline Design:
 You must include, below the header comments and pledge, an outline program design.
 The design must reflect your top-down functional/modular decomposition of the problem.
 The design should follow the layout of the design example in program 1.
Implementation:
 Use named constants instead of variables or literal constants where appropriate.
 There must be absolutely no file-scoped variables declared in your program!
 Use bool variables where appropriate.
 Do not use if…else statements, when a switch statement can be used.
 Choose your control structures appropriately. Do not use while loops for count controlled loops.
 Your implementation must minimally include four user-defined functions, not counting main.
 Function parameters should be passed by reference only when necessary — pass by value otherwise.
 Arrays may not be used in this program.
Functions:
Your program must be implemented with user-defined functions. You will also need to use some Standard Library
functions for some of the calculations. In particular, you must write and use at least six functions besides main (my
solution has twelve). There must be at least one function that returns an int value, at least one that returns a double
value and at least one that is void. Here are some suggestions for functions:

a function to open the files and enable manipulators for the output file

a function to determine if three given sides can form a triangle

functions to determine if a given triangle is acute, or obtuse, or right

a function to determine the area of a given triangle

a function to determine the largest angle in a triangle

a function to print the output file header

a function or functions to print the results for a triangle to the output file
As stated in class, the first function definition in your source code file must be for main(); every other function must have
a prototype and for this project the prototypes must all be file-scoped.
Testing:
At minimum, you should be certain that your program produces the output given above when you use the given input file.
However, verifying that your program produces correct results on a single test case does not constitute a satisfactory testing
regimen. You should make up and try additional input files as well; of course, you'll have to determine what the correct
output would be.
Your submission that receives the highest score will be graded for adherence to these requirements, whether it is
your last submission or not. If two or more of your submissions are tied for highest, the earliest of those will be
graded. Therefore: implement and comment your C++ source code with these requirements in mind from the
beginning rather than planning to clean up and add comments later.
Due: Check Course Web site
Page 3 of 5
CS 1044 Program 4
Fall 2009
Pledge:
Each of your submissions to the Curator must be pledged to conform to the Honor Code requirements for this course.
Specifically, you must include the following pledge statement in the header comment for your program:
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
On my honor:
- I have not discussed the C++ language code in my program with
anyone other than my instructor or the teaching assistants
assigned to this course.
- I have not used C++ language code obtained from another student,
or any other unauthorized source, either modified or unmodified.
- If any C++ language code or documentation used in my program
was obtained from another source, such as a text book or course
notes, that has been clearly noted with a proper citation in
the comments of my program.
- I have not designed this program in such a way as to defeat or
interfere with the normal operation of the Automated Grader.
// Pledge: On my honor, I have neither given nor received unauthorized
// aid on this assignment.
//
// <student's name>
Failure to include this pledge in a submission is a violation of the Honor Code.
Due: Check Course Web site
Page 4 of 5
CS 1044 Program 4
Fall 2009
A short trigonometry review:
Suppose a triangle has sides A, B and C, and assume C is the longest side, say:
C
A
B
Then:
Triangle is:
Acute
Right
Obtuse
If:
C2 < A2 + B2
C2 = A2 + B2
C2 > A2 + B2
The triangle shown above is obtuse. Make C a bit smaller and you get a right triangle. Make C smaller still and you get an
acute triangle. Strong advice: use these facts to test for acute, right or obtuse; don't depend on being able to calculate the
largest angle with enough accuracy to base your decision on that.
The area of a triangle with sides A, B and C is given by Heron's Formula:
Area  S ( S  A)( S  B)( S  C)
where
S  ( A  B  C) / 2
You'll probably want to use the standard library function sqrt( ) which takes one parameter, a double, and returns a
double. Be sure to include cmath.
Now consider the angle opposite the side C, labeled  in the drawing below.
C
A

B
The Law of Cosines states that:
C 2  A 2  B 2  2 AB cos
Solving for the angle  you get:
 A2  B 2  C 2 
  arccos

2 AB


where arccos is the inverse cosine function, which returns an angle between 0 and  (radians).
You'll probably want to use the standard library function acos( ) which takes one parameter, a double, and returns a
double. Again, be sure to include cmath. Note that this function returns the angle measured in radians, which is what we
want for this program. Be very careful using this function. The value you pass to acos( ) must be between -1.0 and
1.0; otherwise, you'll get a runtime error.
Due: Check Course Web site
Page 5 of 5
Download