Home > Blockchain >  complex product in c using struct?
complex product in c using struct?

Time:11-22

(c environment, visual studio) I have this exercise: compute the product between two complex numbers. the result is pointed to by comp1. I know that two complex numbers are multiplied to each other in this way:

(a ib)*(c id)=(ac−bd) i(ad bc). And I did it. this is a very simple program, in fact I used only 1 line (in the function). but in the debugger I read:

"an entity of type double cannot be assigned to an entity of type "struct complex" ".

I tried to use the cast operator, but I figured out that's illegal with a struct type. So I feel completely lost. I know the theory: if I want to pass struct to function, I need to use pointers. and to access to struct members using pointers I use the syntactic sugar "arrow operator "->". it means " *(p).x ". So what's wrong with it?

geometry.c

#include "complex.h"
#include "stdlib.h"
void complex_product(struct complex *comp1, struct complex *comp2) {
    *comp1 =  ((comp1->re * comp2->im) - (comp1->im * comp2->im))   ((comp1->re * comp2->im)   (comp1->im * comp2->re));
}

geometry.h

#if !defined COMPLEX_H 
#define COMPLEX_H
struct complex {
    double re, im; 
};
extern void complex_product(struct complex* comp1, struct complex* comp2); 

#endif 

CodePudding user response:

Read the error that you're getting from the compiler carefully. Understanding error messages is an important skill! The problem is not the way you access the fields of comp1 and comp2 to perform the numerical calculation, but what you do with the result of the calculation.

*comp1 = /*some number*/;

*comp1 is a struct complex. The right-hand side is a number. You can't assign a number to a structure.

I know that two complex numbers are multiplied to each other in this way:

(a ib)*(c id)=(ac−bd) i(ad bc)

Correct.

((comp1->re * comp2->im) - (comp1->im * comp2->im))   ((comp1->re * comp2->im)   (comp1->im * comp2->re))

That's not what you wrote above. Notice that you're calculating a real number here. The i part is missing.

In your program, a complex number is represented by a structure with two fields re and im for the real part and the imaginary part. i isn't represented as something you can multiply. You need to calculate the real part and the imaginary part separately, not add them together.

struct complex result = { /*expression for the real part*/, /*expression for the imaginary part*/ };

or

struct complex result = {
  .re = /*expression for the real part*/,
  .im = /*expression for the imaginary part*/,
};

or

struct complex result;
result.re = /*expression for the real part*/;
result.im = /*expression for the imaginary part*/;

Apparently complex_product should use its first argument both as an input and as an output. When you do that, you need to be careful not to overwrite the input before you've finished the calculation. This wouldn't work:

comp1->re = …;
comp1->im = …;

because the calculation of comp1->im requires the old value of comp1->re. The easiest way to handle that is to use an intermediate variable to store the result, as I showed above, and copy that to the output at the end:

void complex_product(struct complex *comp1, struct complex *comp2) {
    struct complex result;
    result.re = …;
    result.im = …;
    *comp1 = result;
}

Make sure you understand when this program is using * and when it's using ->. This is the sort of thing you need to get used to when you learn C.

I'll leave it to you to fit all this together. Beware that there's at least one mistake above – you're calculating a*d twice and not calculating a*c.

CodePudding user response:

The problem is simple, you are doing the operation correctly but you are not storing the result in the right variable, with a complex number you have a real part and an imaginary part. what you are trying to do is mush them together and assign that value to a complex struct (which is illegal bcz different variable types).

what you need to do is assign the real part to double variable and the imaginary part to a different variable and then assign those variables to the corresponding attributes of the struct, like this:

double re_result = (comp1->re * comp2->im) - (comp1->im * comp2->im);
double im_result = (comp1->re * comp2->im)   (comp1->im * comp2->re);

comp1.re = re_result;
comp1.im = im_result;

this way your comp1 complex variable will have the resulting values of real and imaginary parts.

  • Related