You are on page 1of 2

ESC101: Fundamentals of Computing Lab 3 (August 19-22, 2013)

Duration: 3 hours

Total Marks: 20

1. Tables [Contd]: Write another program by modifying your program in Lab 2 so that it also works for real numbers. However, while printing the real values, make sure that there are no more than 2 digits after decimal as shown below
$./a.out Enter the number: 0.25 0.25 x 1 = 0.25 0.25 x 2 = 0.50 0.25 x 3 = 0.75 0.25 x 4 = 1.00 0.35 x 5 = 1.25

[Total 6 marks] 2. Type Casting: Write a program that takes as input a double number (say 'x') and does the following (a) (b) (c) (d) Typecast it into an integer variable 'a' and print it. Typecast 'a' back into double and print it. Typecast it into an oat variable 'b' and print it. Typecast 'b' back into double and print it. (1 mark) (1 mark) (1 mark) (1 mark)

Here is a sample interaction of the program.


$./a.out Enter the double value (x): 20.3 Value of 20.300000 when typecasted in integer is: 20 Value of 20 when typecasted back to double is: 20.000000 Value of 20.300000 when typecasted in float is: 20.299999 Value of 20.299999 when typecasted back to double is: 20.299999

[Total 4 marks] 3. Functions: Dene two functions, mysin() and mycos(), to compute the sine and cosine of an angle (provided in degrees). Your program should rst ask the user to present a choice:
On an input "1", the program should read an integer (value of an angle in degrees) and should call the function mysin() to print the value of sine of that input using the following expansion:

(3marks) On the input "2", it should read an integer (value of an angle in degrees) and call a function mycos() to print the value of cosine of that input using the following expansion: (3 marks)

You can ignore the terms after the third term in the expansions. Also, see the dierence in your implementation using the inbuilt sin(x) and cos(x) functions in the math.h library. (2 marks)
Note: The argument to these library functions (in math.h) and the variable in the expansions is in radians. Use the following for the conversion:
angle in radian = (angle in degrees * (PI))/180 Use the value of PI to be 3.14159

(2 mark)
IMPORTANT: In this part of the lab assignment, use the following command to compile your code:
$gcc prog.c -lm

Here are some sample interactions of the program:


$./a.out Enter 1 or 2: 1 Enter the angle in degrees: 30 30 degrees in radian is: 0.523598 Sine of 30 degrees is: _____ Sine of 30 degrees by math.h library: 0.500000 $./a.out Enter 1 or 2: 2 Enter the angle in degrees: 30 30 degrees in radian is: 0.523598 Cosine of 30 degrees is: ____ Cosine of 30 degrees by math.h library: 0.866026

[Total 10 marks]

You might also like