CS 160 - Programming Concepts and Applications

Summer II 2018 - A3 - Multiple Random Triangles

Quick Links: Canvas | John Cabot | Piazza | zyBooks

|   Home |  Contact |  Syllabus |  Assignments |  Schedule |  Resources   |

This assignment is due by Thursday, July 12, 2018, 11:59 PM.

· Instructions · Rubric · Submission ·

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.


Hints



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 to enter the first two sides of a triangle. We then need our program to generate a third random real side length that results in a valid triangle. Given the hints above, what can the minimum and maximum possible values be? The rest of the program will then proceed as before using the randomly generated value.


Instructions: Part IV - Do It Again & Again!



For the last step, wrap the previous part to generate a random third side in a loop. First, ask the user how many random triangles they would like generated (we'll call this n). Then, generate n random values that could satisfy the length of the third triangle leg and print out the classification information for that triangle (area, perimeter, triangle type). An example run is below:

Enter the length of the first leg: 3
Enter the length of the second leg: 4
How many triangles do you want generated? 3

Triangle #01
------------
Legs: 3.000 4.000 5.000
Perimeter: 12.000
Area:       6.000
Class:      Right

Triangle #02
------------
Legs: 3.000 3.5000 4.000
Perimeter: 10.500
Area:       5.083
Class:      Acute

Triangle #03
------------
Legs: 2.500 3.000 4.000
Perimeter:  9.500
Area:       3.745
Class:      Acute

Please note the formatting (three decimal places, perimeter & area right aligned).


Grading Rubric


Your submission will be graded according to the following rubric.

PointsRequirement Description
2 All code submitted properly.
12 All labs completed and submitted
+2 Lab 3C Extra Credit completed.
5 Triangle side length computed as random floating point values.
3 Triangle properly classified.
3 n triangle sides printed as prompted by the user.
4 Triangle stats printed properly.
2 (1) Comments used
(2) Coding style followed
(3) Appropriate variable names, constants, and data types used
(4) Instructions followed
31 Total Points

This assignment is due by Thursday, July 12, 2018, 11:59 PM.


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. The following instructions are copied from How to Submit Homework.


It is critical that you follow these steps when submitting homework.

If you do not follow these instructions, your assignment will receive a major deduction. Why all the fuss? Because we have several hundred of these assignments to grade, and we use computer tools to automate as much of the process as possible. If you deviate from these instructions, our grading tools will not work. And that makes us very unhappy. And when we're unhappy, we give penalties. Thus, make us happy.


Submission Instructions



Here are step-by-step instructions for submitting your homework properly:
  1. File and folder names are extremely important in this process. Please double-check carefully, to ensure things are named correctly.
    1. The top-level folder of your project must be named Set3
    2. Inside Set3, create 7 sub-folders that are required for this Set. The name of each sub-folder is defined in that Set (e.g. L3A, L3B, L3C, L3D, L3E, L3F, and A3).
    3. Copy your program main.cpp and supporting files into the subdirectories of Set3 (steps 1-2), zip this Set3 folder (steps 3-4), and then submit the zipped file (steps 5-11) to Canvas.
    4. For example, when you zip/submit Set3, there will be 7 sub-folders called L3A, L3B, L3C, L3D, L3E, L3F, and A3 inside the Set3 folder, and each of these sub-folders will have a file called main.cpp and nothing else.

  2. You will need to download each project file(s) from Codenvy to your computer.

  3. Using Windows Explorer (not to be confused with Internet Explorer), find the file named "main.cpp" located inside the folder for the particular lab or homework assignment you will submit.

    STOP: Are you really sure you are viewing the correct assignment's folder?

  4. Now, for A3, right click on the main.cpp to copy the file. Then, return to the Set3/A3 folder and right click to paste the file. In other words, put a copy of your homework's main.cpp source code and supporting files into the Set3/A3 folder.

    Follow the same steps for L3A, to put a copy of your lab's main.cpp and supporting files into the Set3/L3A folder. Repeat this process for Set3/L3B, Set3/L3C, Set3/L3D, Set3/L3E, Set3/L3F.

    STOP: Are you sure your Set3 folder now has all your code to submit?

  5. Now, right-click on the "Set3" folder.
    1. In the pop-up menu that opens, move the mouse "Send to..." and expand the sub-menu.
    2. In the sub-menu that opens, select "Compressed (zipped) folder".

    STOP: Are you really sure you are zipping a Set3 folder with sub-folders that each contain a main.cpp file in it?

  6. After the previous step, you should now see a "Set3.zip" file.

  7. Now visit the Canvas page for this course and click the "Assignments" button in the sidebar.

  8. Find Set3, click on it, find the "Attach file" area, and then click the "Browse My Computer" button.

  9. Find the "Set3.zip" file created earlier and click the "Open" button.

    STOP: Are you really sure you are selecting the right homework assignment? Are you double-sure?

  10. WAIT! There's one more super-important step. Click on the blue "Submit" button to submit your homework.

  11. No, really, make sure you click the "Submit" button to actually submit your homework. Clicking the "Open" button in the previous step kind of makes it feel like you're done, but you must click the Submit button as well! And you must allow the file time to upload before you turn off your computer!

  12. Canvas should say "This assignment is complete. Click OK to review the results.". Click "OK" and view the files within the zip file you submitted. In other words, verify you submitted what you think you submitted!
In summary, you must zip the "Set3" folder and only the "Set3" folder, this zip folder must have several sub-folders, you must name all these folders correctly, you must submit the correct zip file for this homework, and you must click the "Submit" button. Not doing these steps is like bringing your homework to class but forgetting to hand it in. No concessions will be made for incorrectly submitted work. If you incorrectly submit your homework, we will not be able to give you full credit. And that makes us unhappy.


This assignment is due by Thursday, July 12, 2018, 11:59 PM.

Last Updated: 07/05/18 08:11


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