Advertisement
theelitenoob

Vertical Histogram Program

Jun 18th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include <stdio.h> // vertical word length histogram program, copyright theelitenoob
  2.  
  3. int main(int argc, char *argv[]){
  4.     int t[10 + 1] = {0}, wordLength = 0, maxValue = 0, c, i, j; // declare variables
  5.     while((c = getchar()) != '\n') // get input
  6.         (c != ' ' && c != '\t' && c != '\r') ? ++wordLength : ((wordLength > 10) ? ++t[10] : ++t[wordLength - 1], wordLength = 0);
  7.     for(i = 0; i != 10 + 1; ++i) // set up array
  8.         if(t[i] > maxValue) maxValue = t[i];
  9.     for(i = maxValue; i != 0; --i, putchar('\n')){ // put the new lines you need
  10.         for(j = 0; j != 10 + 1; ++j){ // print out the x for the row
  11.             (t[j] >= i) ? printf(" X ") : printf("   ");
  12.             putchar(' '); // blank space
  13.         }
  14.     }
  15.     for(i = 0; i != (10 + 1)*4 - 1; ++i, putchar('-')); // row separator
  16.     for(putchar('\n'), i = 0; i != 10 + 1; ++i)
  17.         (i != 10) ? printf("%03d ", i + 1) : printf(">%02d", 10); // print out the numbers for word lengths
  18.     return 0; // end program
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement