Objectives

  • to practice creating functions
  • to separate the program and function code into different files
  • to test for an error condition from a function.

    Files

    You will need this lab2_solution.c file to use as a starting point for this lab. Download and save this file as lab4a.c, and modify it for part A. Then use your solution to part A as a starting point for part B.

    Specifications

    Specifications:

    This is a two-part lab intended to get you to first create a function out of existing code, and then move that function to a separate source code file. Start with the solution to lab2, and modify it so that the actual conversion is done in a function. You need a function called convert() that takes the 3 input variables (source currency, destination currency and source value) as parameters, and returns the converted value. If there was a problem with either the source or destination type, your function should indicate an error condition by returning a negative value. Your main() program gets the 3 input values from the user, calls the function, and then either outputs an error message or the converted value, whichever is appropriate.

    For part A of this lab, keep your function in the same file as the main() function, but placed below it. (You will therefore need to include a prototype for the convert() function above the main() function.) Name this file "lab4a.c".

    For part B of this lab, move your function to its own file, "utils.c". You will also have to create an associated "utils.h" file, complete with preprocessor directives so that it can only be processed by the compiler once no matter how many times you include it. Your header file is also the appropriate place for any currency conversion-specific constants! Name the program file as "lab4b.c", but keep the utility files as "utils.c" and "utils.h".

    Note that for both of these programs (parts A and B), the output is still exactly as specified for lab 2; the only difference is the organisation of the code, which should be completely transparent to the user.

    Please only use the given solution for lab 2 as the starting point, even if you got a good mark on your own solution. It will help keep the output consistent between all students, and make it so any small variations won't inadvertently create more work for you.

    Hints

    Handing In

    Hand in the all source code files, including .c files and the .h file That is, email lab4a.c, lab4b.c, utils.c and utils.h to rthorndy@gmail.com, with a subject header: "Lab 4 Submission".

    This lab is due before your next scheduled lab, on Wednesday February 11, at 3:30pm.

    Marking

    The lab will be marked out of 10 points:

    3 points: lab5a compiles and works.
    3 points: lab5b compiles and works.
    2 points: proper use of header file (including preprocessor directives) and prototypes
    2 points: proper use and placement of parameters, local variables and constants.