I am unable to pass one vector per parameter in C

Asked

Viewed 568 times

0

I am trying to pass a vector per parameter to a function in C, but I am going through 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.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

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

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

I do not know if the formula is correct and probably can be optimized more, but I gave a good improvement. It became easier to understand the code this way. Without being able to read easily 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I was trying to solve this issue using VS, but it keeps giving error expression must have a constant value in the statement float f1[n], f2[n];. It’s a compiler thing that VS uses?

  • @Pablovargas must be using the C++ compiler that does not accept, or have some configuration to prevent use.

  • Yes, I imagine it’s better to use a C++ Dev than Visual Studio.

  • Strange is that he is compiling in Coding Ground that uses gcc, but in the terminal is giving error.

  • @Jeffersonbarbosa What a mistake?

  • "error: ờ for' loop initial declarations are only allowed in C99 mode"

  • Are you using a very old compiler, or are in a bad pattern and need to link this with -std=c99, you can resolve this by leaving the variable declaration outside the for. But if the compiler is old it should also not accept to declare array with variable. Unless the compiler is lame. Which compiler and version is using?

  • It’s gcc 4.8.4 on Ubuntu

  • So I guess all we had to do was use the -std=c99 in the compiler command line.

Show 4 more comments

Browser other questions tagged

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