Like Tree2Likes
  • 1 Post By hk_mp5kpdw
  • 1 Post By ledow

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

This is a discussion on How can i sum complex numbers? Need help with this exercise! within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    6

    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. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    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.

    Start with this:
    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;
    }
    Salem likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    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.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    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
    Salem likes this.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    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.
    HomePort : A C Web Service API for heterogeneous home automation systems

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    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. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    scanf("%f\n", &z1.real);
    Get rid of all those \n parts, they don't belong.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    6
    I figured it out, thanks to you. It works now. Thank You.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    As a side note, C99 added native support for complex numbers with complex.h.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. complex numbers in c++ (and c)
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 05-09-2010, 12:33 PM
  2. Complex Numbers
    By Link_26 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2009, 03:20 AM
  3. complex numbers
    By Mastiff in forum C Programming
    Replies: 6
    Last Post: 09-12-2008, 08:43 AM
  4. problem with complex numbers
    By dsc in forum C Programming
    Replies: 2
    Last Post: 12-12-2006, 12:18 PM
  5. Need Help with using complex numbers
    By Rich in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2001, 04:01 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21