Starting from:

$30

CS2211- Assignment 5 Solved

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. 

 

a)     (2 marks) Declare a tag named complex_tag for a structure with two members, real and imaginary, of type double.

b)    (2 marks) Define a type (using typedef) named Complex_type for a structure with two members, real and imaginary, of type double.

c)     (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)

d)    (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.

e)     (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 function. 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)

f)     (4 marks) Put the three functions in a file called operation_functions.c and their prototypes in a file called operation_functions.h.

g)    Write a main program (operation.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.

h)    (8 marks) Write an appropriate makefile that compiles and tests your program.

i)      (2 marks) Provide a readme file that will tell how to use the make file to compile and/or test your program.

 

j)      (7 marks) You should include as many test cases as possible to demonstrate various cases, e.g., dealing with 

P  two complex numbers, 

P  two real numbers, 

P  two imaginary numbers, 

P  a real number and an imaginary number,  P an imaginary number and a real number, 

P  a zero and a complex number, and 

P  a complex number and a zero.

 

More products