Vector exchange of chars with problem

Asked

Viewed 33 times

0

I’m trying to sort this struct vector that has random int values and a char vector also with random values. But I ran into a problem. The idea is to sort them from the random numbers and the letters must accompany the change of position of the numbers within the struct vector. Before the numbers changed position and the letters did not. This way is almost functional, but the letters are not printed correctly. I would like to understand where the letters are being lost. The code follows below. Thanks in advance!

#include <iostream>
#include <string>
#include <time.h>

struct Jogador {
    char nome[5];
    int pontuacao;
};

void troca(int &a, int &b, char c[], char d[]) {
    int temp;
    char temp1[5];

temp = a;
    a = b;
    b = temp;

for(int i = 0; i < 5; i++) {
    c[i] = temp1[i];
}

for(int i = 0; i < 5; i++) {
    c[i] = d[i];
}

for(int i = 0; i < 5; i++) {
    d[i] = temp1[i];
}

}

void selectionSort(Jogador jogador[], int tamanhoVetor) {
int imin;
for(int i = 0; i < tamanhoVetor - 1; i++) {
   imin = i;
  for(int j = i + 1; j < tamanhoVetor; j++)
     if(jogador[j].pontuacao < jogador[imin].pontuacao)
        imin = j;
  troca(jogador[i].pontuacao, jogador[imin].pontuacao, jogador[i].nome, jogador[imin].nome);
 }
}


Jogador criarVetor(Jogador jogador[], int tamanhoVetor) {

for(int i = 0; i < tamanhoVetor; i++) {
    for(int j = 0; j < 5; j++) {
        jogador[i].nome[j] = rand() % 26 + 65;
    }
}

for(int i = 0; i < tamanhoVetor; i++) {
    jogador[i].pontuacao = rand() % 100000 + 1;
}

return jogador[tamanhoVetor];

}

int main() { 

srand(time(NULL));

int tamanhoVetor;

while(true) {
    std::cout << "Tamanho do vetor: ";
    std::cin >> tamanhoVetor;
    if(tamanhoVetor < 1001 && tamanhoVetor > 0) {
        break;
    }
}

Jogador jogador[tamanhoVetor];

jogador[tamanhoVetor] = criarVetor(jogador, tamanhoVetor);

std::cout << std::endl;

for(int i = 0; i < tamanhoVetor; i++) {
    std::cout << "Jogador[" << i << "] = ";
    for(int j = 0; j < 5; j++) {
        std::cout << jogador[i].nome[j];
    }
    std::cout << " e " << jogador[i].pontuacao << std::endl;
}

std::cout << std::endl;

selectionSort(jogador, tamanhoVetor);

for(int i = 0; i < tamanhoVetor; i++) {
    std::cout << "Jogador[" << i << "] = ";
    for(int j = 0; j < 5; j++) {
        std::cout << jogador[i].nome[j];
    }
    std::cout << " e " << jogador[i].pontuacao << std::endl;
}

return 0;

}

1 answer

0


Your problem consists of a simple assignment error in the exchange() function. In the first go inside the function you made

c[i] = temp1[i];

when in fact you should do

temp1[i] = c[i];

At first you overwritten the function parameter with an uninitialized array. That was the reason why the wrong and repeated names appeared on the way out

  • Thank you so much for the help! Small detail that makes all the difference, I will pay more attention to that hehehe.

Browser other questions tagged

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