Kruskal's Algorithm sorted with selection sort

0

I am trying to compare two variables of a struct type, however it is returning error.

struct aresta{
int v1;
int v2;
int peso;
};

...

 aresta peso[62816];
    aresta aux;
    int i, j=0;

  for(i=0;  i<(tamanhoVetor - 1); i++){
    int menor = i;
     for(j=(i+1); j<tamanhoVetor; j++){
          if(peso[menor] > peso[j])){ //ERRO RETORNA DESTA LINHA
            menor = j;
          }
        }
        aux = peso[menor];
        peso[menor] = peso[i];
        peso[i] = aux;
        }

Full Code: link

    
asked by anonymous 11.06.2018 / 09:45

1 answer

0

Viewing your code I noticed some things that are jagged like the struct declaration. ( Declaring a struct in C ). The second point is that C and C ++ do not support the comparison between two structs . Here is a code that will help you solve your problem.

#include <stdio.h>
typedef struct {
        int v1;
        int v2;
        int peso;
    } aresta;

aresta menorAresta(aresta a, aresta b){
    //utilizar a forma como você quer comparar a estrutura
    if(a.peso > b.peso){
        return b;
    }
    else{
        return a;
    }
}
int main()
{

    aresta a ={.v1=1,.v2=2,.peso=2};
    aresta b ={.v1=1,.v2=2,.peso=2};
    aresta c =menorAresta(a,b);
    int i = c.peso;
    printf("%d",i);

    return 0;
}
    
22.08.2018 / 14:46