### Help multiplying complex numbers. I cant seem to figure out how to get the math part sorted out correctly. Can anyone help?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````// s Complex Numbers.cpp : This program should allow the user to add, subtract, and multiply complex numbers. // #include "stdafx.h" #include using namespace std; class Complex { public: void output() { cout << "Your complex number is: " << r << "+" << i << "i" << endl; } void input() { cout << "Enter the real part of the complex number: "; cin >> r; cout << "Enter the imaginary part of the complex number: "; cin >> i; } void add (Complex n1, Complex n2) { r = n1.r + n2.r; i = n1.i + n2.i; } void subtract( Complex n1, Complex n2) { r= n1.r- n2.r; i = n1.i - n2.i; } void multiply (Complex n1, Complex n2) { n1.r = n1.r * n2.r + n1.r * n2.i; n1.i = n1.i * n2.r + n1.i * n2.i ; n2.r = n2.r*n1.r+ n1.r * n2.i; n2.i = n2.i * n1.r+ n2.i * n1.i; r = n1.r + n2.r; i = n1.i + n2.i; } private: int r; int i; }; int main() { Complex number1, number2, sum, differnce, product; number1.input(); number2.input(); sum.add(number1, number2); differnce.subtract(number1, number2); product.multiply(number1, number2); sum.output(); differnce.output(); product.output(); return 0; }``````
Last edited on
Firstly, put your code in [code] [ /code ] tags...
Secondly, the "math" part of multiplying complex numbers is quite easy.
 `(x + yi)(a + bi) = (xa – yb) + (xb + ya)i`

I am not sure if I understood your question. So if this is not what you actually asked, please let me know.

Best of wishes,
~ Raul ~
 ``123`` `````` r = (n1.r * n2.r) - (n1.i * n2.i); i = n1.r * n2.r + n1.i * n2.r ; r + i * (i);``````

Yes that right.. so if i type it into my program it would look like this correct?
Yes. If by `r + i * (i);` you mean the output (cout), then yes.
Nevermind I got it~ Thanks so much for the help its very much appreciated :)
No problem. Glad I could help. :)
Topic archived. No new replies allowed.