Thread: Help with adding new functions

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    3

    Help with adding new functions

    Hi, I am new to c and I have come across a problem when adding other functions to a programme and printing the values. The question I am attempting to solve is :

    Code:
    The following function computes ex by summing the Taylor series expansion to 
    n terms. Write a program to print a table 
    of ex using both this function and the exp()
     function from the math.h library, for x = 0 to 1 in steps of 0.1. 
    The program should ask the user what value of n to use.
    double taylor(double x, int n)
    {
    int i;
        double sum = 1.0;
        double term = 1.0;
            for (i=1; i<=n; i++)
            {                       /*Or we could have written: */
            term = term * x / i;     /* term *= x / i; */
            sum = sum + term;      /* sum += term; */
            }
    return sum;
    }
    My code is

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    
    /*Taylor series for e*/
    
    double taylor(double x, int n)
    {
            int i;
            double sum = 1.0;
            double term = 1.0;
                    
                     /* Value of n*/
                         {
                             printf(" Value of n :\n" );
                             scanf("%f",&n);
                         }
                     
                             for (i=1; i<=n; i++)
                             { 
                                 term = term * x / i; 
                                 sum = sum + term;
                                 printf(" Taylor series : %f\n", sum);
                             }
                             
                             
                             
         return sum;
    }    
        int main ()
        
    {          
             int i;
             double n, x, terme;
             
             /* e using math function*/
                 
                 for(x=0; x<=1; x = x+0.1)
                 {
                 
                 terme = exp(x);
                 printf(" The values of the exponential term : %f\n", terme);
                 
                 }
                 taylor(x,n);
    
         return(0);
    }
    The code prints out the values for exp, but it gets stuck in the Taylor function and I'm not sure how to solve it.

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That scanf() call in tailor() probably causes your program to wait for user input.

    It doesn't help that the %f format tells scanf() to read a floating point value, but the corresponding argument being read is an int. The result of that is undefined behaviour.


    Changing the value of n inside taylor() does not change the value of n in main(). They are distinct variables.


    You code has all the hallmarks of guesswork - you are simply hacking away adding code and hoping it will work. The odds of that working are tiny. Try thinking about what you are trying to do, and reason about it, BEFORE typing in code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Turn up your compiler warnings and fix them:

    Code:
    taylor.c:17:37: warning: format specifies type 'float *' but the argument has
          type 'int *' [-Wformat]
                             scanf("%f",&n);
                                    ~~  ^~
                                    %d
    taylor.c:34:14: warning: unused variable 'i' [-Wunused-variable]
             int i;
                 ^
    taylor.c:46:23: warning: variable 'n' is uninitialized when used here
          [-Wuninitialized]
                 taylor(x,n);
                          ^
    taylor.c:35:18: note: initialize the variable 'n' to silence this warning
             double n, x, terme;
                     ^
                      = 0.0
    3 warnings generated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-27-2013, 06:43 PM
  2. Pointer to functions, program adding two numbers
    By greg677 in forum C Programming
    Replies: 4
    Last Post: 04-14-2010, 08:54 PM
  3. Manually adding controls to a wnd and their functions.
    By earth_angel in forum Windows Programming
    Replies: 4
    Last Post: 08-22-2005, 06:26 PM
  4. Adding functions to a program
    By Finchie_88 in forum Windows Programming
    Replies: 9
    Last Post: 03-04-2005, 11:10 AM
  5. Replies: 6
    Last Post: 05-06-2003, 03:08 PM