Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write the following functions in a C program not C++. 1. complex complex_add(com

ID: 3754228 • Letter: W

Question

write the following functions in a C program not C++.

1. complex complex_add(complex, complex);

complex_add() will add the corresponding fields of two complex variables together and will return a complex value that represents the sum. For instance, if complex_add() is invoked with the values (1.0, 2.0) and (3.0, 4.0), it will return the value (4.0, 6.0). complex complex_neg(complex);

2. complex_ neg() will return the negative form of the argument. E.g., -1.0 times the value of each field.

complex complex_sub(complex, complex);

3. complex_ sub() is similar to complex_add() except that it subtracts the second argument from the first. You should implement this function with something like: complex_add(arg1, complex_neg(arg2))

Explanation / Answer

Code:

#include <stdio.h>

struct complex
{
float real;
float imag;
};

void input(struct complex*);
void display(struct complex);
struct complex complex_sum(struct complex, struct complex);
struct complex complex_sub(struct complex, struct complex);
struct complex complex_neg(struct complex);

int main()
{
struct complex c1, c2, c3, c4,c5;
printf(" Enter Complex Number 1: ");
input(&c1);
printf(" Enter Complex Number 2: ");
input(&c2);
printf(" Complex number 1 is ");
display(c1);
printf(" Complex number 2 is ");
display(c2);
c3 = complex_sum(c1, c2);
printf(" Addition = ");
display(c3);
c4 = complex_neg(c1);
printf(" Product = ");
display(c4);
c5 = complex_sub(c1,c2);
printf(" Subtract = ");
display(c5);
return 0;
}

void input(struct complex *t)
{
printf(" Enter value in ");
printf("real part : ");
scanf("%f", &t->real);
printf("imaginary part : ");
scanf("%f", &t->imag);
}

void display(struct complex c)
{
printf(" %0.2f + %0.2f i", c.real, c.imag);
}

struct complex complex_sum(struct complex t1, struct complex t2)
{
struct complex t;
t.real = t1.real + t2.real;
t.imag = t1.imag + t2.imag;
return t;
}

struct complex complex_neg(struct complex t1)
{
struct complex t;
t.real = t1.real * -1 - t1.imag *-1;
t.imag = t1.real * -1 + t1.imag * -1;
return t;
}
struct complex complex_sub(struct complex t1, struct complex t2)
{
return complex_sum(t1, complex_neg(t2));
}

Output:

Enter Complex Number 1:

Enter value in
real part :23 imaginary part : 34
Enter Complex Number 2:

Enter value in
real part : 3 imaginary part : 4

Complex number 1 is
23.00 + 34.00 i

Complex number 2 is
3.00 + 4.00 i

Addition =
26.00 + 38.00 i

Product =
11.00 + -57.00 i

Subtract =
24.00 + 27.00 i