Problem with vector return in c ++

1

I'm trying to do a program to sort vector and I'm using an auxiliary function to do this, but I'm getting this error message.

/media/isaque/dados/exercicios/c/scripts/estrutura de dados com c++/busca_binaria.cpp: In function ‘void ordenar_vetor(int*)’:
/media/isaque/dados/exercicios/c/scripts/estrutura de dados com c++/busca_binaria.cpp:38:7: warning: variable ‘aux’ set but not used [-Wunused-but-set-variable]
   int aux;

This is my script

#include <iostream>
#include <random>
#define TAM 15

int main()
{
  void ordenar_vetor(int *vetor);
  int gerar_aleatorio();

  int valores[TAM];

  for(int c = 0; c < TAM; c++ )
  {
    valores[c] = gerar_aleatorio();
  }

  ordenar_vetor(valores);

  for(int c = 0; c < TAM; c++ )
  {
    std::cout << valores[c] << std::endl;
  }

  return 0;
}



int gerar_aleatorio()
{
  std::random_device m;
  std::uniform_int_distribution<int> gerar(1,100);
  return gerar(m);
}

void ordenar_vetor(int *vetor)
{
  int aux;

  for(int c = 0; c < TAM; c++)
  {
    for(int i = 0; i < TAM; c++)
    {
      if(vetor[c] < vetor[i])
      {
        aux = vetor[i];
        vetor[i] = vetor[c];
        vetor[c] = vetor[i];
      }
    }
  }
}

I can not identify where I'm going wrong

    
asked by anonymous 27.03.2018 / 19:28

1 answer

3

There are two errors at the end. You gave aux , but never used it. It is also increasing c in the two loops for , when it should increase i in the second loop. I think you wanted to:

for(int c = 0; c < TAM; c++)
{
    for(int i = 0; i < TAM; i++)
    {
        if(vetor[c] < vetor[i])
        {
            aux = vetor[i];
            vetor[i] = vetor[c];
            vetor[c] = aux;
        }
    }
}

The original code would give the value of vetor[c] to vetor[i] , but would not change the value of vetor[c] , because vetor[i] has already changed. Using aux saves the value of vetor[i] to change the value of vetor[c] correctly.

Also, in the original code, i always has a 0 value, so the inner loop never ends.

    
27.03.2018 / 19:39