home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch4 / frequency.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  4KB  |  104 lines

  1. /*                frequency.c
  2.  * 
  3.  *   Synopsis  -  The user specifies how many time a die will be 
  4.  *                rolled. The program dynamically allocates memory 
  5.  *                for an array to store the results when the die is 
  6.  *                rolled and then displays the total number of 
  7.  *                occurrences of each of the results 1, ..., 6.
  8.  *
  9.  *   Objective - To illustrate dynamic allocation of memory.
  10.  */
  11.  
  12. /* Header Files */
  13. #include <stdio.h>
  14. #include <stdlib.h>                                 /* Note 1 */
  15.  
  16. /* Function Prototypes */
  17. void intro( void );
  18. /*   PRECONDITION:  none
  19.  *   POSTCONDITION: announces the purpose of the program
  20.  */
  21.  
  22. void input_rolls( int *arrayptr, int num );
  23. /*   PRECONDITION:  arrayptr points to memory for num ints.
  24.  *   POSTCONDITION: arrayptr has been populated with input
  25.  *                  from the terminal indicating the results
  26.  *                  of each roll of a die.
  27.  */
  28.  
  29. void calculate_and_display( int *array, int num );
  30. /*   PRECONDITION:  array contains num elements with the
  31.  *                  results of num tosses of a die.
  32.  *   POSTCONDITION: the frequency of each possible outcome
  33.  *                  1 to 6 has been displayed on the screen.
  34.  */
  35.  
  36. int main( void )
  37. {    
  38.      int * arrayptr;
  39.      int num_rolls;
  40.      intro();
  41.  
  42.      printf( "How many times did you roll the die? " );
  43.      scanf( "%d", &num_rolls );
  44.                                                     /* Note 2 */
  45.      arrayptr = ( int * )malloc( num_rolls *sizeof( int ) );    
  46.      if ( arrayptr == NULL ) {                      /* Note 3 */
  47.           printf( "Unable to get memory\n" );
  48.           return 1;
  49.      }
  50.                                                     /* Note 4 */
  51.      input_rolls( arrayptr, num_rolls );
  52.                                                     /* Note 4 */
  53.      calculate_and_display( arrayptr, num_rolls );
  54.      free( (void *) arrayptr );                     /* Note 5 */
  55.  
  56.      return 0;
  57. }
  58.  
  59. /*******************************intro()*************************/
  60. void intro( void )
  61. {    
  62.      printf( "DO YOU HAVE A FAIR DIE?\n" );
  63.      printf( "-----------------------\n\n" );
  64.      printf( "Tell me how many times you will roll the die and " );
  65.      printf( "tell me the results of each roll and I will " );
  66.      printf( "calculate the frequency of the results 1 to 6.\n" );
  67. }
  68.  
  69. /*******************************input_rolls()*******************/
  70. void input_rolls( int *arrayptr, int num )
  71. {    
  72.      int i;
  73.  
  74.      printf( "Enter the results of the rolls now.\n" );
  75.  
  76.      for ( i = 0; i < num; i ++ ) {
  77.           printf( "Roll #%d: ", i+1 );
  78.           scanf( "%d", &arrayptr[i] );
  79.                                                    /* Note 6 */
  80.           if ( arrayptr[i] < 0 || arrayptr[i] > 6 ) {
  81.                printf( "Sorry, your input must be a number " );    
  82.                printf( "between 1 and 6.\n" );    
  83.                printf( "Please enter the number again\n" );
  84.                i--;        
  85.           }
  86.      }
  87. }
  88.  
  89. /*******************************calculate_and_display***********/
  90. void calculate_and_display( int *array, int num )
  91. {
  92.      int frequency[6] = {0};
  93.      int i, j;
  94.  
  95.      for ( i = 0; i < num; i++ ) {
  96.           for (j = 0; j < 6; j++ )
  97.                if ( array[i] == j+1 )      /* Note 6 */
  98.                     frequency[j]++;
  99.      }
  100.  
  101.      for ( i = 0; i < 6; i++ )
  102.           printf ( "Frequency of %d's is %f\n", i+1, ( double )frequency[i]/num );
  103. }
  104.