Thread: histogram

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    53
    Rep Power
    11

    histogram


    I need help getting the **** for the histogram all on one line ....not like at step graph

    Code:
    #include <stdio.h>
    
    int find_max(int, int, int, int, int);
    
    int main()  {
     int A,B,C,D,F;
    
     printf("Please enter five integers:");
     scanf("%d%d%d%d%d", &A,&B,&C,&D,&F);
    
     find_max (A,B,C,D,F);
    
     return(0);
    }
    
     int find_max(int A, int B, int C, int D, int F)  {
       int A_max,B_max, C_max, D_max, F_max;
      
      for(A_max=0;A>A_max;A_max++){
       printf("\n    *");
      }
      for(B_max=0;B>B_max;B_max++){
       printf("      *");
      }
      printf("\n  =========================  ");
      printf("\n    A    B    C    D    F  \n");
      
       
    }
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Is this what you are looking for? http://forums.devshed.com/t87684/s.html

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    53
    Rep Power
    11
    no , what I mean is how can I print out a vertical row of asterisks for a input of N integers.

    like



    *
    * *
    * * *
    * * *
    -------------
    A B C

    users input was 4 3 2
    ..

    -gman932
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Will this work for you?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define NUMDIST 10
    #define MAXNUMS 1000000
    #define MAX_X 10
    
    void printDist(int *bins, FILE *fo){
        int i, j, max=0, numPrint;
        char display[NUMDIST][MAX_X];
    
        //normalize results
        for (i=0; i<NUMDIST; i++){
            if (max < bins[i]) max = bins[i];
            for (j=0; j<MAX_X; j++)
                display[i][j] = ' ';
        }
    
        //place results into display grid
        for (i=0; i<NUMDIST; i++){
            numPrint = (bins[i] / (float)max) * MAX_X;
            for (j=0; j<numPrint; j++)
                display[i][j] = '*';
        }
    
        //output results horizontally
        for (i=0; i<NUMDIST; i++){
            fprintf(fo, "%4d (%9d) ", i, bins[i]);
            numPrint = (bins[i] / (float)max) * MAX_X;
            for (j=0; j<numPrint; j++)
                fputc(display[i][j], fo);
            fprintf(fo, "\n");
        }
    
        fprintf(fo, "\n\n");
    
        //output results vertically
        for (j=MAX_X-1; j> -1; j--){
            for (i=0; i<NUMDIST; i++)
                fprintf(fo, "%c ", display[i][j]);
            fprintf(fo, "\n");
        }
        for (i=0; i<NUMDIST; i++)
            fprintf(fo, "%d ", i);
        fprintf(fo, "\n\n");
    
        return;
    }
    
    int main(){
        int bins[NUMDIST];
        int i, val;
    
        srand(10);//change to get a fresh set of numbers
        printf("Iterations: %d\n", MAXNUMS);
        //zero the input array
        for (i=0; i<NUMDIST; i++){
            bins[i] = 0;
        }
    
        printf("Normal distribution\n");
        //zero the input array
        for (i=0; i<NUMDIST; i++){
            bins[i] = 0;
        }
        //increment for each found number
        //this should generate a 'uniform' distribution
        for (i=0; i<MAXNUMS; i++){
            val = (rand() % NUMDIST) / 3;
            val += (rand() % NUMDIST) / 3;
            val += (rand() % NUMDIST) / 3;
            bins[val]++;
        }
    
        printDist(bins, stdout);
    
        return 0;
    }
    Results:

    Code:
    Iterations: 1000000
    Normal distribution
       0 (    27089) *
       1 (    80961) ***
       2 (   161792) *******
       3 (   216140) **********
       4 (   216114) *********
       5 (   161620) *******
       6 (    90524) ****
       7 (    35936) *
       8 (     8857)
       9 (      967)
    
    
          *
          * *
          * *
        * * * *
        * * * *
        * * * *
        * * * * *
      * * * * * *
      * * * * * *
    * * * * * * * *
    0 1 2 3 4 5 6 7 8 9

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo