C++ Program to Add Complex Numbers by Passing Structure to a Function


January 21, 2023, Learn eTutorial
1047

In this C++ program, We are taking two complex numbers as structures and adding them together with the help of functions.

What is a complex number?

A complex number is an element of a number system that contains the real numbers and a specific element denoted 'i', called the imaginary unit, and satisfies the equation i2 = −1. Moreover, every complex number can be expressed in the form a + bi, where a and b are real numbers.

How to add two complex numbers?

To add or subtract two complex numbers, we have to add the real and complex parts of the numbers together. For instance, the sum of 2+ 1i and 3 + 2i is 5 + 3i. 

How to pass a structure to function in C++?

A structure variable can be passed to a function similar to the normal argument.

How to create a C++ Program to Add Complex Numbers by Passing Structure to a Function?

Here we are creating a structure called complex and real, imag as its data members. In this C++ program, two complex numbers entered by the user are stored in the structures num1 and num2.

Now we have to pass these two structures into the function and it will do the calculation and return the result. Then, we calculate the sign of the complex part of the result and store it in a variable.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3:  Create a structure called complex and real, imag as its data members 

Step 4: Store two complex numbers entered by the user in the structures num1 and num2.

Step 5: Create a function addcomplexNumbers() which calculates the sum and returns the result to the main() function

Step 6: Pass the structures num1 and num2 to the function addComplexNumbers()

Step 7: Store the result in the structure complexSum.

Step 8: Determine the sign of the imaginary part of the sum and stored it in the char variable signOfImag 

Step 9: Assign the value of signOfImag to + for a positive value of the imaginary part of complexSum. And a – for negative value

Step 10: Display the result 

Step 11: Exit
 

C++ Source Code

                                          // Complex numbers are entered by the user

#include <iostream>
using namespace std;

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

complexNumber addComplexNumbers(complex, complex);

int main() {
    complexNumber num1, num2, complexSum;
    char signOfImag;

    cout << "For 1st complex number," << endl;
    cout << "Enter real and imaginary parts respectively:" << endl;
    cin >> num1.real >> num1.imag;

    cout << endl
         << "For 2nd complex number," << endl;
    cout << "Enter real and imaginary parts respectively:" << endl;
    cin >> num2.real >> num2.imag;

    // Call add function and store result in complexSum
    complexSum = addComplexNumbers(num1, num2);

    // Use Ternary Operator to check the sign of the imaginary number
    signOfImag = (complexSum.imag > 0) ? '+' : '-';

    // Use Ternary Operator to adjust the sign of the imaginary number
    complexSum.imag = (complexSum.imag > 0) ? complexSum.imag : -complexSum.imag;

    cout << "Sum = " << complexSum.real << signOfImag << complexSum.imag << "i";

    return 0;
}

complexNumber addComplexNumbers(complex num1, complex num2) {
    complex temp;
    temp.real = num1.real + num2.real;
    temp.imag = num1.imag + num2.imag;
    return (temp);
}

                                      

OUTPUT

For 1st complex number,
Enter real and imaginary parts respectively:
2.5
3.5
For 2nd complex number,
Enter real and imaginary parts respectively:
4.2
-2.1
Sum = 6.7+1.4i