Thread: Calculated values coming out strange

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    8

    Calculated values coming out strange

    Hey guys, I cant see whats going wrong here, values for theta calculate out to a very strange value. Function is suppose to estimate pi by doing sqrt(6*(1/1*1)+1/(2*2)+1/(3*3) etc using 5 and 10 terms. Please help if possible! Thanks alot.

    Sam

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    double theta(int terms);
    const double piValue = 3.141592653589793;
    
    int main (void)
    {
        int terms;
        
        printf("The value of pi is %lf\n", piValue);
        terms=5;
        printf("When using %d terms:\n", terms);
        printf("theta = %lf\n", theta(terms));
        printf("The difference between theta and pi is %lf\n\n",
        theta(terms) - piValue);
        terms=10;
        printf("When using %d terms:\n", terms);
        printf("theta = %lf\n", theta(terms));
        printf("The difference between theta and pi is %lf\n\n",
        theta(terms) - piValue);
        system("pause");
        return 0;
    }//main
    
    double theta(int terms)
    {
           int denom;
           double sumOfTerms;
           
           for(denom = 1; denom <= terms; denom++)
           {
                sumOfTerms += (double)1/(denom*denom);
                }
           return sqrt (6*sumOfTerms);
         }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're not initializing sumOfTerms so it's just starting off with some garbage value. Change the line to:
    Code:
    double sumOfTerms = 0;
    If you understand what you're doing, you're not learning anything.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to initialize sumofterms to 0. in your theta function.

    Code:
    double theta(int terms)
    {
           int denom;
           double sumOfTerms = 0;

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    that did it. Thanks a million guys!

    Sam

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Turn on your compiler warnings
    and start paying attention to them
    and you'll be able to save million thanks =)

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You might want to save the value returned by theta(terms) so that you don't call it twice. For a lot of terms this could save a lot of recalculation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-15-2011, 12:31 PM
  2. Need Help
    By fourseventwo in forum C Programming
    Replies: 4
    Last Post: 12-04-2010, 11:01 PM
  3. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  4. adding ASCII values
    By watshamacalit in forum C Programming
    Replies: 1
    Last Post: 12-26-2002, 07:16 PM
  5. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:59 PM