Thread: Need help with a problem

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    3

    Need help with a problem

    Hello everyone, I am a noob at programming and am having trouble finishing an assignment. The problem is to write a function that calculates the sine of a number using a Taylor series expansion up to the 10th term. This is the code I have written so far:
    Code:
    #include <stdio.h>
    
    
    int main(int argc, char **argv)
    {
        float x, y;
        
        long exp(int a, char b)
        {
            int i;
            long result=1;
            for (i=0; i<b; i++)
            {
                result=result*a;
            }
            return result;
        }
    
    
        int factorial(int a)
            {
                int i, result=1;
                for (i=1; i<=a; i++)
                {
                    result=result*i;
                }
                return result;
            }
        
        float sin(float a)
        {
            float result=0;
            int i;
            char n=1;
            
            for (i=1; i<20; i=i+2)
            {
                if (n%2==1)
                {
                    result=result + exp(a,i)/factorial(i);
                }
                else
                {
                    result=result - exp(a,i)/factorial(i);
                }
                n++;
            }
            return result;
        }
        
        x=0.524;
        y=sin(x);
        printf("sine is %f", y);
        
    }
    We were given specific decimal numbers which correspond to radians (0.524=pi/6) to test the program. In its current state I get 0.000000 regardless of my input for x.

    Any help is appreciated.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The maximum value an int can hold is( subject to implementation, but generally ) a little over 2 billion. 19! is over 121 quadrillion...

    Since you don't care much about precision, seeing your pi is 3.14, you should use float instead.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You should turn up your compiler warning level.

    You're not supposed to nest functions in C. You should move them out of main.

    If you're not using argc and argv then it's best to declare main as int main(void).

    Since the standard library already has functions called "sin" and "exp", it's best to name your functions something else, perhaps "mysin" and "myexp".

    Normally we use double instead of float unless there's a reason to do otherwise.

    n should be an int. There's no advantage to making it a char. Same with b in myexp.

    You should print a newline after the output.

    As to the reason you're getting 0 as output, there are two main problems.

    1. In myexp, you need to accept a float (or double) and make result (and return) the same.

    2. You need to return a long (or possibly long long) from factorial. factorial(20) is 2,432,902,008,176,640,000 which is WAY outside the range of a 32-bit int, but within the range of 64-bits.

  4. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    3
    Thank you GReaper and algorism. This was very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  4. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread