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

In this 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, just add or subtract the corresponding real and imaginary parts. For instance, the sum of 5 + 3i and 4 + 2i is 9 + 5i. For another, the sum of 3 + i and –1 + 2i is 2 + 3i.

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

A structure variable can be passed to a function similar to the normal argument.
struct Person
 {
   // structure person is created
};
int main() {
    Person p;// structure variable p is declared
displayData(p);//the structure variable p is passed to a function
return 0;
}

void displayData(Person p)
 { // function display data is created
}

 
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.
typedef struct complex {
    float real;
    float imag;
} complexNumber;

In C++, a typedef name must be different from any class type name declared within the same scope. If the typedef name is the same as a class type name, it can only be so if that typedef is a synonym of the class name
In this program, two complex numbers entered by the user are stored in the structures num1 and num2.
complexNumber num1, num2
These two structures are passed to addComplexNumbers() function which calculates the sum and returns the result to the main() function.
complexNumber addComplexNumbers(complex, complex);

the function will be like
complexNumber addComplexNumbers(complex num1, complex num2) {
    complex temp;
    temp.real = num1.real + num2.real;
    temp.imag = num1.imag + num2.imag;
    return (temp);

This result is stored in the structure complexSum.
complexSum = addComplexNumbers(num1, num2);
Then, the sign of the imaginary part of the sum is determined and stored in the char variable signOfImag.
signOfImag = (complexSum.imag > 0) ? '+' : '-';
If the imaginary part of complexSum is positive, then signOfImag is assigned the value '+'. Else, it is assigned the value '-'.
We then adjust the value of complexSum.imag.
complexSum.imag = (complexSum.imag > 0) ? complexSum.imag : -complexSum.imag;
This code changes complexSum.imag to positive if it is found to be of negative value.
This is because if it is negative, then printing it along with signOfImag will give us two negative signs in the output.
So, we change the value to positive to avoid sign repetition.
After this, we finally display the sum.

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