CS2301 - B-term 11
Homework 1
Calculate the Perimeter and Area of a Triangle
Due: Tuesday, November 1 at 5pm
Read the expectations on homework. You will be doing this assignment
and all subsequent homework assignments individually.
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 of Kernighan & Ritchie. Details about scanf() and
printf() can be found in sections 7.2 and 7.4.
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()
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.
Assumptions and Restrictions
There are no restrictions on the input data; that is, the user may enter
any real or integer values for the x- and y-coordinates of each of the
three points of the triangle.
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 detailing any assumptions and any problems you had. (You can use kwrite to create
your README.txt file.)
From your Linux account, submit your C source code file and your README.txt file using the following
turnin command:
/cs/bin/turnin submit cs2301 PROJECT1 triangle.c README.txt
Programs submitted after 5pm on Nov 1 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 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.
- Correct compilation without warnings - 2 points
- README.txt provided as required - 2 points
- Program documentation (comments) and formatting - 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