• We just launched and are currently in beta. Join us as we build and grow the community.

Histogram of the frequencies of characters - C Program

Beater

Third-Party Risk Analyst
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
159
Likes
195
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
A program written in C to print a histogram of the
frequencies of different characters (printable)
in its input as horizontal bars

  1. #include<stdio.h> /* C Standard Input and Output Library*/

  2. /*Variable declarations*/
  3. char

    string_of_characters[

    200

    ]

    ;
  4. int

    character_frequency_counter;
  5. int

    counter;
  6. int

    print_histogram_counter;
  7. int

    output_counter;
  8. char

    frequency_array[

    26

    ]

    [

    2

    ]

    =

    {

    'a'

    ,

    0

    ,

    'b'

    ,

    0

    ,

    'c'

    ,

    0

    ,

    'd'

    ,

    0

    ,

    'e'

    ,

    0

    ,

    'f'

    ,

    0

    ,

    'g'

    ,

    0

    ,

    'h'

    ,

    0

    ,

    'i'

    ,

    0

    ,

    'j'

    ,

    0

    ,

    'k'

    ,

    0

    ,

    'l'

    ,

    0

    ,

    'm'

    ,

    0

    ,

    'n'

    ,

    0

    ,

    'o'

    ,

    0

    ,

    'p'

    ,

    0

    ,

    'q'

    ,

    0

    ,

    'r'

    ,

    0

    ,

    's'

    ,

    0

    ,

    't'

    ,

    0

    ,

    'u'

    ,

    0

    ,

    'v'

    ,

    0

    ,

    'w'

    ,

    0

    ,

    'x'

    ,

    0

    ,

    'y'

    ,

    0

    ,

    'z'

    ,

    0

    }

    ;



  9. /*Functions declaration - prototyping*/
  10. char

    frequency_of_characters (

    char

    string_of_characters[

    50

    ]

    )

    ;
  11. int

    printout_of_character_frequencies(

    char

    frequency_array[

    25

    ]

    [

    2

    ]

    )

    ;

  12. int

    main(

    void

    )

    /* The main method*/
  13. {

    /* Brief detail on the screen about the program*/
  14. printf

    (

    " /*************************************************************************\n

    * Mureithi David Wachira *\n

    * P15/2204/2011 *\n

    * *\n

    * University of Nairobi *\n

    * School of Computing & Informatics *\n

    * *\n

    * Course: PROGRAMMING AND PROBLEM SOLVING (CSC 121) *\n

    * Date: Thursday 11th April 2013 *\n

    * *\n

    * A program to print a histogram of the *\n

    * frequencies of different characters (printable) *\n

    * in its input (draw horizontal bars) *\n

    * *\n

    *************************************************************************/\n

    "

    )

    ;

  15. printf

    (

    "Please enter a string of characters \n

    "

    )

    ;

    /* Prompt to instruct use to enter a string of characters*/
  16. gets

    (

    string_of_characters)

    ;

    /* Capture of the string by the system using function gets()*/

  17. frequency_of_characters (

    string_of_characters)

    ;

    /* Call of function frequency_of_characters()*/
  18. printout_of_character_frequencies(

    frequency_array)

    ;

    /* Call of function printout_of_character_frequencies()*/


  19. return

    0

    ;

    /* An indication that the program runs successfully*/
  20. }

    /* The end of the main method*/



  21. char

    frequency_of_characters (

    char

    string_of_characters[

    50

    ]

    )

    /* Function to calculate the frequencies of the characters and storage of the results in an array*/
  22. {
  23. for

    (

    counter=

    0

    ;

    counter<=

    strlen

    (

    string_of_characters)

    ;

    counter++

    )

    /* Loop to go through the array of characters (string) */
  24. {
  25. for

    (

    character_frequency_counter=

    0

    ;

    character_frequency_counter<=

    25

    ;

    character_frequency_counter++

    )

    /* Loop to go through the frequency array*/
  26. {
  27. if

    (

    frequency_array[

    character_frequency_counter]

    [

    0

    ]

    ==

    string_of_characters[

    counter]

    )

    /* Comparability of a character to characters in the string*/
  28. {
  29. frequency_array[

    character_frequency_counter]

    [

    1

    ]

    =

    (

    frequency_array[

    character_frequency_counter]

    [

    1

    ]

    )

    +

    1

    ;

    /* If character if found its count is incremented by one(1)*/
  30. }
  31. }

  32. }

  33. return

    frequency_array;

    /* The function returns an array to the main method (caller)*/

  34. }


  35. int

    printout_of_character_frequencies(

    char

    frequency_array[

    25

    ]

    [

    2

    ]

    )

    /* Function to print out the results*/
  36. {
  37. printf

    (

    "\n

    \n

    %-10s%-8s%15s\n

    "

    ,

    "Letter"

    ,

    "Frequency"

    ,

    "Histogram"

    )

    ;
  38. for

    (

    output_counter=

    0

    ;

    output_counter<=

    25

    ;

    output_counter++

    )

    /* Loop to print the elements of the frequency array*/
  39. {
  40. printf

    (

    "%3c%12d "

    ,

    frequency_array[

    output_counter]

    [

    0

    ]

    ,

    frequency_array[

    output_counter]

    [

    1

    ]

    )

    ;

  41. for

    (

    print_histogram_counter=

    1

    ;

    print_histogram_counter<=

    frequency_array[

    output_counter]

    [

    1

    ]

    ;

    print_histogram_counter++

    )

    /* Loop to print the horizontal bars of the histogram*/
  42. {

    printf

    (

    "%c"

    ,

    '*'

    )

    ;

    }

  43. printf

    (

    "\n

    "

    )

    ;

    /* Next line*/
  44. }
  45. return

    0

    ;

    /* Ends after the function executes successfully*/
  46. }



  47. <

    c>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top