CS2301 - D-term 13

Homework 1
Calculate the Circumference and Area of a Circle

Due: Tuesday, March 19 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...

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 circle.c that does the following:

The formula for determining the length lAB of the line between points (xA, yA) and (xB, yB) is

  _________________
 (xA -xB)2+(yA -yB)2 

Include files

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 circle.c

Assumptions and Restrictions

The user may enter any real or integer values for the x- and y-coordinates of the endpoints of the radius. You should use the pre-processing directive #define to define the value of pi as 3.14159. 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: -3.2
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: 6.7

Length of radius is 114.796249
Circumference is 721.285522
Area is 41400.433594

Investigating floating-point arithmetic in C

C supports two data types for floating point numbers: float and double. I made two versions of my circle.c program. In one version, all of the variables declared in the program were of type float. In the other version, all of the variables were of type double. When I run the two programs with the same inputs, they sometimes produce different outputs. For example, I ran both versions with endpoints (0,0) and (5,0) for the radius (i.e. the length of the radius was 5.0). The float version of the program calculated the circumference of the circle to be 31.415899. The double version of the program calculated the circumference to be 31.415900.

Read these Wikipedia entries on single-precision floating point format and double-precision floating point format. In your own words, explain why the two versions of my program gave me different answers for the circumference. Your explanation will be submitted as part of your README.TXT file (see next section, Deliverables).

Deliverables

Computer software is often distributed with a file called README.TXT that contains information about the installation and operating instructions for the software, known bugs, credits and acknowledgements, and other information. Write a short README.TXT file that summarizes your program, how to run it, and details any assumptions you made, if any (other than the assumption listed above) or problems you had. Also include in your README.TXT file your explanation of the differences between float and double. 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 March 19. The name of the turnin project is Homework 1. Programs submitted after 5pm on March 19 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: Points will be deducted for files not named as indicated above, and for missing names/usernames of author(s)