Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int max(int values[], int N) {
  5.     int i, max = values[0];
  6.     for(i=1; i<N; i++) {
  7.         if (max < values[i]) max = values[i];
  8.     }
  9.     return max;
  10. }
  11.  
  12. void histogram(char * result,int * values, int numValues) {
  13.     int i, j, h;
  14.     h = max(values,numValues);
  15.     strcpy(result,"");
  16.     for (i=0; i<(h+2); i++) {
  17.         if (i==0 || i==h+1) {
  18.             for (j=0; j<(numValues+2); j++){
  19.                 strcat(result,"*");
  20.             }
  21.             strcat(result,"\n");
  22.         }
  23.         else
  24.             for (j=0; j<(numValues+3); j++){
  25.                 if (j==0 || j==numValues+1) strcat(result,"*");
  26.                 else if (j==numValues+2)  strcat(result,"\n");
  27.                 else {
  28.                     if (values[j-1] >= h-(i-1)) strcat(result,"X");
  29.                     else strcat(result," ");
  30.                 }
  31.             }
  32.     }
  33. }
  34.  
  35. int main()
  36. {
  37.     int values1[10] = {1,0,3,1,2,4,5,6,2,2};
  38.     int values2[3] = {1,0,1};
  39.     int values3[6] = {3,4,1,0,2,3};
  40.     char * formatted;
  41.     char * example = "*****\n*X X*\n*****";
  42.     histogram(formatted,values1, 10);
  43.     printf("%s\n\n",formatted);
  44.    
  45.     histogram(formatted,values2,3);
  46.     printf("%s\n",formatted);
  47.     if (strcmp(example,formatted) == 0) printf("This matches EXACTLY and is correct");
  48. histogram(formatted,values3,6);
  49. printf("%s\n",formatted);
  50.         return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement