Compress array with equal values C

Asked

Viewed 233 times

0

The problem is the following: Make an algorithm that reads an array A[10]. Then remove the duplicate elements by compressing vector A. Finally insert the value zero in the last elements. Show the updated vector A.

But at last it does not execute, it gives the impression, because it does nothing and does not print the new vector on screen.

follows code below:

#include <stdio.h>
#include <string.h>

/*
Fazer um algoritmo que lê um vetor A[10].
Após, retire os elementos em duplicata compactando o vetor A. 
Por fim inserir o valor zero nos últimos elementos. 
Mostre o vetor A atualizado.
*/

int main(void)
{
// declara variaveis
    //declara vetor unico
    int vet[9];
    //declara contador referente a vetor
    int count;
    //variavel auxiliar zero_final
    int zf;
    //variavel recebimento, recebe valores para testalos com o resto do array
    int rec;
    //variavel incremento auxiliar
    int i;
    //variavel compressao de array
    int comp;



    //variavel 'zf' (zero_final) decrementa valores para incluir zeros no final do vetor
    zf=10;

    /*cabeçalho inicial*/

    //entra com valores
    puts("\t*---------------------------*");
    puts("\t| <<< ENTRE COM VALORES >>> |");
    puts("\t*---------------------------*");
    for(count;count<=9;count++)
        scanf("%i",&vet[count]);    
    count=0;
    //exibe valores ordenados em tela
    for(count;count<=9;count++)
        printf("\t VETOR[%i] = %i\n",count,vet[count]);     
    /*cabeçalho inicial*/

    i=0;
    for(count=0;count<=9;count++)
    {
        rec=vet[count];
        while(i!=9)
        {
            if(rec==vet[count])
            {
                zf--;
                comp=count;
            }
            for(;count<=9;count++)
            {
                vet[count]=vet[count++];
            }
            count=comp;
            i++;
        }
    }

    //inclui valor zero no fim do array
    for(;count<=zf;count++)
    {
        vet[count]=0;
    }   
    //imprime array em tela
    for(count;count<=9;count++)
        printf("\t VETOR[%i] = %i\n",count,vet[count]); 

    printf(" -- PRESSIONE ALGO --");
    return 0;
}
  • Here it rotated and the output was exactly the input vector: http://ideone.com/VWBka4

  • Boot count before the first loop for. In doing so, the result will be what @Andersoncarloswoss quoted.

  • @merchant was not made that already?

  • ta but in fact would have to do what the statement asks, will have how to help me about?

  • @Agenaro was not done, you just declared the variable. Another point is that your vector has size 9, and your statement says it needs a size 10.

  • The problem of lack of initialization also occurs with the variable i.

Show 1 more comment

1 answer

1

As pointed out in the comments, your first problem is the initialization of the comp variable before the for commands. Solving this, a logic problem appears when discovering the repeated variables. I fixed the comp problem and rewrote the search for equal elements. see below.

#include <stdio.h>
#include <string.h>

/*
Fazer um algoritmo que lê um vetor A[10].
Após, retire os elementos em duplicata compactando o vetor A. 
Por fim inserir o valor zero nos últimos elementos.  
Mostre o vetor A atualizado.
*/

int main(void)
{
// declara variaveis
//declara vetor unico
int vet[9];
//declara contador referente a vetor
int count;
//variavel auxiliar zero_final
int zf;
//variavel recebimento, recebe valores para testalos com o resto do array
int rec;
//variavel incremento auxiliar
int comp;
//variavel auxiliar no for


//variavel 'zf' (zero_final) decrementa valores para incluir zeros no final do vetor
zf=10;

/*cabeçalho inicial*/

//entra com valores
puts("\t*---------------------------*");
puts("\t| <<< ENTRE COM VALORES >>> |");
puts("\t*---------------------------*");
for(count = 0;count<=9;count++){
    scanf("%i",&vet[count]);
}

for(count = 0, comp = 9; count <= comp; count++){ //Um for que compara a atual posição com todas as seguintes
    for(rec = count + 1; rec <= comp; rec++){
        if(vet[count] == vet[rec]){ //se houver uma repetição
            /*Por questão de eficiencia, troca-se o valor repetido pelo ultimo, decrementa o tamanho do comprimento "real" do vetor*/
            vet[rec] = vet[comp--];
            rec--;//caso a ultima posição seja repetida
        }
    }

}

//inclui valor zero no fim do array
for(count = comp + 1; count < zf; count++)
{
    vet[count]=0;
}   

//imprime array em tela
for(count = 0; count<=9;count++)
    printf("\t VETOR[%i] = %i\n",count,vet[count]); 

printf(" -- PRESSIONE ALGO --\n");
getchar();
return 0;

}

Browser other questions tagged

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