Please Help

This is a discussion on Please Help within the C Programming forums, part of the General Programming Boards category; Hello I wrote this program to except user input parameters for a complex number in polar from then convert them ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Please Help

    Hello I wrote this program to except user input parameters for a complex number in polar from then convert them to a + jb format. But I used the cos() function in the math.h libbary and it keeps giving me the answer in radians not in dergees. For example if I input the paratmerers Z=1 and Theta=90 it should return 0+1j but it does'nt it gives me -0.448074+51.222233j instead. Can someone please point out to me where I am making a mistake and how to fix it.
    Thank you.

    Code:
    int main()
    {
        float Real, Complex, Z, Theta;
        float pi;
        
        pi = 3.141592654;
        
        printf("Please enter Z: ");
        scanf("%f",&Z);
        
        printf("Please enter theta: ");
        scanf("%f",&Theta);
        
        Real = Z*(cos(Theta));   
            
        Complex = sin(Theta)*(180/pi);
            
        if(Real > 0 && Complex > 0)
        {
                printf("The number is %f+%fj \n", Real, Complex);
        }
        
        if(Real > 0 && Complex < 0)
        {
                printf("The number is %f%fj \n", Real, Complex);
        }
        
        if(Real < 0 && Complex > 0)
        {
                printf("The number is %f+%fj \n", Real, Complex);
        }
        
        if(Real < 0 && Complex < 0)
        {
                printf("The number is %f%fj \n", Real, Complex);
        }
        
        return 0;
        
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You said what the problem was: your angle is in degrees and cos() expects radians. So convert Theta to radians before trying to do cos() and sin().

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You're converting the result of sin() - Complex = sin(Theta)*(180/pi);... That looks like a radians-to-degrees conversion but it's in the wrong place. And the wrong way around. The degrees-to-radians conversion should be made of the Theta argument to sin and cos.
    Last edited by nonoob; 10-05-2008 at 12:56 PM.

Popular pages Recent additions subscribe to a feed

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