Solução: Problema da moda - dificuldade 2 (Eduardo Ramos)

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
#include <stdio.h>
#include <stdlib.h>
 
int main () {
 
	long int number,i=0,j,t,value,aux,moda,cont;
	long int *num=NULL;
 
	do {
		scanf("%ld",&number);
//		while (number < -2147483648 && number > 2147483647)
//			scanf("%ld",&number);
		++i;
		num=realloc(num,sizeof(int)*i);
		if (! num)
			exit(1);
		else
			num[i-1]=number;
	} while (number != 0);
 
 
	for (j=0; j<i-1 ; j++) {
		for (t=j; t<i-1; t++) {
                	if (num[j] >= num[t]) {
                        	aux=num[t];
                                num[t]=num[j];
                                num[j]=aux;
			}
		}
	}
 
 
	value=num[0]; aux=0; moda=0; j=0;
	while (j <= i-1) {
		if (num[j] == value) {
			aux++;
			j++;
		}
		else {
			value=num[j];
			aux=0;
		}
 
		if (aux >= moda) moda=aux;
	}
 
	value=num[0]; cont=0; j=0;
	while (j < i-1) {
		if (value == num[j]) {
			t=j;
			j++;
			cont++;
		}
		else {
			value=num[j];
			cont=0;
		}
 
		if (cont == moda) printf("Moda: %ld\n",num[t]);
	}
 
	return 0;
}