Histogram help

This is a discussion on Histogram help within the C Programming forums, part of the General Programming Boards category; Hi there! I have the following code which generates a population of N Gaussian numbers. Code: #include <stdio.h> #include<math.h> #include ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    20

    Histogram help

    Hi there!
    I have the following code which generates a population of N Gaussian numbers.
    Code:
    #include <stdio.h>
    #include<math.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
    
    int iseed,i,j,N,l,m,k,y,z;
    float a,b,e,stand,c,d,sum=0.00;
    printf("Enter the number of Gaussion numbers needed\n");
    scanf("%d", &N);
    printf("Enter the standard deviation (larger or equal 1) \n");
    scanf("%f", &stand);
    z=(6*stand);
    y=0-z;
    int values[6+z+1]; 
    for(j=0; j<6+z+1; j++) {
    values[j] = 0; /* sets whole array set to 0 */
    }
    iseed=1934871; 
    srand(time(NULL));
    
    for (j=0; j<N; j++) {
    
       for (i=0; i<=12 ; i++)
          {
            a=(float)rand();
    	e=a/(float)RAND_MAX;
            sum = sum + e;
          }
       b=sum-6;
       c=stand*b; 
       
       for (l=y-1; l<=z; l++){ /* For loop starting at a value of -6xstandard dev and going to =6xstandard dev */
       if((l-0.5)<=c)          /* If C falls withing +/- 0.5 of the current value of l then we add 1 to the array values */
       d=c;
       if(d<(l+0.5)) {
       values[l+z] += 1;
       break;
       }                        /* if the if statement is satisfied exit the loop */
       }
         
       sum=0.00;
    }
    for (m=y;m<0;++m){
    printf("%d %d",m,values[m+z]);
    for(k=0; k<(values[m+z]);++k) {
    printf("%c",'*');
    }
    printf("\n");
    }
    for (m=0;m<=z;++m){
    printf(" %d %d",m,values[m+z]);
    for(k=0; k<(values[m+z]);++k) {
    printf("%c",'*');
    }
    printf("\n");
    }
    return 0;
    }
    The programme currently generates a * every time an element is read in the array values[]. However, as I am using large population sizes it would be better if the programme generated a * for every n times the element is read. Could someone provide assistance on how to do this? Many Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    k is the iterator in your for loop that prints out the stars. To get stars printed only on every num k's, instead of every k, you can use:

    Code:
    for(k=0;k<something;k++) {
      if(k % num==0)
        printf("%c", '*');
    }
    Logic like that. k%num will equal zero, when k is zero. If you don't want that add this:

    if(k%num== 0 && k)

    Cheers!

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,784
    A small point to make, there is no need to do printf("%c", '*'). Just do printf("*"), it's much clearer.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,826
    Quote Originally Posted by anduril462
    A small point to make, there is no need to do printf("%c", '*'). Just do printf("*"), it's much clearer.
    It would be even better to just write putchar('*').
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    20
    Cheers for your help guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a histogram program
    By makonikor in forum C Programming
    Replies: 33
    Last Post: 04-17-2010, 01:17 AM
  2. a question on producing a pixel histogram of a picture
    By tracyhaha in forum C Programming
    Replies: 3
    Last Post: 04-07-2007, 07:27 AM
  3. Max & Min value and refine histogram
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-20-2002, 08:04 AM
  4. Still can't print a Histogram from fail input
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-11-2002, 12:24 PM
  5. Help Me ! Print a histogram From a file
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 07-10-2002, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21