Quantcast
Channel: SourceCodester - Visual Basic, VB.NET, C#, PHP/MySQL, C/C++, Java/JavaScript, ASP/ASP.NET, MS Access, FoxPro, SQL
Viewing full articles (9326 articles)
Browse article index
↧

Histogram of the frequencies of characters - C Program

$
0
0

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.  
  3. /*Variable declarations*/
  4. char string_of_characters[200];
  5. int character_frequency_counter;
  6. int counter;
  7. int print_histogram_counter;
  8. int output_counter;
  9. 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};
  10.  
  11.  
  12.  
  13. /*Functions declaration - prototyping*/
  14. char frequency_of_characters (char string_of_characters[50]);
  15. int printout_of_character_frequencies(char frequency_array[25][2]);
  16.  
  17. int main(void)/* The main method*/
  18. {/* Brief detail on the screen about the program*/
  19. 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");
  20.  
  21. printf("Please enter a string of characters \n");/* Prompt to instruct use to enter a string of characters*/
  22. gets(string_of_characters);/* Capture of the string by the system using function gets()*/
  23.  
  24. frequency_of_characters (string_of_characters);/* Call of function frequency_of_characters()*/
  25. printout_of_character_frequencies(frequency_array);/* Call of function printout_of_character_frequencies()*/
  26.  
  27.  
  28. return0;/* An indication that the program runs successfully*/
  29. }/* The end of the main method*/
  30.  
  31.  
  32.  
  33. 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*/
  34. {
  35. for(counter=0; counter<=strlen(string_of_characters); counter++)/* Loop to go through the array of characters (string) */
  36. {
  37. for(character_frequency_counter=0;character_frequency_counter<=25;character_frequency_counter++)/* Loop to go through the frequency array*/
  38. {
  39. if(frequency_array[character_frequency_counter][0]==string_of_characters[counter])/* Comparability of a character to characters in the string*/
  40. {
  41. frequency_array[character_frequency_counter][1]=(frequency_array[character_frequency_counter][1])+1;/* If character if found its count is incremented by one(1)*/
  42. }
  43. }
  44.  
  45. }
  46.  
  47. return frequency_array;/* The function returns an array to the main method (caller)*/
  48.  
  49. }
  50.  
  51.  
  52. int printout_of_character_frequencies(char frequency_array[25][2])/* Function to print out the results*/
  53. {
  54. printf("\n\n%-10s%-8s%15s\n","Letter","Frequency","Histogram");
  55. for(output_counter=0;output_counter<=25;output_counter++)/* Loop to print the elements of the frequency array*/
  56. {
  57. printf("%3c%12d ", frequency_array[output_counter][0], frequency_array[output_counter][1]);
  58.  
  59. 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*/
  60. {printf("%c",'*');}
  61.  
  62. printf("\n");/* Next line*/
  63. }
  64. return0;/* Ends after the function executes successfully*/
  65. }
  66.  
  67.  
  68.  
  69. <c>
↧

Viewing full articles (9326 articles)
Browse article index

Latest Images

Trending Articles



Latest Images