Main Content

C Course exercise and sample answer - source in 2 files

Archive - Originally posted on "The Horse's Mouth" - 2010-06-30 06:53:33 - Graham Ellis

Here's a sample exercise from the C course ... set yesterday to tailor the course for the particular group of delegates that we have this week:




/* Complete my program ...
... in 2 files
... fixed VAT rate at compile time
 
Function params - cost per item (net) and
no. of items at this cost
 
tc is gross.
 
*/
 
 
items = 0;
tc = 0.0
tc += vatable(17.6,2);
tc += vatable(18.4,4);
tc += vatfree(19.5,3);
 
printf ("............", items, tc);





You'll work out from the pseudocode that items needs to be a global variable - and that really the vatable and vatfree functions should be in a separate file. That brings function templates and extern into play in the example.

Solution - source code of main file [here] and of the the functions [here].

The compile line to build the application under gcc would be as follows:
  gcc -o xrcz xrcz.c taxi.c
or to separate out the compiles:
  gcc -c taxi.c
  gcc -c xrcz.c
  gcc -o xrcz xrcz.o taxi.o