How to store the distinct elements that occur in another vector?

0

In the vector below 300 elements, I need to store the distinct elements that occur in another vector called values. Since then I have to register how many times the i-th value of the vector values occurs in the first vector. I started to do the code of the vector of 300 values but then I do not know how to do it, thank you for the attention

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {


    void lernumeros(int A[]){
        for(int i = 0; i <= 300; i++) {
        printf("\nDigiite um valor qualquer positivo:\n");
            scanf("%d", &A[i]);
            if(i < 0){ 
                printf("Numeros Negativos não são permitidos!");
                break;
                }
            }
    }





    return 0;
}
    
asked by anonymous 04.07.2017 / 13:27

1 answer

0

Good morning, first of all I suggest strongly that you adjust your code and work with linked list, which is much easier. But let's answer:

  

In the vector below 300 elements, I need to save the elements   distinct values that occur in another vector called values.

If I understand correctly, each element that you insert, if it is different from the existing ones, you want to save it in another vector.

void Valor_Distinto (int Valor, *Valores) {
    int i = 0, valor = 0 ;
    while ( valorVetor != NULL ) {
        valorVetor = Valores[i];

        if(ValorVetor == Valor)
            return;

        i++;
    }

    if(Valores[i] != NULL){
        Valores[i] = Valor;
    }
}

I did not test the code, but the logic is there. (possibly this addition in the vector is wrong).

  

Then I have to register how many times the i-th value of the vector   values occurs in the first vector.

For this you can do another function:

int Ocorrencias (int valor, *Vetor) {
    int i = 0, int ocorrencia = 0;    
    while (Vetor[i] != NULL) {

        if(Vetor[i] == valor)
            ocorrencias++;

        i++;
    }
    return ocorrencias;
}

That's it, I hope it has helped .. note that in both functions the vector call is probably wrong, so you'll have to adjust it a bit ..

Tips: In addition to using linked list, I also recommend that you use MALLOC in these vectors and do not create a function inside the main as in your code. Hugs!

    
04.07.2017 / 15:05