home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-3.ZIP / EXAMPLES.ZIP / PLOTEMP4.C < prev    next >
C/C++ Source or Header  |  1992-02-18  |  3KB  |  130 lines

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