1
votes

I'm coding a something that should find an optimal answer to the following problem:

You have a vector of ints that you have to put in decrescent order by doing the "flip" operation, that is, given an i position in the vector of size n you should switch the values of i and n-1, the values of i+1 and n-2 and so on and so forth. The goal is to find and optimal way to put the vector in order, that is, with the minimal number of flips

I decided to use the A* algorithim to find the answer, since it's important to find an optimal answer, and to store all the information I needed to, I choose to use the following struct:

typedef struct{
            int *v, *flips,g, h;
} node;

in which v would be a state of the solution, flips would store my choices, g would hold the value of the g function, and h the value of the heuristic.

So to write the program I made a vector of the type node as in:

estados=malloc(n*sizeof(node));

and the idea was to use each position of the node vector to store a state so it could be compared and expanded and etc. For the most part the compiler isn't complaining much but in these functions:

int funcaoh(node estados,int n,int goal[], int pos){
    int cont=0, i;
    for(i=0;i<n;i++){
        if(estados[pos].v[i]!=goal[i]){
            cont++;
        }
    }
    return (cont/2);
}

void criaNode(node x[], int n, int pos){
    int i;
    x[pos].v =malloc(n*sizeof(int));
    x[pos].flips =malloc(n*sizeof(int));
    for(i=0;i<n;i++){
        x[pos].flips[i]=-1;
        x[pos].v[i]=-1;
    }
    x[pos].h=0;
    x[pos].g=0;
    return;
}

I get the error "error: subscripted value is neither array nor pointer nor vector" in the if statement of the funcaoh function and the "warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]" warning on the first malloc statement but not on the second oddly enough. The same happens in another few similar instances of the code, I suspect the warnig might just be me using malloc in the wrong way because I'm getting the same warning in other places that really shouldn't be a problem such as in:

 entrada=malloc(n * sizeof(int));

But the main thing is getting the struct vector working because it's obviously quite integral to the success of the program. If I were to guess, I suppose it has to do with the use of struct as a vector, maybe it doesn't work the way I think it does? I don't know, though.

P.S. I'm Brazilian so some parts of the code are written in Portuguese, mostly just the names of variable and of functions, if necessary I could translate them.

1
Note that the C standard does not use the term "vector", but the more basic "array". - too honest for this site
As you yourself admitted, it would be more consistent to give both functions and variables in your code meaningful English names. I am not a native English speaker either, nor are programmers around me in Paris, but I do enforce this rule at my company and all other places I have been in charge of. - chqrlie

1 Answers

2
votes

In funcaoh():

int funcaoh(node estados,int n,int goal[], int pos){

estados is a simple structure, not a pointer to a structure, so you can't subscript it, etc. That's what the compiler said; I have to agree with it.

You presumably need one of these (they're equivalent):

int funcaoh(node *estados, int n, int goal[], int pos) { … }

int funcaoh(node estados[], int n, int goal[], int pos) { … }