About GBdirect Consultancy Training Development

Section navigation

Leeds Office (National HQ)

GBdirect Ltd
Leeds Innovation Centre
103 Clarendon Road
LEEDS
LS2 9DF
West Yorkshire
United Kingdom

consulting@gbdirect.co.uk

tel: +44 (0)870 200 7273
Sales: 0800 651 0338

South East Regional Office

GBdirect Ltd
18 Lynn Rd
ELY
CB6 1DA
Cambridgeshire
United Kingdom

consulting@gbdirect.co.uk

tel: +44 (0)870 200 7273
Sales: 0800 651 0338

Please note: Initial enquiries should always be directed to our UK national office in Leeds (West Yorkshire), even if the enquiry concerns services delivered in London or South/East England. Clients in London and the South East will typically be handled by staff working in the London or Cambridge areas.

1.7. Exercises

Exercise 1.1. Type in and test Example 1.1 on your system.

Exercise 1.2. Using Example 1.2 as a pattern, write a program that prints prime pairs — a pair of prime numbers that differ by 2, for example 11 and 13, 29 and 31. (If you can detect a pattern between such pairs, congratulations! You are either a genius or just wrong.)

Exercise 1.3. Write a function that returns an integer: the decimal value of a string of digits that it reads using getchar. For example, if it reads 1 followed by 4 followed by 6, it will return the number 146. You may make the assumption that the digits 0–9 are consecutive in the computer's representation (the Standard says so) and that the function will only have to deal with valid digits and newline, so error checking is not needed.

Exercise 1.4. Use the function that you just wrote to read a sequence of numbers. Put them into an array declared in main, by repeatedly calling the function. Sort them into ascending numerical order, then print the sorted list.

Exercise 1.5. Again using the function from Exercise 1.3, write a program that will read numbers from its input, then print them out in binary, decimal and hexadecimal form. You should not use any features of printf apart from those mentioned in this chapter (especially the hexadecimal output format!). You are expected to work out what digits to print by calculating each one in turn and making sure that they are printed in the right order. This is not particularly difficult, but it is not trivial either.