ECE-1021
HOMEWORK #A
(Last Mod: 27 November 2010 21:38:38 )
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.
double GetCOMPLEX_real(COMPLEX a);
double GetCOMPLEX_imag(COMPLEX a);
double SetCOMPLEX_real(COMPLEX *a, double real); //returns real
double SetCOMPLEX_imag(COMPLEX *a, double imag); //returns imag
You should then use these functions to create the following low level utility functions:
Re(a): returns the real part of the complex number a
Im(a): returns the imaginary part of the complex number a
cartesian(real, imaginary): returns a complex number given that number's real and imaginary components.
Mag(a): returns the magnitude of the complex number a
Arg(a): returns the arg (the angle in radians) of the complex number a
polar(magnitude, arg): returns a complex number given that number's magnitude and arg.
The functions listed in the problem should then be implemented using only the low level utility functions above.
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.