Thread: Help C programming that reads value of x and n?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Question Help C programming that reads value of x and n?

    C program that reads the value of x and n from the keyboard and then approximately computes the value of cos(x) using the following formula:

    Cosx = 1 - x^2/2! + x^4/4! - x^6/6! .....+ (-1)^n x^2n/(2n)!

    (Written)
    cos of x equal 1 minus x square over two ! plus x to the fourth over 4! minus x to the sixth over six !.......plus(minus one) to the n...
    x squared n over (two n) !

    Can you all check if the middle part of the code is correct.

    Code:
    #include <stdio.h>
    #include <math.h>
    int main (void)
    {
    
    /*Declare variables*/
    int i, n;
    double x, cos;
    
    printf("Enter the value of x and n: ");
    scanf("%lf %d", &x, &n);
    
    
    /* loop to compute cosx using above formula*/ 
    
    /*Please can  you all check this part"*/
    
    double cos=1;
    int count=2;
    int p=1;
    int fact=1;
    int i,j;
    
    for( i = 0; i <= n; i=i+2)
    {
    if (i!=0)
    {
    //for x^2, x^4, x^6....
    for(j=1;j<=i;j++)
    p=p*x;
    
    //for factorial
    for(j=1;j<=i;j++)
    fact=fact*j;
    
    if(count%2==0)
    cos -= p/fact;
    else
    cos += p/fact;
    }
    count++;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    printf("Approximate cosx is %lf when n=%d \n", cosx, n);
    printf("Cos(x) in math lib returns %lf \n", Cos(x));
    
    
    system ("pause")
    
    return 0;
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would help a whole lot if the code was indented so that we could see what it actually looks like.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    It would be simpler for you to use print statements for each iteration of factorial and for the values of x^n to determine if the loops are equating the correct values or you!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kcpilot
    It would be simpler for you to use print statements for each iteration of factorial and for the values of x^n to determine if the loops are equating the correct values or you!
    Or use a debugger instead of print statements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    37
    I indented the code hope it helps.

    Code:
    #
    #include <stdio.h>
    #
    #include <math.h>
    #
    int main(void)
    #
    {
    #
     
    #
    /*Declare variables*/
    #
    int i, n;
    #
    double x, cos;
    #
     
    #
    printf("Enter the value of x and n: ");
    #
    scanf("%lf %d", &x, &n);
    #
     
    #
     
    #
    /* loop to compute cosx using above formula*/
    #
     
    #
    /*Please can you all check this part"*/
    #
     
    #
    double cos = 1;
    #
    int count = 2;
    #
    int p = 1;
    #
    int fact = 1;
    #
    int i, j;
    #
     
    #
    for (i = 0; i <= n; i = i + 2) {
    #
    if (i != 0) {
    #
    //for x^2, x^4, x^6....
    #
    for (j = 1; j <= i; j++)
    #
    p = p * x;
    #
     
    #
    //for factorial
    #
    for (j = 1; j <= i; j++)
    #
    fact = fact * j;
    #
     
    #
    if (count % 2 == 0)
    #
    cos -= p / fact;
    #
    else
    #
    cos += p / fact;
    #
    }
    #
    count++;
    #
    }
    #
     
    #
     
    #
     
    #
     
    #
     
    #
     
    #
     
    #
     
    #
     
    #
     
    #
     
    #
     
    #
     
    #
    printf("Approximate cosx is %lf when n=%d \n", cosx, n);
    #
    printf("Cos(x) in math lib returns %lf \n", Cos(x));
    #
     
    #
     
    #
    system("pause")
    #
     
    #
    return 0;
    #
     
    #
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not entirely sure what you are doing, but it looks even more rubbish than before.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed