Help for a C program [closed]

0

Hi, I just wrote a C program as a university internship which must calculate the cosine of a number through the Taylor series. The problem is that the program compiles perfectly but at the time of executing it it stops right after the second scanf. I have tried to use the ddd debugger but it does not seem to detect the problem. The code is as follows:

#include <stdio.h>

main()
{

    float x, cos=0, t;
    int n, z, r, exp, aux=0, aux2, aux3;

    printf("Introduzca el valor de x(real): ");
    scanf("%f%*c", &x);
    printf("Introduzca el valor de n(natural): ");
    scanf("%d", &n);

    while(aux<=n);
    {
        printf("prueba");
        aux3=2*aux;
        r=aux3;
        exp=2*aux;
        t=x;
        aux2=2;
        if(exp==0)
        {
            t=1;
        }

        while(aux2<=exp)
        {
            t=t*x;
            aux++;
        }
        while(aux3>1)
        {
            aux3--;
            r=r*aux3;
        }
    if(aux3==0)
            r=1;
        if(aux%2==0)
        {
            z=1;
        }
        else
        {
            z=-1;
        }

        cos=cos + (t/r)*z;
        aux++;
    }
    printf("cos(%f) = %f\n", x, cos);
}

I would very much appreciate someone helping me because I have been looking for the problem for a long time and I can not find it.

    
asked by Aldagar 24.03.2017 в 19:59
source

1 answer

5

Your problem is in a small line of readable code:

while(aux<=n); // <- El ;

The while cycle in any programming language, followed by ; literally means:

  

Do nothing while the condition is true.

And in your case, it will always be true, so it will remain in an infinite cycle (Forever!)

The solution:

Remove that ; at the end of that line, leaving it like this:

while (aux <= n) 
{
    // Resto del código...
}

EDIT:

In addition to that, I found that you update the aux within another cycle while, the truth I do not know how it can affect the code so as not to show the information at the end, but I changed it and it worked, I hope that to you same. that I still have no explanation of why it does not show the printf("prueba"); that you have in the original code, but I discovered that in the following line of code:

while(aux2<=exp)
{
    t=t*x;
    aux++;
}

You increase to aux instead of aux2 , and therefore never leave another infinite cycle.

This is the resulting code (I changed some things because I was lost reading, I hope it will please everyone.: P) :

#include <stdio.h>

int main(void)
{
    float x = 0.00f, cos = 0.00f, t = 0.00f;
    int n = 0, z = 0, r = 0, exp = 0, aux = 0, aux2 = 0, aux3 = 0;

    printf("Introduzca el valor de x(real): ");
    scanf("%f%*c", &x);
    printf("Introduzca el valor de n(natural): ");
    scanf("%d[^\n]", &n);

    while(aux <= n) {
        aux3 = 2 * aux;
        r = aux3;
        exp = 2 * aux;
        t = x;
        aux2 = 2;

        if(exp == 0) 
            t = 1;

        while(aux2 <= exp) 
            t = t * x, aux2++; // <- En esta línea exactamente.

        while(aux3 > 1) 
            aux3--, r = r * aux3;

        r = (aux3 == 0) ? 1 : r;
        z = (aux % 2 == 0) ? 1 : -1;
        cos = cos + (t / r) * z;
        aux++;
    }

    printf("cos(%f) = %f\n", x, cos);
    return 0;
}

(Even though the program's response is wrong, it does not enter any infinite cycle)

    
answered by 24.03.2017 в 20:14