Thread: problem printing histogram about number frequencies

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    45

    problem printing histogram about number frequencies

    hello everyone ...

    i have this program .. i tried to make it work to give me this result
    output:
    Code:
    input array {1,3,5,1,1,8,6,6,1,3,2,1,6,8,8}
    [0]
    [1] *****
    [2]*
    [3]**
    [4]
    [5]*
    [6]***
    [7]
    [8]***
    [9]

    this is my code
    Code:
    
    
    Code:
    
    int main ( void )
    {
       int i, n,j;
       
       printf ("How many values in your data set? ");
       scanf ("%d", &n);
    
       int list[n];
       for (i=0; i < n; i++) {
          printf ("Enter a value: ");
          scanf ("%d", &list[i]);
          
       }
    
       
       int hist[10];    
    
    
       
       printHistogram ( hist, 10);
      
       return 0;
    }
    
    void printHistogram ( int *list, int n )
    {
        int i, j;
    
       for (i=0; i < n; i++) {
          printf ("[%d] ", i);
          for (j = 1; j <= list[i]; j++)
             printf ("*");
          printf("\n");
        }
       
    }
    

    instead i'm having asterisks at the end of the output

    can you please help me ....
    thank you ..

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Do you know what you are doing at all or are you just randomly cutting and pasting code from various places? These questions are ridiculous between this post and your other one.
    1. What is the point in int hist[10]?
    2. Why do you store all of your input into the list array but then pass the hist array to your print function?
    3. WHERE DID YOU STEAL THIS CODE FROM AND WHY ARE YOU NOT CODING YOURSELF?
    4. Which university in India are you going to?


    Ok that is about all the questions I have for now.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Quote Originally Posted by AndrewHunter View Post
    Do you know what you are doing at all or are you just randomly cutting and pasting code from various places? These questions are ridiculous between this post and your other one.
    1. What is the point in int hist[10]?
    2. Why do you store all of your input into the list array but then pass the hist array to your print function?
    3. WHERE DID YOU STEAL THIS CODE FROM AND WHY ARE YOU NOT CODING YOURSELF?
    4. Which university in India are you going to?


    Ok that is about all the questions I have for now.

    i have read some of your replies to other people ... you don't have to insult people every time
    answering your questions :
    i didn't steal any one's code ( you always say that in your posts )
    this code is given to me as an assignment and i should correct it .. if you notice unlike my previous program i didn't say i wrote it .. so relax .. plus i'm not India!

  4. #4
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    so now i understand there is not point of
    Code:
    int hist [10]
    i deleted this tricky addition ..
    and fix the code to :
    Code:
    void  printHistogram ( int *hist, int n );
    
    int main ( void )
    {
       int i, n,j;
       
    
       
       printf ("How many values in your data set? ");
       scanf ("%d", &n);
    
      int list[n];
       for (i=0; i < n; i++) {
          printf ("Enter a value: ");
          scanf ("%d", &list[i]);
          
       }   
           
       printHistogram ( list, n);
       
       return 0;
    }
    
    void printHistogram ( int *list, int n )
    {
        int i, j;
    
        for (i=0; i < n; i++) {
          printf ("[%d] ", i);
          for (j = 1; j <= list[i]; j++)
             printf ("*");
          printf("\n");
        }
       
    }
    


    i get this as an output:
    Code:
    
    
    Code:
    How many values in your data set? 6
    技Enter a value: 1
    技Enter a value: 1
    技Enter a value: 3
    技Enter a value: 5
    技Enter a value: 6
    技Enter a value: 3
    舷[0] *
    舷[1] *
    舷[2] ***
    舷[3] *****
    舷[4] ******
    舷[5] ***
    
    


    anyone can explain how can i count the frequency of each number by adding asterisks ??
    thank you ..

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You need to look at your entry loop:
    Code:
     for (i=0; i < n; i++) {
          printf ("Enter a value: ");
          scanf ("%d", &list[i]);
    }
    All this does is put values into the array. The layout of the buckets for your histogram is a one to one mapping with the element numbers of your array, e.g. bucket 1 is at array[1]. So how would you increment each bucket depending on input? Can you describe this in words?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Quote Originally Posted by AndrewHunter View Post
    You need to look at your entry loop:
    Code:
     for (i=0; i < n; i++) {
          printf ("Enter a value: ");
          scanf ("%d", &list[i]);
    }
    All this does is put values into the array. The layout of the buckets for your histogram is a one to one mapping with the element numbers of your array, e.g. bucket 1 is at array[1]. So how would you increment each bucket depending on input? Can you describe this in words?
    by using a sort of counter ?
    I'm thinking about a new counter array but i'm still thinking how to use it

  7. #7
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Ok I'm sure that i should use a counter after reading each number into the array. the counter must be
    Code:
    int count[i]=0;
    .
    .
    .
    after reading into array
    count[i]++;
    then use it in for loop and print the asterisks based on this counter...
    am i right here ?
    i tried to make this work...it just didn't

  8. #8
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    hello again ..
    as i said i tried to declare a new array called freq .. to count the frequencies of each number enters the array
    here is what i have :
    Code:
    void  printHistogram ( int *hist,int freq[], int n );
    int main ( void )
    {
       int i, n,j;
       
       printf ("How many values in your data set? ");
       scanf ("%d", &n);
    
       int list[n];
       int freq
    [list]=0;
       
       for (i=0; i < n; i++) {
          printf ("Enter a value: ");
          scanf ("%d", &list[i]);
         freq
    [list[i]]++:
                
       }
       
       printHistogram ( list,10);
     
       return 0;
    }
    
    void  printHistogram ( int *hist,int freq[], int n );
    {
        int i, j;
    
        for (i=0; i < n; i++) 
        {
          printf ("[%d] ", i);
          for (j = 1; j <= freq
    [list[[i]]; j++)
             printf ("*");
          printf("\n");
        }
       
    }
    


    but it's not working
    if any one has any idea how to fix this code ..
    reminder : i didn't write this code .. it's written by the professor and i should correct to make it work..
    thanks ..

  9. #9
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    You call printHistogram with to few arguments.
    Try:
    Code:
    …
        printHistogram (list, freq, 10);
    …
    Last edited by WoodSTokk; 03-16-2015 at 03:38 PM.
    Other have classes, we are class

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    double post with Chromium ??? sorry.
    Last edited by WoodSTokk; 03-16-2015 at 03:37 PM.
    Other have classes, we are class

  11. #11
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Quote Originally Posted by WoodSTokk View Post
    double post with Chromium ??? sorry.
    thanks I fixed it ..
    but still not working

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Make sure your code is neatly formatted and indented. And "paste as text" so the forum formatting can do its own thing. This is closer to what your code should look like:

    Code:
    #include <stdio.h>
    
    void printHistogram ( int *hist,int freq[], int n );
    
    int main ( void )
    {
        int i, n,j;
    
        printf ("How many values in your data set? ");
        scanf ("%d", &n);
    
        int list[n];
        int freq[list]=0;
    
        for (i=0; i < n; i++) {
            printf ("Enter a value: ");
            scanf ("%d", &list[i]);
            freq[list[i]]++:
        }
    
        printHistogram ( list,10);
    
        return 0;
    }
    
    void printHistogram ( int *hist,int freq[], int n );
    {
        int i, j;
    
        for (i=0; i < n; i++)
        {
            printf ("[%d] ", i);
            for (j = 1; j <= freq[list[[i]]; j++)
                printf ("*");
            printf("\n");
        }
    }
    Did you try compiling and running your program? (I doubt it.) Let us know exactly what problems you're having.

    Code:
    int freq[list]=0;
    Explain to us what you think this line does.

    Line 26: You're not supposed to have a semi-colon after the function definition.

  13. #13
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Quote Originally Posted by Matticus View Post
    Make sure your code is neatly formatted and indented. And "paste as text" so the forum formatting can do its own thing. This is closer to what your code should look like:

    Code:
    #include <stdio.h>
    
    void printHistogram ( int *hist,int freq[], int n );
    
    int main ( void )
    {
        int i, n,j;
    
        printf ("How many values in your data set? ");
        scanf ("%d", &n);
    
        int list[n];
        int freq
    [list]=0;
    
        for (i=0; i < n; i++) {
            printf ("Enter a value: ");
            scanf ("%d", &list[i]);
            freq
    [list[i]]++:
        }
    
        printHistogram ( list,10);
    
        return 0;
    }
    
    void printHistogram ( int *hist,int freq[], int n );
    {
        int i, j;
    
        for (i=0; i < n; i++)
        {
            printf ("[%d] ", i);
            for (j = 1; j <= freq
    [list[[i]]; j++)
                printf ("*");
            printf("\n");
        }
    }
    Did you try compiling and running your program? (I doubt it.) Let us know exactly what problems you're having.

    Code:
    int freq
    [list]=0;
    Explain to us what you think this line does.

    Line 26: You're not supposed to have a semi-colon after the function definition.

    yes i fixed these errors ( semicolons , arguments ..)
    this is the code
    Code:
     void  printHistogram ( int *hist,int freq, int n );
    
    /**
     * This program requests data from the user and then
     * computes and prints a histogram. You may assume that
     * the data values entered are in the range of 0 to 9.
     */
    int main ( void )
    {
       int i, n,j;
       
    
       // Data entry
       //
       printf ("How many values in your data set? ");
       scanf ("%d", &n);
    
       int list[n];
       int freq
    [list[n]];
       
       for (i=0; i < n; i++) {
          printf ("Enter a value: ");
          scanf ("%d", &list[i]);
         freq
    [list[i]]++;
                
       }
    
       // Processing data to compute histogram
       
           
    
    
       // Printing histogram
       printHistogram ( list,freq,10);
     
       
       return 0;
    }
    
    void  printHistogram ( int *hist,int freq, int n )
    {
        int i, j;
    
        for (i=0; i < n; i++) 
        {
          printf ("[%d] ", i);
          for (j = 1; j <= freq
    [list[i]]; j++)
             printf ("*");
          printf("\n");
        }
       
    }
    


    i got two errors and a warning:
    Code:
    
    
    Code:
    histogram.c:42:24: warning: 
    舷      incompatible
    舷      pointer
    舷      to
    舷      integer
    舷      conversion
    舷      passing
    舷      'int
    舷      
    [list[n]]'
    舷      to
    舷      parameter
    舷      of
    舷      type
    舷      'int'
    舷      [-Wint-conversion]
    舷  ...freq...
    舷     ^~~~
    霞histogram.c:9:39: note: 
    舷      passing
    舷      argument
    舷      to
    舷      parameter
    舷      'freq'
    舷      here
    舷  ...fr...
    舷     ^
    霞histogram.c:55:29: error: 
    舷      use
    舷      of
    舷      undeclared
    舷      identifier
    舷      'list';
    舷      did
    舷      you
    舷      mean
    舷      'hist'?
    舷  ...list...
    舷     ^~~~
    舷     hist
    霞histogram.c:48:29: note: 
    舷      'hist'
    舷      declared
    舷      here
    舷  ...*h...
    舷      ^
    霞histogram.c:55:28: error: 
    舷      subscripted
    舷      value
    舷      is
    舷      not
    舷      an
    舷      array,
    舷      pointer,
    舷      or
    舷      vector
    舷  ...freq
    [list[i]...
    舷     ~~~~^~~~~~~~
    舷1 warning and 2 errors generated.
    舷


    about your question :
    i was thinking about freq
    [list[i]] as a counter for each entry in the element to count the frequency


  14. #14
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Your function should declare like:
    Code:
    void  printHistogram (int *hist, int *freq, int n );
    'freq' is an array of integers, the argument is therefore 'int *freq' (or 'int freq[]' like before).
    Change this in your definition and declaration.

    Your compiler should warn you about this mistake.
    Are you compiling with the highest warn level?
    (for gcc the option is '-Wall')

    The compiler should also warn you about unused variable j in main.
    Other have classes, we are class

  15. #15
    Registered User
    Join Date
    Mar 2015
    Location
    New Delhi, India, India
    Posts
    1
    Hi Hana

    Try making it incrementally. Assume that all inputs are going to be between 1-10 (both inclusive), doesn't matter how many they are but what values they are you need.

    As for printing histogram,

    Now you know what your inputs are (from 1 - 10). So drop the n from argument.
    You can also drop hist, not using it anyway are you?

    Run the loop over your freq then for each freq run a loop on its count. you should be good.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in histogram word
    By elwad in forum C Programming
    Replies: 1
    Last Post: 04-18-2009, 09:55 AM
  2. printing axis for histogram
    By bazzano in forum C Programming
    Replies: 6
    Last Post: 04-06-2007, 08:42 PM
  3. Language character frequencies
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-13-2005, 10:26 PM
  4. Extracting frequencies from a wav file
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-18-2005, 10:42 AM
  5. problem printing with floating number
    By ssharish in forum C++ Programming
    Replies: 4
    Last Post: 01-25-2005, 07:31 PM