2

This is the code i wrote to find the term that is lower than 0.00005 in a series depends on the value of x entered. When i run the code with value x equal to 1 and 2 the code work fine, but when i enter number 3 or number that is more than 3, code block seems to have some error with the compiler. Does anyone know what can i do to fix this?

#include <math.h>
#include <stdio.h>
main()
{
    double term=1.0,x;
    int i,fact();

    printf("Enter the value of x: ");
    scanf("%lf",&x);

    for(i=0;term>0.00001;i++){
        term=fabs((pow(-1,i)*pow(x,2*i))/fact(2*i));
    }

    printf("The term become smaller than 0.00005 when term %d reached\n",i );

}

//method for calculating factorial
int fact(int num)
{
    int fact=1;
    for(int i=1;i<=num;++i){
        fact*=i;
    }
    return fact;
}
15
  • main() is not one of the signatures for main. Minumum is int main(void). Try that. Can you post the errors that you did see? undefinded function maybe? And what is this: int i,fact();? (my compiler generates warning: warning: function declaration isn't a prototype. Commented Dec 1, 2021 at 14:49
  • @ryyker i tried that just now but still the same error Commented Dec 1, 2021 at 14:51
  • 3
    Running your code in an online compiler, at one point term goes from 0.003740 0.021478 and then keeps getting larger until inf. Commented Dec 1, 2021 at 15:01
  • 1
    For some values of x, term never gets small enough to leave the loop, resulting in an infinite loop. determine the valid ranges of x that your code should accept, then place a range limitation statement in to prevent running anything out of that range. By the way, your code does run on my Code::Blocks using MinGW (`gcc``) Commented Dec 1, 2021 at 15:02
  • 1
    The compiler compiles the code - when you run it you get this error. Not all numerical algorithms settle on an answer - sometimes initial conditions mean things get worse and further from the "right" number, rather than closer. Maybe add a max_iterations so it doesn't run forever? Commented Dec 1, 2021 at 15:07

1 Answer 1

4

A runtime construct (user input) can not cause a compiler error.

If we clean up your code, and add an additional condition to the loop we can see that your calculations can reach towards infinity. Without this condition your program will loop forever (giving the appearance of not working).

You'll need to adjust your formula, or your bounds.

#include <math.h>
#include <stdio.h>

int fact(int);

int main(void)
{
    double term = 1.0, x;
    int i;

    printf("Enter the value of x: ");
    scanf("%lf", &x);

    for (i = 0; term != INFINITY && term > 0.00001; i++) {
        term = fabs((pow(-1, i) * pow(x, 2 * i)) / fact(2 * i));
        printf("%lf\n", term);
    }

    printf("Final term: %d\n", i);
}

int fact(int num)
{
    int fact = 1;

    for (int i = 1; i <= num; ++i)
        fact *= i;

    return fact;
}

With a value of 2:

Enter the value of x: 2
1.000000
2.000000
0.666667
0.088889
0.006349
0.000282
0.000009
Final term: 7

With a value of 3:

Enter the value of x: 3
1.000000
4.500000
3.375000
1.012500
0.162723
0.016272
0.001109
0.003740
0.021478
0.431218
1.658689
60.034725
363.980804
1371.104161
16628.818145
146096.045130
862879.766548
inf
Final term: 18
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.