Adjust vector post element removal

Asked

Viewed 47 times

0

So, imagine that there is already a function that registers students and there is this struct of students below and a variable that counts how many registered students we have and the constant that controls the size of the type struct array aluno(N_ALUNOS)

typedef struct {
    char nome[25];
    int matricula;
    float notas[2];
    char situacao[10];
} Aluno;

Aluno Alunos[N_ALUNOS];

int alunosCadastrados = 0;

This function(is the problem) takes the position excluded from the vector, the idea of the function is that from a position excluded from the vector all elements that are in a higher position return a position, vector that is the one quoted above

void atualizaArray(int posExcluida){
    // Se a posição excluida não for a última
    if(posExcluida < alunosCadastrados - 1){
        // Percorre todo vetor
        for(int j = 0; j < alunosCadastrados; j++){
            // se o índice atual for maior que a posição excluida do vetor
            if(j > posExcluida){
                Alunos[j - 1].matricula = Alunos[j].matricula;
                strcpy(Alunos[j - 1].situacao, Alunos[j].situacao);
                Alunos[j - 1].notas[0] = Alunos[j].notas[0];
                Alunos[j - 1].notas[1] = Alunos[j].notas[1];
                strcpy(Alunos[j - 1].nome, Alunos[j].nome);
            }
        }
    }
}

In tests done I was defining N_ALUNOS as 3 and when filling the 3 indexes of the vector I tried to delete the first index and when checking the two indexes that remained , were repeated with the values of the 2 index, any suggestion?

1 answer

1


Its code does what is intended, to pull all the elements one position back although it is more complicated than would be necessary. Now remember that what you actually did is copy all the information from each student one position backwards, but the size of the vector remains the same. So the last position, it has repeated information, but it’s supposed to be disregarded, which is something you’re not doing. Need to say that the number of enrolled students decreased doing:

alunosCadastrados--;

And in all the places where you use the vector, you only have to use up to the amount of registered students you have at the moment, so:

for (int i = 0;i < alunosCadastrados;i++){
//             ^^^^^^^^^^^^^^^^^^^^

Simplifying its function to remove and including the missing instruction:

void atualizaArray(int posExcluida){
    if(posExcluida >= 0 &&  posExcluida < alunosCadastrados - 1){
        for(int j = posExcluida; j < alunosCadastrados - 1; j++){
        //              ^--- começa na posExcluida  ^--- termina uma antes do fim
            Alunos[j].matricula = Alunos[j + 1].matricula;
            strcpy(Alunos[j].situacao, Alunos[j + 1].situacao);
            Alunos[j].notas[0] = Alunos[j + 1].notas[0];
            Alunos[j].notas[1] = Alunos[j + 1].notas[1];
            strcpy(Alunos[j].nome, Alunos[j + 1].nome);
        }
        alunosCadastrados--; //ajustar o tamanho
    } 
}

See this example in Ideone

The difference in this version, apart from the size adjusting instruction, is that I start the for directly in the excluded position, and I assign the values with the front element. I can’t go all the way, and I have to finish one before the end.

In contrast to your version, you got one less if within the for and also only travels students from the excluded position, thus becoming more efficient. I took advantage and includes the test to prevent the position to be excluded may be negative.

Browser other questions tagged

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