ECE-1021

HOMEWORK #A

(Last Mod: 27 November 2010 21:38:38 )

ECE-1021 Home


PROGRAM A - Complex Math

Programming Problem #10.3 (p545).

As the problem states, you are to use the structure that is defined in Section 10.2. But it is important to recognize that Section 10.2 implements these various operations as macros and you are being asked to implement them as functions. Functions are not macros - you can determine what each operation is by studying the macros in Section 10.2 and get a good idea of how to implement them as functions, but don't assume you can copy them unaltered and expect them to work.

It is also important to recognize that the function descriptions in Problem 10.3 imply certain things. First and foremost, they imply that the structures will be passed to these functions by value and that the functions, where appropriate, return them by value. This is important because some of these functions, such as add_c(), have to return "new" complex values.

You are to implement these functions using the concept of "utility" and "primitive" functions. In this context, a primitive is a basic function that is allowed to directly access, either for read or write, the contents of a structure. These are sometimes referred to as "GetSet" functions because they usually appear in pairs - one of which gets the contents of a specific member element of the structure and another which sets that same element. The idea is that the primitives form an input/output interface to the structure.

For the structure as given in Section 10.2, you should have four GetSet functions with the following prototypes:

Use the typedef statement (Section 10.3) to create a type called COMPLEX.

You should then use these functions to create the following low level utility functions:

The functions listed in the problem should then be implemented using only the low level utility functions above.


Extra Credit

After you have gotten the above working (and tested!) perform the following:

Create an alternate version of the structure definition that uses the elements mag and arg to store the complex number (i.e., store the complex number in polar form) and the write a set of GetSet functions for this version. Finally, write versions of your utility functions based on these new GetSet primitives. The programmer should be able to select which versions are used by defining or undefining the symbolic constant COMPLEX_POLAR.

The idea here is that you have two sections of code. Both contain a structure definition, a set of primitive functions, and a set of utility functions. But only one section will be compiled and which section gets compiled is controlled by whether the symbolic constant COMPLEX_POLAR is defined or not. See Section 5.6 for information on the preprocessor directives #ifdef, #else, #endif.

The rest of the functions should not have to be touched - at all - in order to work properly. If they do, then they are not using the concept of primitive/utility functions in an acceptable manner.