Thread: Series Question

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    4

    Series Question

    Here is the question I have on my HW The cosine of an angle can be computed from the following infinite series: 1- x^2/2!+x^4/4!-x^6/6! +......

    Write a program that reads an angle x (in radians) from the keyboard, Then you are asked to write two functions: one has one input parameter to compute the factorial, for example (2!, 4!, etc.), the other one is to compute the cosine of the angle using this series until the absolute value of a term ( where a is a real even number, 2, 4, 6, 8,…) is greater than 0.0001. Finally, print out the value computed along with the value of cosine computed using the C library function. NO GLOBAL VARIABLES ARE ALLOWED.

    So far i have this code, but for some reason my program always spits out a huge number any help would be appreciated.


    Code:
    	#include <stdio.h>
          #include <stdlib.h>
          #include <math.h>
    		
          int aFactorial(int);
    
          int main(void)
           {
             int i =1,iX;
             float fAns=1;
             printf ("\nPlease enter a value for X: ");
             scanf("&#37;d",&iX); 
             
     while(pow(iX,i)/aFactorial(i) >=.0001)
    {        
       fAns += pow(iX,i)/aFactorial(i);
      i+=2;
    }  
                  
             printf("\nSum of the series is %f",fAns);
             return 0;
           }
    
          int aFactorial(int iNumber)
           {
             int i,iFact=1;
             for (i=1;i<=iNumber; ++i)
              {
               iFact *= i;
              }
             return (iFact);		  
           }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you sure it actually compiles? It looks like it will not compile to me, since you are trying to use &#37; on a float, and iAns has never been declared.
    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

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    yeah i got it to compile it just printed out the incorrect value of the cos of the number i inputed

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have not handled the alternating addition/subtraction. Incidentally, you might want to indent your code properly.
    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
    Apr 2007
    Posts
    4
    oh ok , so do i need to add an if else statement after the while statement?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think that you should just express the series in summation notation first. Basically, you are calculating the sum, from i=0 to i=n, of ((-1)^i * (x^(2i) / (2i)!)) where ^ denotes exponentiation instead of bitwise xor, and ! denotes factorial. This corresponds very nicely to a for loop, just that you do not actually have to compute ((-1)^i), but can take advantage of the fact that ((-1)^i) is positive and negative on alternating iterations.

    EDIT:
    Oops, on closer inspection I saw that my expression was incorrect.
    Last edited by laserlight; 04-16-2007 at 09:02 PM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM