CMPT 130 Assignment 1 - Triangles

 

For this assignment you are to create a program to print data relating to a triangle, the side lengths of which are entered by the user. Your program will be marked by compiling it using g++ on Linux; therefore you should test your programs with this compiler and in the Linux OS before submitting them. If you fail to do this, and if your program does not compile it will not be marked. This assignment is worth 5% of your final grade.

 

Please make sure that you review the marking scheme so that you know what we are expecting.

 

Description

Write your program in a file called triangle.cpp. 

 

Your program should:

§  Prompt the user to enter three positive real numbers (that you should store in variables of type float) which represent the three sides of a triangle

§  Print each of the values entered by the user (often referred to as echoing the input)

§  Calculate the perimeter of the triangle

§  Calculate the area of the triangle: area = root(s(s-a)(s-b)(s-c)) where s is the semi-perimeter (perimeter/2) of the triangle and a, b and c are the lengths of its sides

§  Determine if the three sides could form a triangle; if the triangle's area as calculated above is 0 or negative the sides cannot belong to a triangle. If this is the case, print a message indicating that the sides cannot form a triangle and exit the program. Otherwise:

§  Print the perimeter of the triangle

§  Print the area of the triangle

§  Print a message indicating whether or not the triangle is an equilateral triangle – all three sides are equal

§  Print a message indicating whether or not the triangle is an isosceles triangle – any two sides are equal

 

Output Format

Your output should be formatted as shown in the example below. User input is shown in green. Note that the user can enter any values for the coordinates (not just those shown in the sample).

 

Sample Run 1

TRIANGLES

 

Enter length of first side: 5

Enter length of second side: 5

Enter length of third side: 5

 

You entered sides of length 5, 5 and 5

 

perimeter = 15

area = 10.8253

The triangle is an equilateral triangle

The triangle is an isosceles triangle

 

Sample Run 2

TRIANGLES

 

Enter length of first side: 3.1

Enter length of second side: 4.2

Enter length of third side: 5.7

 

You entered sides of length 3.1, 4.2 and 5.7

 

perimeter = 13

area = 6.37683

 

Sample Run 3

TRIANGLES

 

Enter length of first side: 3

Enter length of second side: 7

Enter length of third side: 3

 

You entered sides of length 3, 7 and 3

These three side lengths cannot form a legal triangle

 

Implementation Requirements

 

Program Structure

You are not expected to write functions, other than a single main function, for assignment 1.

 

Input and Error Handling

§  The program is not expected to handle negative side lengths

§  The program is not expected to handle non-numeric side lengths

§  You are not responsible for handling input that does not comply with these requirements; that is, your program does not have to handle input errors (such as the user entering non-numeric input or negative values)

§  You are expected to handle valid input where the sides could not form a legal triangle – as discussed in the requirements

 

Output Format

Your output should be in the same format as the example shown above.

 

File Name

Write your program in a file called triangle.cpp.

 

Calculations

§  If you want more information about triangles it is easily available online (i.e. look it up, don't email me J)

§  You may use the square root (sqrt) function from the cmath library

 

Determining Legal Triangles

There may run into a complication with determining if the three sides form a legal triangle. If you try to calculate the square root of a negative number, the sqrt functions returns NaN (short for not a number) which will be stored in whatever variable you assign the result of the sqrt function to.

 

Any numerical comparison with NaN returns false. So, for example (3 > NaN) is false and (3 <= NaN) is also false. Fortunately, you can test for whether or not a variable contains NaN with the cmath library function isnan, as demonstrated in the example below.

 

double x = sqrt(-1);

if(isnan(x)){

cout << x << " - x is not a number";

}

 

Assessment

The assignment is out of 20.  Marks are assigned as follows:

§  Echoing input for the three lines – 3 marks

§  Printing the correct perimeter of the triangle – 2 marks

§  Printing the correct area of the triangle – 3 marks

§  Printing whether the triangle is equilateral – 1 mark

§  Printing whether the triangle is isosceles – 2 marks

§  Identifying that the lines can form a legal triangle – 2 marks

§  Output format – 1 mark

§  Correct indentation – 2 marks

§  Comments – 2 marks

§  Variable naming – 2 marks – hint: don't use single letters such as a, b,c and s as variable names

 

Assessment Notes

If your program does not compile, it will not be marked. There are a few reasons why your program might not compile:

§  It contains compilation errors that you have not fixed – make sure that you fix any compilation errors before submitting your program, if you cannot get one part of the assignment to work then remove it (or comment it out) so that we can mark the rest of your assignment

§  Your program is not compliant with the g++ compiler – make sure that your program compiles on a Linux machine using g++ before submitting it

§  You have included libraries that are not standard C++ libraries – for assignment 1 you should only include the iostream and cmath libraries; to reiterate the above point, makes sure that it is compliant before submitting it

 

Submission

You should submit your assignment online to the CoursSys submission server.  You must submit a single .cpp file, please read the documentation on the Coursys site for further information.   The assignment is due by 11:59 pm on Wednesday the 26th of September.

 

 

CMPT 130 Home

 

John Edgar (johnwill@sfu.ca)