I can not pass one vector per parameter in C

0

I'm trying to pass one vector per parameter to a function in C, but I'm experiencing difficulties.

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

void calculafx( x2, *f3, *f4, m){
    int j;
    float fx1=0;

for(j=1; j<=m;j++){
    fx1= fx1+f3[j]*pow(x2, f4[j]);
}
    return fx1;
}

int main(){
    int cont, n , x, i;
    float epsilon, fa, fb, x1, fxx1, a, b;

    printf("Digite o número de termos da função: \n");
    scanf("%d", &n);
    system("clear");
    float f1[n], f2[n];

    printf("Digite o epsilon: \n");
    scanf("%f", &epsilon);
    printf("Digite o primeiro valor do intervalo: \n");
    scanf("%f", &a);
    printf("Digite o segundo valor do intervalo: \n");
    scanf("%f", &b);
    system("clear");

    for(i=1; i<=n; i++){
        printf("Digite o termo %d da função: \n", i);
        scanf("%f", &f1[i]);
        printf("Digite o grau do termo: \n");
        printf("Digite 0 se o termo for uma constante: \n");
        scanf("%f", &f2[i]);
        system("clear");
    }

    do{
        x1= ((a+b)/2);
        fxx1= calculafx(x1, &f1, &f2, n);
        if((fxx1*calculafx(a, &f1, &f2, n))<0){
            b= x1;
        }
        else{
            a= x1;
        }
    }while(abs(a-b)>epsilon);

    fa= calculafx(a, &f1, &f2, n);
    fb= calculafx(b, &f1, &f2, n);
    printf("f(a) é igual a %f e f(b) é igual a %f \n", fa,fb);
} 

Only the compiler says it expects a ")" before calculafx (x2, * f3 ...) and points to * f3. Could someone tell me where I'm going wrong? I'm using gcc to compile.

    
asked by anonymous 18.02.2017 / 15:29

1 answer

1

The problem is that you did not put type in the parameters and return the function.

It is also wrong to pass the two vectors with the & operator. An array is already a pointer, so this operator should not be used, since it just takes the memory address of the variable.

I do not know if the formula is correct and probably can optimize more, but I've made a good improvement. It was easier to understand the code this way. Without being able to read easy it is more difficult to find the solution.

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

float calculafx(float x2, float *f3, float *f4, int m) {
    float fx1 = 0;
    for (int j = 1; j <= m; j++) {
        fx1 += f3[j] * pow(x2, f4[j]);
    }
    return fx1;
}

int main() {
    int n;
    float epsilon, fa, fb, x1, fxx1, a, b;
    printf("Digite o número de termos da função: \n");
    scanf("%d", &n);
    system("clear");
    float f1[n], f2[n];
    printf("Digite o epsilon: \n");
    scanf("%f", &epsilon);
    printf("Digite o primeiro valor do intervalo: \n");
    scanf("%f", &a);
    printf("Digite o segundo valor do intervalo: \n");
    scanf("%f", &b);
    system("clear");
    for(int i = 1; i <= n; i++) {
        printf("Digite o termo %d da função:\n", i);
        scanf("%f", &f1[i]);
        printf("Digite o grau do termo: \n");
        printf("Digite 0 se o termo for uma constante:\n");
        scanf("%f", &f2[i]);
        system("clear");
    }

    do {
        x1 = (a + b) / 2;
        fxx1 = calculafx(x1, f1, f2, n);
        if (fxx1 * calculafx(a, f1, f2, n) < 0) {
            b = x1;
        } else {
            a = x1;
        }
    } while (abs(a - b) > epsilon);
    fa = calculafx(a, f1, f2, n);
    fb = calculafx(b, f1, f2, n);
    printf("f(a) é igual a %f e f(b) é igual a %f \n", fa,fb);
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
18.02.2017 / 15:44