SHARE
TWEET

ASCII Histogram Printing

ISSOtm Feb 15th, 2018 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Prints a histogram using the given `values` array.
  2. // The supplied `values` array should have values in range [0.0, 1.0], and contain at least `nb_values` entries.
  3. // `nb_values` represents the number of values to be printed (and thus the lengths of both arrays).
  4. // `descriptions` must contain strings labelling each entry. At least `nb_values` strings are expected.
  5. void print_histogram(double* values, uint8_t nb_values, char** descriptions) {
  6.     for(int8_t slice = 9; slice != -1; slice--) {
  7.         printf("%2d%% ", slice * 10);
  8.        
  9.         for(uint8_t valueID = 0; valueID < nb_values; valueID++) {
  10.        
  11.             putchar( (values[valueID] > (double)slice / 10) ? '|' : ' ');
  12.             // Pad to the right
  13.             for(uint8_t i = strlen(descriptions[valueID]); i != 0; i--) {
  14.                 putchar(' ');
  15.             }
  16.            
  17.         }
  18.         putchar('\n');
  19.     }
  20.    
  21.     printf("    ");
  22.     for(uint8_t valueID = 0; valueID < nb_values; valueID++) {
  23.         printf("%s ", descriptions[valueID]);
  24.     }
  25.     putchar('\n');
  26. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top