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 / bug.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  59 lines

  1. /*               bug.c
  2.  *
  3.  *   Synopsis  - Supposed to accept input values of 10
  4.  *               integer values for the array, add 5 to
  5.  *               each value, and display the values in the 
  6.  *               array, but it doesn't work.
  7.  *
  8.  *   Objective - To provide practice in finding a common 
  9.  *               mistake.
  10.  */
  11.  
  12. /* Include Files */
  13. #include <stdio.h>
  14.  
  15. /* Function Declarations */
  16. void init_array( int[], int );
  17. void add_five( int[], int );
  18. void print_array( int[], int );
  19.  
  20. int main( void )
  21. {
  22.      int intarray[10];
  23.  
  24.      init_array( intarray, 10 );
  25.      add_five( intarray, 10 );
  26.      print_array( intarray, 10 );
  27.      return 0;
  28. }
  29.  
  30. void init_array( int array[], int numelts )
  31. {
  32.      int i;
  33.  
  34.      printf( "Please enter values for the array:\n" );
  35.      for ( i = 0; i < numelts; i++ ) {
  36.           printf( "%d: ", i );
  37.           scanf( "%d", array+i );
  38.      }
  39. }
  40.  
  41. void add_five( int array[], int numelts )
  42. {
  43.      int i = -1;
  44.  
  45.      printf("\nAdding five to each element of the array.\n");
  46.      while ( i++ < numelts - 1 );
  47.           array[i] += 5;
  48. }
  49.  
  50. void print_array( int array[], int numelts )
  51. {
  52.      int i;
  53.  
  54.      printf( "\nThe values in the array are:\n" );
  55.      for ( i = 0; i < numelts; i++ )
  56.           printf( "%d\t", *( array+i ) );
  57.      printf( "\n" );
  58. }
  59.