Pass vector as argument to a [closed]

-1

I'm trying to pass a vector to a function in C, but I do not know what I'm doing wrong.

As the code is small, I'll post the entire one:

#include <stdio.h>

int calcula(double A){
    if(A<=10.0){
        resposta = A;
    }

    return resposta;
}

int main(){     
    double A[100];
    int i;

    for(i=0; i<=99; i++){
        scanf("%lf", &A[i]);
    }      

    for(i=0; i<=99; i++){
        calcula(A[i]);
        printf("A[%d] = %.1lf\n",i, A[i]);
    }

    return 0;
}
    
asked by anonymous 26.02.2018 / 19:06

1 answer

1

This code does not make much sense, the question does less.

You are not passing a vector, it is passing a single value. If you want to pass a vector do this and treat it, but everything indicates that just passing the value itself, and this is correct.

What is wrong is the function's internal code. First, that relational comparison with a double number does not always work, it has no exactness. Second you are putting a value in resposta and this variable does not even exist. so the compiler should be reporting this. If this is the variable neither is needed.

Even if you declare a variable need to initialize with an invalid value. What would he be? 0? A negative? A NaN? Another thing? Then you would have to change the return type.

To tell the truth maybe it should only return true or false, or even that since the code is too simple. No matter what comes back if you have to filter something on main() you have to have if there too, otherwise it even works as long as you choose a value that will be invalid, but it will print normally if you do not have a filter .

We can not know exactly what the code should do, so it is not possible to help more than this, but the error is this that I have demonstrated.

    
26.02.2018 / 19:16