How to show the number that most appears in a vector in the C language?

Asked

Viewed 1,330 times

-5

Given a sequence of N numbers between 0 and 100. Determine the highest frequency value.
Follow what I’ve done so far:

#include <stdio.h>
#define max 100

int main()
{    
    int vetor[max],i,j,frequencia=0,maior=0,tamanho_vetor,cont=0;    

    scanf("%d",&tamanho_vetor);

    if(tamanho_vetor>=1 && tamanho_vetor<=1000000)
    {
        for(i=0; i<tamanho_vetor; i++)    
        {
            scanf("%d",&vetor[i]);

           /* Como vou fazer a comparação, para achar o numero que mais aparece e quantas vezes ele aparece? */

                 if(vetor[i]==)    
                {
                    cont++;
                    frequencia ;
                }                
        }
        printf("%d %d\n",frequencia,cont);
    }

    else
        return 0;    
}
  • 2

    Young man, playing only the enunciation doesn’t help much in understanding your difficulty, click EDIT, add what you’ve tried and explain where you’re having trouble.

  • OK I’ll edit the question

  • Just don’t need to write in high box, no need.

  • Okay, thanks for the tip!

2 answers

0

I’ll send a code that returns the highest frequency of numbers, but which is used for a small range of numbers, I know you want to return the frequency with a higher range of numbers, for that you will have to first sort the numbers by selection and then take the fashion, which is the largest number that repeats.

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


int main() {

int vetor[10];
int j=0;
int tamanho;
int c = 0;
int i = 0;

for (i = 0; i <= 11; i++)
 vetor[i] = 0;

printf("Digite quantidades de numeros que deseja procurar:\n");
 scanf("%d >>", &tamanho);

for (j = 0; j < tamanho; ++j){
 scanf("%d", &c);
 vetor[c]++;
}


int moda = 0;

for (c = 1; c <= tamanho; c++) {
 if (vetor[c] > vetor[moda])
     moda = c;
}

printf("A moda dos valores e': %d", moda);


return 0;
} 

0


Try this way:

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

int main(int argc, char* argv[])
{
    int balde[100];
    //int* amostra;
    int x;
    int n;
    int i;
    int maiorBalde;

    // Esvazia os baldes.
    for (i = 0; i < 100; ++i)
    {
        balde[i] = 0;
    }

    printf("Digite o tamanho da amostra (0 para sair): ");
    scanf("%d", &n);

    if (n)
    {
        //amostra = (int*)malloc(n * sizeof(int));

        fflush(stdin);
        printf("Digite os %d numeros (entre 0 e 100) separados por espacos: ", n);
        for (i = 0; i < n; ++i)
        {
            scanf("%d", &x);
            //amostra[i] = x;
            ++balde[x - 1];
        }

        // Escolhendo o x que tem o maior balde.
        maiorBalde = 0x80000000;
        for (i = 0; i < 100; ++i)
        {
            if (maiorBalde < balde[i])
            {
                maiorBalde = balde[i];
                x = i + 1;
            }
        }

        // Imprime x.
        printf("O valor com maior frequencia e %d, com %d de frequencia\n",
            x, maiorBalde);

        //free(amostra);
    }
}

Browser other questions tagged

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