Kruskal algorithm ordered with Selection Sort

Asked

Viewed 71 times

0

I’m trying to compare two variables of a struct type, but it’s 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;
        }

Complete code: https://pastebin.com/vQTbkY60

  • 2

    The error line in your code is not as you have here in the question. Change the code you have in the question so that it is not different, and also take advantage and put the error that the compiler gives.

  • 2

    C++ does not support comparison between two structs, unless you overload the operator.

1 answer

0

Looking at your code I noticed some things that are irregular like the statement of the type struct.( How to declare a struct in C). The second point is that C and C++ do not support the comparison between two structs. Follow 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;
}

Browser other questions tagged

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