# Thread: How can i sum complex numbers? Need help with this exercise!

1. ## How can i sum complex numbers? Need help with this exercise!

Hello, I just need help with this exercise:
So Z1=5+3i
and Z2=-2-5i

The sum is 3-2i. But how can I print it? The code i wrote here prints me 3 4198924 i. What am I doing wrong?

Code:
```#include <stdio.h>

int a,b,c,d;
int COMPSUM(int a,int b,int c,int d);

int main()
{
//int a,b,c,d;
printf("Enter first complex number real part:\n");
scanf("%d\n",&a);
printf("Enter first complex number imaginary part:\n");
scanf("%d\n",&b);
printf("Enter second complex number real part:\n");
scanf("%d\n",&c);
printf("Enter second complex number imaginary part:\n");
scanf("%d\n",&d);
printf("%d %d i\n",COMPSUM(a,b,c,d));
return 0;
}
int COMPSUM(int a,int b,int c,int d){
int e,f;
e=a+c;
f=b+d;
return e,f;
}```

2. A return statement can only return a single value, a complex number is represented by two values. In order to return a complex number as a single value, you would need to wrap up the two components in a struct and return a struct representing the sum. Then, you would still be unable to print as you are doing since can't print a struct in C (you could in C++ with operator overloading). You would have to print the elements of the struct individually which would most likely mean storing the sum into a temporary of some kind and then passing the two components of the temporary to the printf call.

Code:
```struct complex
{
int real;
int imaginary;
};

int main()
{
struct complex first, second, sum;

/* Get real/imaginary values into first, second struct */

/* Compute sum and store into sum struct */

/* Print the sum struct's real/imaginary components */

return 0;
}```

3. Code:
`return e,f;`
Wrong, wrong, wrong, wrong, wrong.

You cannot return two answers in the return value of a function, and you certainly can't do it using the "," operator.

Similarly, you can't use a printf with two %d's and then give it only the SINGLE return value that will come out of COMPSUM. That's why you get junk.

4. P.S. ENABLE WARNINGS ON YOUR COMPILER!

(I really get sick of telling people that).

It would have spotted both problems for you immediately:

..\test.c: In function 'main':
..\test.c:17: warning: too few arguments for format
..\test.c: In function 'COMPSUM':
..\test.c:24: warning: left-hand operand of comma expression has no effect

5. You cannot return multiple parameters from a function in C. You need either to return a struct that will contain both numbers, or use a sum function two times, one for each part.

6. Code:
```#include <stdio.h>

typedef struct Complex {
float real;
float img;
} Complex;

Complex complex_sum (Complex z1, Complex z2) {
Complex z3;
z3.real = z1.real + z2.real;
z3.img = z1.img + z2.img;
return z3;
}
int main()
{
Complex z1, z2, z3;

printf("Enter first complex number\n");
printf("Enter complex number real part:\n");
scanf("%f\n", &z1.real); //here I enter 5
printf("Enter complex number imaginary part:\n");
scanf("%f\n", &z1.img); //here I enter 3

printf("Enter second complex number\n");
printf("Enter complex number real part:\n");
scanf("%f\n", &z2.real); //here I enter -2
printf("Enter complex number imaginary part:\n");
scanf("%f\n", &z2.img); //here I enter -5

z3 = complex_sum(z1, z2);
//Show sum

printf("Sum: %f + i*%f", z3.real, z3.img);
getchar();
return 0;
}```
I get no error messages or warnings, but it prints me 3.000000 + i*3.000000.

7. Code:
`scanf("%f\n", &z1.real);`
Get rid of all those \n parts, they don't belong.

8. I figured it out, thanks to you. It works now. Thank You.

9. As a side note, C99 added native support for complex numbers with complex.h.