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

#define NUM_NUM 16

int findMax(int *tally) {
		int i, max;
		max = tally[0];
		for (i = 1; i < NUM_NUM; i++) 
				if (tally[i] > max) max = tally[i];
		return max;
}

int main(int argc, char *argv[]) {
		int *array, *count, elements, i;
		FILE *out = fopen("tally6.txt","w");

		srand(time(0));
		printf("Number of elements? ");
		scanf("%d", &elements);
		array = (int *)malloc(elements * sizeof(int));
		count = (int *)malloc(NUM_NUM * sizeof(int));
		for (i = 0; i < elements; i++) array[i] = rand()%NUM_NUM;
		for (i = 0; i < NUM_NUM; i++) count[i] = 0;

		for (i = 0; i < elements; i++) count[array[i]]++;
		fprintf(out, "Value    ");
		for (i = 0; i < NUM_NUM; i++) fprintf(out, "\t%d",i);
		fprintf(out, "\nFrequency");
		for (i = 0; i < NUM_NUM; i++) fprintf(out, "\t%d", count[i]);
		fprintf(out, "\nThe most frequent number(s) show(s) up ");
		fprintf(out, "%d times.\n", findMax(count));

		free(array);
		free(count);
		return 0;
}
