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

C programming Complex Number Calculator (35 points) Create a complex number calc

ID: 673407 • Letter: C

Question

C programming

Complex Number Calculator (35 points)

Create a complex number calculator (For a brief overview on complex numbers, take a look at http://mathworld. wolfram.com/ComplexNumber.html). Your calculator should be able to compute the following:

• Addition of two complex numbers: z1 + z2

• Subtraction of two complex numbers: z1 z2

• Multiplication of two numbers: z1 · z2

• Division of two complex numbers: z1 z2

• Computation of the magnitude and phase (in degrees) of a complex number z. 1

The program should ask the user which of the five operations he wants to perform. It then asks the user to enter the appropriate values for the complex numbers and performs the necessary computation. Your program should execute continuously until the user requests to terminate it.

Sample run:

Press + for addition, - for subtraction, * for multiplication, / for division, m for magnitude and phase, or any other key to exit: +

Enter a complex number: 2 5

Enter a complex number: 1 8

(2+5i)+(1+8i)=(3+13i)

Press + for addition, - for subtraction, * for multiplication, / for division, m for magnitude and phase, or any other key to exit: n

Your program should include the following functions:

1. void read complex(float *z r, float *z i) // reads a complex number from the keyboard

2. void print complex(float z r, float z i) // prints a float number on the screen in the form a+bi

3. void add complex(float z1 r, float z1 i, float z2 r, float z2 i, float *z3 r, float *z3 i) // z3=z1+z2

4. void sub complex(float z1 r, float z1 i, float z2 r, float z2 i, float *z3 r, float *z3 i) // z3=z2-z1

5. void mul complex(float z1 r, float z1 i, float z2 r, float z2 i, float *z3 r, float *z3 i) // z3=z1· z2

6. void div complex(float z1 r, float z1 i, float z2 r, float z2 i, float *z3 r, float *z3 i) // z3=z1 z2

7. void par complex(float z1 r, float z1 i, float *mag, float *ph) // magnitude and phase

I got every part, the only thing that Im missing is fillinf out the print_complex function.

Can someone please help me??

Explanation / Answer

You said "I got every part, the only thing that Im missing is fillinf out the print_complex function", so in the below code i am proving read and print_complex functions only.

#include<stdio.h>

// reads a complex number from the keyboard
void read_complex(float *zr, float *zi)
{
   printf("Enter complex number :");
   float xr, xi;
   scanf("%f",&xr);
   scanf("%f", &xi);

   *zr = xr;
   *zi = xi;

   zr++;
   zi++;

   printf(" Enter complex number :");

   scanf("%f",&xr);
   scanf("%f", &xi);

   *zr = xr;
   *zi = xi;


}

void print_complex(float zr, float zi) // prints a float number on the screen in the form a+bi
{
   //need to print in a+bi format
   printf("(%g+%gi)",zr,zi);
}

int main() {

   float *zr, *zi;
   float rArr[2], iArr[2];
   zr = &rArr[0];
   zi = &iArr[0];
   read_complex(zr,zi);

   printf(" Entered complex numbers ");
   print_complex(rArr[0],iArr[0]);
   printf(" ");
   print_complex(rArr[1],iArr[1]);
   return 0;
}