c - Putting output from rand() into an array -
i wrote small dice rolling program print out results of many dice rolls entered. want count how each number occurs thought put output rand() function array , search array different values. don't know how put numbers array not entered in manually.
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int count; int roll; srand(time(null)); printf("how many dice being rolled?\n"); scanf("%d", &count); printf("\ndice rolls\n"); (roll = 0; roll < count; roll++) { printf("%d\n", rand() % 6 + 1); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int count; int roll; int* history; srand(time(null)); printf("how many dice being rolled?\n"); scanf("%d", &count); history = malloc( sizeof(int) * count ); if( !history ) { printf( "cannot handle many dice!\n" ); exit( -1 ); } printf("\ndice rolls\n"); (roll = 0; roll < count; roll++) { history[roll] = rand() % 6 + 1; printf("%d\n", history[roll]); } // interesting history here free( history ); return 0; }