Anda di halaman 1dari 2

School of Engineering

Computing for Engineers - Lab Sheet 2: Mathematical operations


Aim
To be able to implement most mathematical operations in C.
Objectives

Enhance understanding on variables, input and output in C (scanf, printf).


Understand arithmetic operators and their precedence/order of evaluation.
Understand the importance of comments in programs.

Assessment / submission procedures


A 50-word summary of your work within the lab session
must be uploaded to Canvas by 13:00 (end of the lab session).
A correctly submitted summary will count 1% towards the module mark.
You need not have finished all of the exercises within the two hours.
Email submissions will not be looked at.
If your lab group is working pairs,
then both lab partners should submit their own summary.
Relevant Lectures
Lecture 1, 2, 3.
Laboratory Work
A. Write a program to read in two integers a and b using scanf. Divide a by b and print out the result. Observer what
happens when you enter 100 and 30.
B. Repeat program A above, but using doubles instead of ints.
C. Write a program to enter an integer p. Display the remainder that you get when you divide p by 10.
Hint: use the modulus (%) operator to work out the remainder.
As a check, enter 512 should give 2, entering 70 should give 0.
D. Write a program that calculates the volume of a sphere using the following formulas:

V 4 r 3 / 3

where r is the radius of the sphere. Declare 3.14159265 . Your program should declare the variables as doubles
and read in the input value using scanf. Implement r3 as r*r*r.
E. Write a program using the following formula to convert a temperature reading in degrees Fahrenheit to degrees
Celsius:

C (5 / 9)*( F 32)

Where F represents the variable for the Fahrenheit and C represents that variable of the calculated Celsius conversion.
Your program should allow the user to input temperature values in Fahrenheit.
If you enter the formula exactly as above, you will get 0.000000 as the answer, no matter what value you enter.
Identify what the problem is, and correct the problem.
Test your program with the following inputs in Fahrenheit: 212, 100, 32, 0, and -40.
F. Repeat program A above, but using the pow library function to calculate r3. You will need to include the math.h
header file. Check that you get the same answer as before.
G. In a right-angled triangle, the length of the hypotenuse is given by Pythagoras formula:

c a 2 b2
Write a program to enter a and b using scanf, and to display c. All variables should be declared as doubles.
Use the sqrt library function to work out the square root. As a check, entering 12 and 5 should give 13.000000.

If you have not finished the exercises, upload your summary and
then you should work on them in your own time, before the next lab session.

Anda mungkin juga menyukai