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

Asked

Viewed 219 times

0

In the vector below 300 elements, I need to store the distinct elements that occur in another vector called values. Then I have to record how many times the i-th value of the vector values occurs in the first vector. I started to make the vector code of 300 values but then do not know from there, I thank 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;
}
  • It is not every C compiler that correctly interprets functions declared within functions. I can also observe that you are taking the closed interval of [0,300], which gives a total of 301 elements; leave the end of the range open: [0,300)

  • Also, to check the number typed, do not check by i, but for A[i]

  • Related: https://answall.com/q/217720/64969

  • Do the elements need to be in the single vector in the same order as in the original vector? Because my first reflex is to sort the vector using qsort() and then copy each new value as soon as it changes in the ordered vector, copying the same beforehand to a array draft, if necessary.

1 answer

0

Good morning, first I strongly suggest that you fix your code and work with Linked list, which is much easier. But let’s answer:

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

If I understand correctly, to each element that you insert, if it is different from the already existing wants to store in another vector.. da to do so so:

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 have not tested the code, but the logic is there. (possibly this addition to the vector is wrong).

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

For this it is possible to 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 helped.. note that in both functions the vector call is probably wrong, so you’ll need to adjust a little bit..

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

  • Your example gave compilation error in ideone: expected declaration specifiers or ‘...’ before ‘*’ token

  • The counting approach takes a step for each element of the value vector, so its algorithm has quadratic time. Depending on how the code is validated, it may not pass validation because it is not very performative

  • The compilation problem occurs because, unlike the definition of multiple local variables, the definition of parameters cannot have the type elided; so one can say int a, b;, but not int func(int a, b);, for example.

Browser other questions tagged

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