home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c021 / 7.img / EXAMPLES.ZIP / PLOTEMP4.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-04  |  3.0 KB  |  128 lines

  1. /* PLOTEMP4.C--Example from Chapter 7 of Getting Started */
  2.  
  3. /* This program creates a table and a bar chart plot from a
  4.    set of temperature readings */
  5.  
  6. #include <conio.h>
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. /* Prototypes */
  12.  
  13. void   get_temps(void);
  14. void   table_view(void);
  15. void   min_max(int num_vals, int vals[], int *min_val, int *max_val);
  16. float  avg_temp(int num_vals, int vals[]);
  17. void   graph_view(void);
  18. void   save_temps(void);
  19. void   read_temps(void);
  20.  
  21. /* Global defines */
  22.  
  23. #define TRUE      1
  24. #define READINGS  8
  25.  
  26. /* Global data structures */
  27.  
  28. int temps[READINGS];
  29.  
  30. int main(void)
  31. {
  32.    while (TRUE)
  33.    {
  34.       printf("\nTemperature Plotting Program Menu\n");
  35.       printf("\tE - Enter temperatures for scratchpad\n");
  36.       printf("\tS - Store scratchpad to disk\n");
  37.       printf("\tR - Read disk file to scratchpad\n");
  38.       printf("\tT - Table view of current data\n");
  39.       printf("\tG - Graph view of current data\n");
  40.       printf("\tX - Exit the program\n");
  41.       printf("\nPress one of the above keys: ");
  42.  
  43.       switch (toupper(getche()))
  44.       {
  45.      case 'E': get_temps();  break;
  46.      case 'S': save_temps(); break;
  47.      case 'R': read_temps(); break;
  48.      case 'T': table_view(); break;
  49.      case 'G': graph_view(); break;
  50.      case 'X': exit(0);
  51.       }
  52.    }
  53. }
  54.  
  55. /* Function definitions */
  56. void  get_temps(void)
  57. {
  58.    char inbuf[130];
  59.    int reading;
  60.  
  61.    printf("\nEnter temperatures, one at a time.\n");
  62.    for (reading = 0; reading < READINGS; reading++)
  63.    {
  64.       printf("\nEnter reading # %d: ", reading + 1);
  65.       gets(inbuf);
  66.       sscanf(inbuf, "%d", &temps[reading]);
  67.  
  68.       /* Show what was read */
  69.       printf("\nRead temps[%d] = %d", reading, temps[reading]);
  70.    }
  71. }
  72.  
  73. void  table_view(void)
  74. {
  75.    int reading, min, max;
  76.  
  77.    clrscr();                                  /* clear the screen */
  78.    printf("Reading\t\tTemperature(F)\n");
  79.  
  80.    for(reading = 0; reading < READINGS; reading++)
  81.       printf("%d\t\t\t%d\n", reading + 1, temps[reading]);
  82.  
  83.    min_max(READINGS, temps, &min, &max);
  84.    printf("Minimum temperature: %d\n", min);
  85.    printf("Maximum temperature: %d\n", max);
  86.    printf("Average temperature: %f\n", avg_temp(READINGS, temps));
  87. }
  88.  
  89. void  min_max(int num_vals, int vals[], int *min_val, int *max_val)
  90. {
  91.    int reading;
  92.  
  93.    *min_val = *max_val = vals[0];
  94.  
  95.    for (reading = 1; reading < num_vals; reading++)
  96.    {
  97.       if (vals[reading] < *min_val)
  98.      *min_val = (int)&vals[reading];
  99.       else if (vals[reading] > *max_val)
  100.      *max_val = (int)&vals[reading];
  101.    }
  102. }
  103.  
  104. float avg_temp(int num_vals, int vals[])
  105. {
  106.    int reading, total = 1;
  107.  
  108.    for (reading = 0; reading < num_vals; reading++)
  109.       total += vals[reading];
  110.  
  111.    return (float) total/reading; /* reading equals total vals */
  112. }
  113.  
  114. void  graph_view(void)
  115. {
  116.    printf("\nExecuting graph_view().\n");
  117. }
  118.  
  119. void  save_temps(void)
  120. {
  121.    printf("\nExecuting save_temps().\n");
  122. }
  123.  
  124. void  read_temps(void)
  125. {
  126.    printf("\nExecuting read_temps().\n");
  127. }
  128.