   Next: Conditionals Up: Basic C Programming Previous: Order of Precedence

# Exercises

Write C programs to perform the following tasks. Use CEILIDH (See Appendix ) to input and run programs if you wish. Use CEILIDH also to test and mark your program.

The location of the exercise in ceilidh is given in () at the end of each question

1. Input n (a positive integer) numbers and work out their sum, average and sum of the squares of the numbers.

">

2. Input and output your name, address and age to an appropriate structure.

">

3. Write a program that works out the largest and smallest values from a set of 10 inputted numbers.

">

4. Write a program to read a "float" representing a number of degrees Celsius, and print as a "float" the equivalent temperature in degrees Fahrenheit. Print your results in a form such as

100.0 degrees Celsius converts to 212.0 degrees Fahrenheit.

">
(unit1:introduction:c-f).

5. Write a program to print several lines (such as your name and address). You may use either several printf instructions, each with a newline character in it, or one printf with several newlines in the string.

">
(unit1:introduction:mlp).

6. Write a program to read a positive integer at least equal to 3, and print out all possible permutations of three positive integers less or equal to than this value.

">
(unit1:introduction:prn).

7. Write a program to read a number of units of length (a float) and print out the area of a circle of that radius. Assume that the value of pi is 3.14159 (an appropriate declaration will be given you by ceilidh - select setup).

Your output should take the form: The area of a circle of radius ... units is .... units.

If you want to be clever, and have looked ahead in the notes, print the message Error: Negative values not permitted. if the input value is negative.

">
(unit2:basics:ccl)

8. Given as input a floating (real) number of centimeters, print out the equivalent number of feet (integer) and inches (floating, 1 decimal), with the inches given to an accuracy of one decimal place.

Assume 2.54 centimeters per inch, and 12 inches per foot.

If the input value is 333.3, the output format should be:

333.3 centimeters is 10 feet 11.2 inches.

">

(unit2:basics:cms).

9. Given as input an integer number of seconds, print as output the equivalent time in hours, minutes and seconds. Recommended output format is something like

7322 seconds is equivalent to 2 hours 2 minutes 2 seconds.

">

(unit2:basics:hms).

10. Write a program to read two integers with the following significance.

The first integer value represents a time of day on a 24 hour clock, so that 1245 represents quarter to one mid-day, for example.

The second integer represents a time duration in a similar way, so that 345 represents three hours and 45 minutes.

This duration is to be added to the first time, and the result printed out in the same notation, in this case 1630 which is the time 3 hours and 45 minutes after 12.45.

Typical output might be

```
Start time is 1415.
Duration is 50.
End time is 1505.

There are a few extra marks for spotting.

Start time is 2300.
Duration is 200.
End time is 100.```

">

(unit2:basics:tt).   Next: Conditionals Up: Basic C Programming Previous: Order of Precedence

Dave.Marshall@cm.cf.ac.uk
Fri May 20 13:40:49 BST 1994