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

  1. /*                        epp4.c
  2.  *
  3.  *   Synopsis  - Accepts input of 6 stock prices into a
  4.  *               two-dimensional array and should display
  5.  *               the percent change from the old prices. 
  6.  *
  7.  *   Objective - To provide practice with the C syntax and 
  8.  *               the relationship between different 
  9.  *               expressions.
  10.  */
  11.  
  12. /* Include Files */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. /* Constant Definitions */
  17. #define BUF_SIZE        20
  18.  
  19. /* Global Variables */
  20. double stock_prices[2][6] = { {  12, 80, 30,
  21.                                 100, 40, 50  } };
  22.  
  23. /* Function Prototypes */
  24. /* Add PRECONDITION and POSTCONDITION information for 
  25.  * each function
  26.  */
  27. void output_change( double *new, double *old );
  28. void input_prices( void );
  29.  
  30. int main( void )
  31. {
  32.      printf( "STOCK INFORMATION PROGRAM\n");
  33.      printf( "-------------------------\n");
  34.      printf( "This program will give information about the " );
  35.      printf( "prices of \nsix specific stocks. You are to " );
  36.      printf( "enter the current prices of \nthe stocks. The ");
  37.      printf( "percent change from the buying prices\n" );
  38.      printf( "will be displayed.\n\n" );
  39.  
  40.      input_prices();
  41.      output_change( *stock_prices, *( stock_prices+1 ));
  42.      return 0;
  43. }
  44.  
  45. /*********************** input_prices() **********************/
  46. /*  Accepts input of 6 quantities of type float to be 
  47.  *  stored in the second "row" of the array. The 
  48.  *  combination of library functions fgets() and atof()
  49.  *  is used to input the float value.
  50.  */
  51.  
  52. void input_prices( void )
  53. {
  54.      int i;
  55.      char instring[BUF_SIZE];
  56.  
  57.      printf( "Please enter the current stock prices now.\n" );
  58.      printf( "Stock\tBuying price\tCurrent price\n" );
  59.      printf( "-----\t------------\t-------------\n" );
  60.      for ( i = 0; i < 6; i++ ) {
  61.           printf( "#%d: \t%6.2f   \t  ",
  62.                                  i+1, stock_prices[0][i] );
  63.           fgets( instring, BUF_SIZE, stdin );
  64.           stock_prices[1][i] = atof( instring );
  65.      }
  66.      printf( "Thank you!\n" );
  67. }
  68.  
  69. /*********************** output_change() ************************/
  70. /*  Outputs the percentage change of the new prices in
  71.  *  comparison with the old.
  72.  */
  73. void output_change( double *old, double *new )
  74. {
  75.      double change;
  76.      int i;
  77.  
  78.      printf( "\nPercent change of each stock:\n" );
  79.      for ( i = 0; i < 6; i++ ) {
  80.           printf( "Stock #%d: ", i );
  81.  
  82.           /* Calculate the percent change */
  83.           change = ( *new++ / *old++ - 1 ) * 100;
  84.           printf( "Percent change is %3.0f%%.\n", change );
  85.      }
  86. }
  87.