CS2301 - B-term 12
Homework 1
Calculate the Perimeter and Area of a Triangle
Due: Tuesday, October 30 at 5pm
Read the expectations on homework. If you have requested a homework partner, you and your partner
may work together on this and all subsequent homework assignments. Otherwise,
this and all other homework assignments are to be done individually. The names
and WPI usernames of the author(s) of all submitted files should be provided
at the beginning of each file.
Outcomes
After successfully completing this assignment, you will
be able to...
- Develop a C program on a Linux platform
- Use C's arithmetic operators to do calculations using floating point
arithmetic
- Use formatting strings and conversion specifiers to do I/O in a C program
Before Starting
Read Chapters 1 and 2, and Chapter 9, sections 9.1 - 9.5, 9.8, and 9.10 - 9.12
in Deitel & Deitel.
The Assignment
Write a C program called triangle.c
that does the following:
- prompts the user for the x- and y-coordinates of the first corner of
a triangle
- prompts the user for the x- and y-coordinates of the second corner of
the triangle
- prompts the user for the x- and y-coordinates of the third corner of
the triangle
- calculates and prints the lengths of the three sides of the triangle
- calculates and prints the perimeter of the triangle
- calculates and prints the area of the triangle
The formula for determining the length lAB of the line between
points (xA, yA) and (xB, yB) is
|
_________________
|
√
|
(xA -xB)2+(yA -yB)2
|
If lAB, lBC, and lCA are the lengths of the three
sides of a triangle, then the area of the triangle is
|
_____________________________
|
√
|
s * (s - lAB) *(s - lBC) *(s - lCA)
|
where s is half the perimeter of the triangle.
Include files
- stdio.h provides printf() and scanf()
- math.h provides sqrt(), pow()
The sqrt() function returns the square root of its argument. The argument may
be any non-negative numerical value, and the result returned by sqrt() is of
type double. If the argument is negative, sqrt() fails with an error and
the result is undefined.
The pow() function can be used for exponentiation. pow(x, y)
computes x
to the y
power. There are some subtleties
involved in using pow(), so an alternative way to square a number would be
to simply multiply the number by itself. Either way is acceptable for this
homework problem.
Note: When compiling a program that uses a function, such as sqrt()
or pow(),
from the math library, the compilation command needs to include the option
-lm
. For example:
gcc -Wall -lm hw1.c
Assumptions and Restrictions
The user may enter
any real or integer values for the x- and y-coordinates of each of the
three points of the triangle; you may assume that the three points entered
do indeed form a triangle (i.e. the three points may not all be on the same
line).
Do not
use loops, conditionals, arrays, user-defined functions (other than main) or any other constructs/statements
that haven't yet been covered in class.
Sample Execution
Enter the x-coordinate of point A: 1.05
Enter the y-coordinate of point A: 2
Enter the x-coordinate of point B: 1.115e+2
Enter the y-coordinate of point B: 21.1
Enter the x-coordinate of point C: -25
Enter the y-coordinate of point C: -3.14159265358979323846
Length of AB is 112.089
Length of BC is 138.636
Length of CA is 26.553
Perimeter is 277.278
Area is 35.181
Deliverables
Write a short text file called README.txt
that summarizes your program, how to run it,
and details any assumptions you made (other than the assumption listed above) or problems you had. (You can use kwrite to create
your README.txt
file.)
Make sure both your C source code file and your README.txt file contain
the name(s) and WPI username(s) of the author(s) of the files.
Submit your C source code file and your README.txt
file to
web-based turnin no later
than 5pm on October 30. The name of the turnin project is Homework 1.
Programs submitted after 5pm on October 30 will be tagged as late,
and will be subject to the late homework
policy.
Grading
This assignment is worth 15 points. Your program must compile without errors using the gcc compiler in order to receive
any credit. It is suggested that before you submit your program, compile it again one more time
to be sure it compiles correctly. The 15 points will be allocated as follows:
- Correct compilation without warnings - 2 points
- README.txt provided as required - 2 points
- Program documentation (comments), formatting, names of authors - 2 points
- Correct usage of scanf() to get inputs from user - 3 points
- Correct usage of printf() to print various lines of output - 3 points
- Correct answers with the grader's test cases - 3 points