Search code examples
c++classcomplex-numbers

Why are the complex numbers printed here incorrect?


I am trying to complete a c++ assignment code of operating with complex numbers. The logic I have used in the operations is pretty much alright, but I cant get the output to come out as I want it. It must be a logical error but I am just not getting it.

I have tried constructors, parameterized and more, but I am just not getting it.

#include<iostream>
using namespace std;
class complex
{
     int r, i;
public:
    int Read()
    {
        cout << "Enter Real part" << endl;
        cin >> r;
        cout << "Enter Imaginary part" << endl;
        cin >> i;
        return 0;
    }

    complex Add(complex A, complex B)
    {
        complex sum;
        sum.r = A.r + B.r;
        sum.i = A.i + B.i;
        return sum;
    }
    complex Subtract(complex A, complex B)
    {
        complex diff;
        diff.r = A.r - B.r;
        diff.i = A.i - B.i;
        return diff;
    }
    complex Multiply(complex A, complex B)
    {
        complex prod;
        prod.r = A.r*B.r + A.i*B.i*(-1);
        prod.i = A.r*B.i + B.r*A.i;
        return prod;
    }
    complex Divide(complex A, complex B)
    {
        complex c_B; //conjugate of complex number B
        c_B.r = B.r;
        c_B.i = -B.i;
        complex quotient;
        complex numerator;
        complex denominator;
        numerator.Multiply(A, c_B);
        denominator.Multiply(B, c_B);
        int commonDenom = denominator.r + denominator.i;
        quotient.r = numerator.r / commonDenom;
        quotient.i = numerator.i / commonDenom;
        return quotient;
    }

    int Display()
    {
        cout << r << "+" << i << "i" << endl;
        return 0;
    }

};

int main()
{
    complex a, b, c;
    cout << "Enter first complex number" << endl;
    a.Read();
    cout << "Enter second complex number" << endl;
    b.Read();
    c.Add(a, b);
    c.Display();
    c.Multiply(a, b);
    c.Display();



    system("pause");
    return 0;
}





the expected output on input of 1+2i and 2+3i should be
3+5i
8+i

but output is
-858993460+-858993460i
-858993460+-858993460i

Solution

  • Take a look at this code:

    c.Add(a, b);
    c.Display(); // <- Here
    

    Here’s something to think about: which complex number are you displaying here?

    Take a look at your Add function. Notice that calling c.Add(a, b) does not actually set c equal to the sum of a and b. Instead, it essentially ignores c (look over the code - notice that you never read or write any fields of the receiver object) and then produces a new complex number equal to a + b. As a result, when you call c.Display(), you’re not printing the sum. Instead, you’re taking c, which never had its data members initialized, and printing out its value.

    There are several different strategies you can use to fix this. Fundamentally, I’d look back over how you’ve defined Add and the other member functions that compute on complex numbers. If those member functions don’t use the receiver object, then consider either

    1. Making them static or free functions so that they just operate on their two arguments, not two arguments plus an implicit this argument, or

    2. Make them take exactly one parameter, with the two complex numbers operated on being the receiver object and the parameter. You then have a choice of whether to make those functions modify the receiver or return new values.

    Once you’ve decided how you want to address that above issue, go back and look at the code you’ve written to add and print values. You may need to introduce more variables to explicitly capture the sums, differences, etc. of the operations you’ve performed.

    Hope this helps!