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

  1. /*                            cast.c 
  2.  *
  3.  *   Synopsis  -Accepts input of three integers and displays 
  4.  *              their average.
  5.  *
  6.  *   Objective - Illustrates one use of type casts.
  7.  */
  8.  
  9. /* Include Files */
  10. #include <stdio.h>
  11.  
  12. /* Function Prototypes */
  13. void intro( void );
  14. /* PRECONDITION:  none.
  15.  *
  16.  * POSTCONDITION: Displays an introduction to the program.
  17.  */
  18.  
  19. void results( int num1, int num2, int num3 );
  20. /* PRECONDITION:  num1, num2 and num3 can be any integers.
  21.  *
  22.  * POSTCONDITION: Calculates and displays the average of 
  23.  *                its arguments. 
  24.  */
  25.  
  26. int main( void )
  27. {
  28.      int first_num, second_num, third_num;
  29.  
  30.      intro();
  31.  
  32.      scanf( "%d", &first_num );
  33.      scanf( "%d", &second_num );
  34.      scanf( "%d", &third_num );
  35.  
  36.      results( first_num, second_num, third_num );
  37.      return 0;
  38. }
  39.  
  40. /******************************* intro() *************************/
  41.  
  42. void intro( void )
  43. {
  44.      printf( "This program  will calculate the " );
  45.      printf( "average of three integers.\n" );
  46.      printf( "Enter the integers now. " );
  47.      printf( "Press Return after each one.\n" );
  48. }
  49.  
  50. /*******************************results()***********************/
  51.  
  52. void results( int num1, int num2, int num3 )
  53. {
  54.      float average;                                  /* Note 1 */
  55.      int sum;
  56.  
  57.      sum = num1 + num2 + num3;
  58.      average = ( float ) sum / 3;                    /* Note 2 */
  59.  
  60.      printf( "The average of your data is %6.3f.\n", average );
  61. }
  62.  
  63.  
  64.  
  65.