Structures

Hi, I need a little help modifying more for this program.

Instructions:(Menu based program)
Complex Number Menu
1. input complex number 1
2. input complex number 2
3. add
4. subtract
5. magnitude of a complex number
6. angle of a complex number
7. display complex numbers
8. exit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  #include <stdio.h>
#include <stdlib.h>

typedef struct complex 
{
  float real;
  float imag;
} complex_t;

complex_t add_complex(complex_t a, complex_t b)
{
   complex_t temp;
   temp.real = a.real + b.real;
   temp.imag = a.imag + b.imag; 
   return temp;
}

// prints values of a complex number
void print_complex(complex_t a);
// defines a complex value by supplying the real and imag values
complex_t complex(float a, float b);
          
int main() 
{
    complex_t x={1,-2},y,z;
    y = complex(-1,-2);
    print_complex(y); // print the values of y
    print_complex(x); // print the values of x
    z = add_complex(y,x); // add x and y and put result into z 
    print_complex(z); // print the values z  
}

void print_complex(complex_t a)
{ 
     printf("\n%f ",a.real);
     // this if-else takes care of the sign when printing
     // so that the - is place before j for negative values
     if (a.imag<0) printf("-j%f\n",-a.imag);
     else printf("+j%f\n",a.imag);
}

// note that this function returns a complex type
complex_t complex(float a, float b)
{
   complex_t temp;
   temp.real = a;
   temp.imag = b;
   return temp;   
}
Last edited on
That's a very non-specific question. What is your problem?

You seem to have "acquired" enough code to do items 1-3, and if you can add two complex numbers then subtracting them ought to be easy.

So, what can't you do?
Topic archived. No new replies allowed.