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

  1. /*                       avg.c
  2.  *
  3.  *   Synopsis  - Computes and displays the average of
  4.  *               a set of up to 100 integers.
  5.  *
  6.  *   Objective - To illustrate the syntax of a function returning
  7.  *               a noninteger value. Also illustrates the use of
  8.  *               the value returned by scanf().
  9.  */
  10.  
  11. /* Include Files */
  12. #include <stdio.h>
  13.  
  14. /* Constant Definitions */
  15. #define MAX 100
  16.  
  17. /* Function Prototypes */
  18. float average( int *intarray, int count );                 /* Note 1 */
  19. /* PRECONDITION:  intarray is the address of an array of ints. It 
  20.  *                must be declared by the calling function. count is 
  21.  *                the number of items in intarray{}.
  22.  *
  23.  * POSTCONDITION: Calculates and returns the average of the integers 
  24.  *                in the array intarray. 
  25.  */
  26.  
  27. int getinputvalues( int *valarray );
  28. /* PRECONDITION:  valarray is the address of an array of ints. It 
  29.  *                must be declared by the calling function.
  30.  *
  31.  * POSTCONDITION: Reads integers from the keyboard until a 
  32.  *                noninteger is found. Puts the input into the
  33.  *                parameter valarray[]. Returns the number of 
  34.  *                integers entered. 
  35.  */
  36.  
  37. int main( void )
  38. {
  39.      int count;
  40.      int values[MAX];
  41.  
  42.      printf( "Enter your data now. ");
  43.      printf( "Enter one integer per line.\n" );
  44.      printf( "Enter 'Q' when you want to quit.\n" );
  45.  
  46.      count = getinputvalues( values );                       /* Note 2 */
  47.                                                              /* Note 3 */
  48.      printf( "The average is %5.2f.\n", average( values, count ));
  49.      return 0;
  50. }
  51.  
  52. /******************************* getinputvalues() **********************/
  53.  
  54. int getinputvalues( int *valarray )                          /* Note 4 */
  55. {
  56.      int i = 0;
  57.  
  58.      printf( ">  " ); 
  59.                                                              /* Note 5 */
  60.      while ( i < MAX && scanf( "%d", valarray + i )) {
  61.           i++;
  62.           printf( ">  " );
  63.      }
  64.      return ( i );
  65. }
  66.  
  67. /******************************* average() *****************************/
  68.  
  69. float average( int *intarray, int count )                    /* Note 6 */
  70. {
  71.      int i, sum = 0;
  72.  
  73.      if ( count != 0 ) {
  74.           for ( i = 0; i < count; i++ )
  75.                sum += intarray[i];
  76.           return ( ( float ) sum / count );                  /* Note 7 */
  77.      }
  78.      else
  79.           return ( 0.0 );                                     /* Note 8 */
  80. }
  81.