CSCI 261 - Programming Concepts (C++)

Fall 2017 - Assignment 2 - Random Triangles

Quick Links: Canvas | CS @ Mines | Cloud9 | Piazza | zyBooks

|   Home |  Contact |  Syllabus |  Assignments |  Schedule |  Resources   |
This assignment is due by Tuesday, September 12, 2017 11:59 PM.

As you will learn, we LOVE triangles in this class. (And I particularly love them in the computer graphics class). Get ready to learn more than you'll ever want to about geometry and triangles.


Instructions: Part I - Classify Their Triangle



Write code to first read three real value measurements, i.e., your program should prompt the user to enter the three real values (which should be stored as three double variables). Once done, write code to determine whether the three real value measurements make a triangle. If yes, your program should tell the user whether the triangle is a right, acute, or obtuse triangle (see hints below). If no, your program should politely terminate with an appropriate error message.

The user should be allowed to enter the three real values in any order; in other words, your program should not assume any particular input order such as ascending or descending.


Hints


  • A triangle is possible IFF each side is smaller than the sum of the others.
  • To classify a triangle, first you need to determine which of the three sides is the longest. The variable names a, b, and c are often used for the sides of a triangle, with c being the longest. If c is the longest side of the triangle, then the triangle is a right triangle if (and only if) a2 + b2 == c2.
  • Due to the imprecise nature of double variables, you cannot directly compare two double variables with the == operator. Instead, you should do the comparison using a TOLERANCE constant (e.g., TOLERANCE == 0.0001) in the following way:
  • // The fabs function (defined in cmath) returns the absolute value of a given floating point number.
    // Thus, if the following equation is true, then we assume a2 + b2 == c2.
             fabs(a2 + b2 - c2) <= TOLERANCE

  • The TOLERANCE constant represents the error we are willing to accept when comparing two double values for equality; in other words, if two values are different by TOLERANCE (or less), then we consider the two values as equivalent. In your program, declare a TOLERANCE constant and set it to 0.0001.
  • You must check to see if the triangle is a right triangle first; otherwise you may be classifying a right triangle as acute or obtuse by mistake.
  • If the triangle is not right, you should then check whether if it is acute (i.e., a2 + b2 > c2).
  • Otherwise, if the triangle is not right or acute, then you can say the triangle is obtuse (i.e., a2 + b2 < c2)

Test Values



Here are some known input/output values that you can test against. Be sure to try your own to verify the code works!

Inputs Classification Purpose of Test
3 4 5 Right Known Right Triangle
5 12 13 Right Known Right Triangle
5 5 9 Obtuse Known Obtuse Triangle
5 5 1 Acute Known Acute Triangle
Sides are not in increasing order
Two sides share the longest length
1 1 1 Acute Known Acute Triange
All sides share the longest length
1 2 3 Not a Triangle Invalid triangle. a + b > c fails
3 4 0 Not a Triangle Invalid triangle. One side has a zero length
-3 -4 -5 Not a Triangle Invalid triangle. All sides have negative lengths (even though
a2 + b2 = c2 would still hold.)


Instructions: Part II - Triangle Stats



If the result of Part I determines we do have a valid triangle, then we will continue to print out some stats about our triangle. We first will want to print out the three sides in increasing order. Then, we will print out both the perimeter and area of the triangle.


Test Values



Here are some known input/output values that you can test against. Be sure to try your own to verify the code works!

Inputs Classification Perimeter Area Purpose of Test
3 5 4 Right 12 6 Known Right Triangle
Sides are not in increasing order
13 12 5 Right 30 30 Known Right Triangle
Sides are not in increasing order
5 5 9 Obtuse 19 9.8075 Known Obtuse Triangle
5 5 1 Acute 11 2.4875 Known Acute Triangle
Sides are not in increasing order
Two sides share the longest length
1 1 1 Acute 3 0.433 Known Acute Triangle
All sides share the longest length
1 2 3 Not a Triangle Invalid triangle. a + b > c fails
3 4 0 Not a Triangle Invalid triangle. One side has a zero length
-3 -4 -5 Not a Triangle Invalid triangle. All sides have negative lengths (even though
a2 + b2 = c2 would still hold.)


Instructions: Part III - Randomize The Triangle



Now for the tricky part. We are going to go back and edit what we did at the very beginning. Initially, the user was entering the length of the three sides. Instead, we want to ask the user what the maximum possible side length can be (assume it will be greater than 0 and less than 100). Call this value X. We then need our program to generate three random real values in the range of (0, X]. The rest of the program will then proceed as before using these randomly generated values.



Grading Rubric


Your submission will be graded according to the following rubric.

Points Requirement Description
2 All code submitted properly
8 Labs completed
+2 Lab 2D Extra Credit
2 In-class exercises completed
7 Triangle coordinates computed as random floating point values
6 Triangle properly classified
3 Triangle stats printed properly
2 (1) Comments used (2) Coding style followed (3) Appropriate variable names, constants, and data types used (4) Instructions followed
30 Total Points


Submission


Always, always, ALWAYS update the header comments at the top of your main.cpp file. And if you ever get stuck, remember that there is LOTS of help available.

From your Cloud9 workspace, right click on the A2 folder in your workspace tree. Select "Download" from the pop-up menu. This will download a file called A2.zip to your computer. It contains all the files of your A2 folder (therefore Lab2A, Lab2B, Lab2C, Lab2D, A2). Now in Canvas, go to Assignments > A2. Upload your A2.zip file you just downloaded. And voila! Easy peasy.



This assignment is due by Tuesday, September 12, 2017 11:59 PM.
Last Updated: 09/12/17 19:40


Valid HTML 4.01 Strict Valid CSS! Level Triple-A conformance, W3C WAI Web Content Accessibility Guidelines 2.0