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 / stocks.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  3KB  |  89 lines

  1. /*                    stocks.c
  2.  *
  3.  *   Synopsis  - Accepts the input of six stock prices into a 
  4.  *               two-dimensional array. 
  5.  *
  6.  *   Objective - Illustrates the declaration, initialization, and
  7.  *               accessing of elements of two-dimensional arrays.
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. /* Constant Definitions */
  16. #define ROWS          2
  17. #define COLUMNS       6
  18. #define LGTH         20
  19.  
  20. /* Function Prototypes */
  21. void input_prices( void );
  22. /* PRECONDITION:  none.
  23.  * POSTCONDITION: Accepts input of 6 quantities of type float to be 
  24.  *                stored in the second "row" of the global array 
  25.  *                stock_prices. The combination of library functions 
  26.  *                fgets() and atof() is used to perform the input of 
  27.  *                the float values.
  28.  */
  29.  
  30. void process( void );
  31. /* PRECONDITION:  none.
  32.  * POSTCONDITION: This is a stub function.
  33.  */
  34.  
  35. void output_results( void );
  36. /* PRECONDITION:  none.
  37.  * POSTCONDITION: This is a stub function.
  38.  */
  39.  
  40. /* Global Variables */                                    /* Note 1 */
  41. double stock_prices[ROWS][COLUMNS] = { { 12.5, 76.125, 34.875,
  42.                                          112,  43.25,  88      } };
  43. int main( void )
  44. {
  45.      printf( "STOCK INFORMATION PROGRAM\n");
  46.      printf( "-------------------------\n");
  47.      printf( "This program will give information about the " );
  48.      printf( "prices of \nsix specific stocks. You are " );
  49.      printf( "to enter the current prices of\nthe stocks. " );
  50.      printf( "A comparison will be made with the buying " );
  51.      printf( "prices\nof the stocks.\n\n" );
  52.  
  53.      input_prices();
  54.      process();
  55.      output_results();
  56.      return 0;
  57. }
  58.  
  59. /******************************* input_prices() **********************/
  60. void input_prices( void )
  61. {
  62.      int i;
  63.      char instring[LGTH];
  64.  
  65.      printf( "Please enter the current stock prices now.\n" );
  66.      printf( "Stock\tBuying price\tCurrent price\n" );
  67.      printf( "-----\t------------\t-------------\n" );
  68.      for ( i = 0; i < COLUMNS; i++ ) {
  69.                                                            /* Note 2 */
  70.           printf( "#%d: \t%6.2f   \t  ", i+1, stock_prices[0][i] );
  71.           fgets( instring, LGTH, stdin );
  72.                                                            /* Note 3 */
  73.           stock_prices[1][i] = atof( instring );
  74.      }
  75.      printf( "Thank you!\n" );
  76. }
  77.  
  78. /******************************* process() ***************************/
  79. void process( void )
  80. {
  81.      printf( "process() stub entered.\n" );
  82. }
  83.  
  84. /******************************* output_results() ********************/
  85. void output_results( void )
  86. {
  87.      printf( "output_results() stub entered.\n" );
  88. }
  89.