98 989
Assignments Done
99%
Successfully Done
In May 2021

# Answer to Question #189103 in C++ for Waleed Abid

Question #189103

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

1
2021-05-04T12:42:57-0400
#include <iostream>
using namespace std;

typedef struct {
double real;
double imaginary;
} Complex;

Complex complexSum(Complex a, Complex b) {
Complex sum;

sum.real = a.real + b.real;
sum.imaginary = a.imaginary + b.imaginary;

return sum;
}

void complexPrint(Complex a) {
if ( a.imaginary > 0 ) {
cout << a.real << "+" << a.imaginary << "i";
}
if ( a.imaginary < 0 ) {
cout << a.real << a.imaginary << "i";
}
if ( a.imaginary == 0 ) {
cout << a.real;
}
}

int main() {
Complex z1, z2, sum;

cout << "Please enter real and imaginary parts for the first complex number:" << "\n";
cin >> z1.real >> z1.imaginary;

cout << "\nPlease enter real and imaginary parts for the second complex number:" << "\n";
cin >> z2.real >> z2.imaginary;

sum = complexSum(z1, z2);

cout << "\nThe sum of your complex numbers is: ";

complexPrint(sum);

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!