I created a function to receive a structure, but it always returns 0, can anyone help me?

Asked

Viewed 41 times

0

Structure:

typedef struct entrada{

    int M;
    long int Nmax;
    double a0, a1, a2, a3, a4, a5;
    double A, B, e1, e2;
}valor;

Function:

double FuncaoDada(struct entrada valor, double x)
{
    double resultado;
    resultado = ((valor.a0)*(cos((valor.a1)*x))) + ((valor.a2)*(sin((valor.a3)*x))) + (exp(valor.a4*x)) + (valor.a5);
    return resultado;
}

1 answer

0

You have assigned the nickname 'value' to the struct 'input' through typedef, that is, every time you write 'value' in your code, it will interpret as 'struct input', and you have used 'struct input value' in the parameter of your function, thus, the function parameter was interpreted as 'struct input struct input', the right one would be in your struct you remove typedef

struct entrada{
    int M;
    long int Nmax;
    double a0, a1, a2, a3, a4, a5;
    double A, B, e1, e2;
};

Or you use 'value v' in the function parameter

double FuncaoDada(valor v, double x)
{
    double resultado;
    resultado = ((v.a0)*(cos((v.a1)*x))) + ((v.a2)*(sin((v.a3)*x))) + 
    (exp(v.a4*x)) + (v.a5);
    return resultado;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.