Thread: i did not understood to the question. But i did i don't know is this correct or not

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

    Post i did not understood to the question. But i did i don't know is this correct or not

    Code:
    #include <stdio.h>
    #include <math.h>
    int
    factorial (int number)
    {
      int factorial;
      factorial = 1;
    
    
      for (int i = number; i > 1; i--)
        {
          factorial *= i;
        }
      return factorial;
    }
    
    
    
    
    int sigma()
    {
        int sigma = 0;
        int n=2;
        for(int i=0; i<=100; i++){
            sigma += n*i+1;
        }
        return sigma;
    }
    
    
    
    
    float sinus_cal(float x)
    {
        float sinus;
        float u = 0;
        int l = sigma();
        for(int i=0; i<=100; i++){
            // -1^i
            u += pow(-1, i);
        }
        sinus = ((u / factorial(l)) * pow(x,l));
        return sinus;
    }
    
    
    int main()
    {   
        printf("%f",sinus_cal(3));
        return 0;
    }
    Attached Images Attached Images i did not understood to the question. But i did i don't know is this correct or not-midterm4-png 
    Last edited by Jaloliddin; 04-26-2021 at 05:33 PM.

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    139
    1. Your factorial is taking int parameters. You should print out the factorials of all the numbers up to 201! to be sure it works. (Hint: try it on your favorite calculator first.)

    2. The expression -1 ^n is really just -1 * -1 * -1 ..., out to n times. Thus, -1^0 is 1, -1^1 is -1, -1^2 is 1, etc. These are just +1/-1 alternating each time.

    3. The function takes a float, and so presumably returns a float. You need to pay attention to losses due to type conversion and rounding.

    4. "Sigma" is the big greek letter that looks like an "E" in the image you posted. It operates over everything to the right of it, unless demarcated with some kind of bracket. If you're going to have a "sigma()" function, it should compute everything.

    5. Since the factorials you are being asked to compute are increasing, you can get much better performance by remembering the last argument and result in your factorial function.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by aghast View Post
    1. Your factorial is taking int parameters. You should print out the factorials of all the numbers up to 201! to be sure it works. (Hint: try it on your favorite calculator first.)
    A small program to test this:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    float fact ( unsigned int x )
    {
      float n = 1.0;
    
      while ( x > 1 )
        n *= x--;
    
      return n;
    }
    
    int main ( void )
    {
      float n, m;
      int i;
    
      n = 1.0;
    
      for ( i = 2; i <= 201; i++ )
      {
        m = fact ( i );
    
        printf ( "%d -> %g ", i, m );
    
        // stops if is a nan or inf.
        if ( ! isnormal ( m ) )
        {
          puts ( "[fail]." );
          break;
        }
    
        n = m;
    
        putchar ( '\n' );
      }
    }
    Running it you can see it fails for factorial of 35. Changing to double and will fail for fatorial of 171. It works for factorial of 201 with long double (will fail for factorial of 1755).

    2. The expression -1 ^n is really just -1 * -1 * -1 ..., out to n times. Thus, -1^0 is 1, -1^1 is -1, -1^2 is 1, etc. These are just +1/-1 alternating each time.
    As such it can be easily coded as:
    Code:
    int signal_( unsigned int x ) { return x & 1 ? -1 : 1; }
    5. Since the factorials you are being asked to compute are increasing, you can get much better performance by remembering the last argument and result in your factorial function.
    Or, since there are a fixed interations for the loop, use a pre-compiled table:
    Code:
    /* gentbl.c */
    #include <stdio.h>
    
    static long double fact ( unsigned int x )
    {
      long double n = 1.0;
    
      while ( x > 1 )
        n *= x--;
    
      return n;
    }
    
    int main ( void )
    {
      unsigned int i;
    
      fputs ( "long double tbl_[] = {\n", stdout );
    
      for ( i = 0; i < 100; i++ )
        printf ( "  %.18LgL,\n", fact ( 2*i + 1 ) );
    
      printf ( "  %.18LgL\n};\n", fact ( 2*i + 1 ) );
    }
    Compile, and running to create the table:
    Code:
    $ cc -o gentable gentable.c
    $ ./gentable > tbl.c
    The sin_() function:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    /* from tbl.c */
    extern long double tbl_[];
    
    double sin_( double x )
    {
      unsigned int i;
      long double n;
      double t;
    
      // x cannot be a non-number
      if ( isnan( x ) || isinf( x ) )
        return NAN;
    
      // Restrict x to (-2*PI,2*PI).
      // not sure if this will have an impact
      // on "precision".
      t = fabs( x );
      if ( t >= 2.0 * M_PI )
        x /= t / (2.0 * M_PI );
    
      for ( i = 0; i <= 100; i++ )
      {
        n += (i & 1 ? -1 : 1 ) * powl( x, 2*i + 1 ) / tbl_[i];
    
        // powl() can result in an overflow!
        if ( isinf( n ) || isnan( n ) )
          break;
      }
    
      // n between -1 and 1 will fit in a double.
      return n;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. have i finally understood this or is it another fluke
    By cooper1200 in forum C Programming
    Replies: 3
    Last Post: 05-26-2019, 04:10 PM
  2. I thought I understood this.
    By somekid413 in forum C Programming
    Replies: 2
    Last Post: 01-11-2010, 08:32 PM
  3. Have I understood the atoi funtion right?
    By austra in forum C Programming
    Replies: 5
    Last Post: 11-13-2009, 09:15 AM
  4. Functions are Still Not Understood.
    By errigour in forum C Programming
    Replies: 6
    Last Post: 04-09-2009, 02:54 PM
  5. I thought I understood but....(pointer question)
    By caroundw5h in forum C Programming
    Replies: 1
    Last Post: 02-22-2005, 03:05 AM

Tags for this Thread