A complex number is a number that can be expressed in the form a + i b, where a and b are real numbers and i is the imaginary unit, which satisfies the equation i2 = −1.
In a + i b, a is called the real part and b is called the imaginary part of the complex number.
Complex numbers extend the concept of the one-dimensional number line to the two-dimensional complex plane by using the horizontal axis for the real part and the vertical axis for the imaginary part.
- (2 marks) Declare a tag named complex_tag for a structure with two members, real and imaginary, of type double.
- (2 marks) Define a type (using typedef) named Complex_type for a structure with two members, real and imaginary, of type double.
- (10 marks) Write a function that accepts two parameters of type complex_tag and returns a Complex_type of their multiplication.
Note that: if c1 and c2 are two complex numbers, where c1 = a1 + i b1 and c2 = a2 + i b2, then c1 × c2 = (a1 × a2 − b1× b2) + i (a2 × b1 + a1 × b2)
- (15 marks) Write a function that accepts three parameters of type pointer to complex_tag and returns an integer. The function will set the content of the third complex_tag pointer to be the division result of the content of the first two complex_tag pointers.
Note that: if c1 and c2 are two complex numbers, where c1 = a1 + i b1 and c2 = a2 + i b2, then
c1 ÷ c2 = (a1 × a2 + b1× b2) ÷ (a22 + b22) + i (a2 × b1 − a1 × b2) ÷ (a22 + b 0
Note that, if (a22 + b22) = 0, return -2, otherwise return 0.
- (20 marks) Write a function that accepts four parameters: two parameters of type complex_tag and the other two of type pointer to a pointer to complex_tag. The function returns an integer. The function allocates memory for two complex_tag variables using the malloc Make sure that the memory allocation operation is successful before using its output Otherwise, you need to print an error message and return -1. The value of one of these two allocated complex_tag variables will be the sum of the first two arguments, while the other will be the difference between them. A pointer to one of these two created structures will be returned to the caller via the third parameter, while the pointer to the other created structure will be returned to the caller via the fourth parameter. Upon successfully calculating the sum and difference, return 0.
Note that: if c1 and c2 are two complex numbers, where c1 = a1 + i b1 and c2 = a2 + i b2, then c1 + c2 = (a1 + a2) + i (b1 + b2) c1 ̶ c2 = (a1 ̶ a2) + i (b1 b̶ 2)
- (4 marks) Put the three functions in a file called c and their prototypes in a file called operation_functions.h.
- Write a main program (c) that
- (10 marks) Declares two variables of type complex_tag. The value of these two variables will be initialized using the command-line arguments as 4 separate values, two for each variable.
- (15 marks) Passes these initialized two structure variables (or a pointer to them, whenever appropriate) and whatever other arguments as needed to the three functions described above. Store the results in appropriate variables that you will also need to declare in the main function.
- (5 marks) Prints the entered complex numbers, as well as the results of multiplication, division, addition, and subtraction operations in an easy-to-ready format.
- (8 marks) Write an appropriate makefile that compiles and tests your program.
- (2 marks) Provide a readme file that will tell how to use the make file to compile and/or test your program.
- (7 marks) You should include as many test cases as possible to demonstrate various cases, e.g., dealing with
- two complex numbers,
- two real numbers,
- two imaginary numbers,
- a real number and an imaginary number, P an imaginary number and a real number,
- a zero and a complex number, and
- a complex number and a zero.