I
ilan pillemer
Hi,
I have made an attempt at exercise 1.14 in K&R2: namely;
"Write a program to print a histogram of the frequencies of different
characters in its input."
Here is my program and the output.
#include <stdio.h>
#define HISTHEIGHT 400
int main()
{
int c, i, j, tot;
int nchars[26];
for (i = 0; i < 26; i++)
nchars = 0;
tot = 0;
while ((c = getchar()) != EOF) {
if ((c - 'A') >= 0 && (c-'A') <= 26) {
nchars[c - 'A']++;
tot++;
}
else if ((c - 'a') >= 0 && (c - 'a' <=26)) {
nchars[c-'a']++;
tot++;
}
}
/* Turn into a value out of HISTHEIGHT so it will fit screen */
/* when printing character frequencies as a histogram */
for (i = 0; i < 26; i++)
nchars = ( nchars * HISTHEIGHT ) / tot;
for (i = HISTHEIGHT; i > 0; i--) {
for (j = 0; j < 26; j++)
if (nchars[j] >= i)
printf("***");
else
printf(" ");
printf("\n");
}
printf(" A B C D E F G H I J K L M N O P Q R S T U V W X Y Z\n");
return 0;
}
***************************OUTPUT*********************
[ilan@pillemer Ch1]$ ./a.out < /usr/share/dict/words
***
***
***
***
***
***
***
***
***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** *** ****** ***
*** *** *** ****** ******
*** *** *** ****** ******
*** *** *** ****** *********
*** *** *** ****** *********
*** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** *** ****** *********
*** *** *** *** *** ****** *********
*** ********* *** *** ****** ************
*** ********* *** *** ****** ************
*** ********* *** *** ********* ************
*** ********* ****** *************** ************
*** ********* ****** *************** ************
*** ********* ********* *************** ************
*** ********* ********* *************** ************
*************** ********* *************** ************ ***
*************** ********* *************** ************ ***
*************** ********* *************** ************ ***
*************************** *************** ************ ***
*************************** ****************** ****************** ***
*************************** ****************** ****************** ***
*************************** ****************** ***************************
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
[ilan@pillemer Ch1]$
I have made an attempt at exercise 1.14 in K&R2: namely;
"Write a program to print a histogram of the frequencies of different
characters in its input."
Here is my program and the output.
#include <stdio.h>
#define HISTHEIGHT 400
int main()
{
int c, i, j, tot;
int nchars[26];
for (i = 0; i < 26; i++)
nchars = 0;
tot = 0;
while ((c = getchar()) != EOF) {
if ((c - 'A') >= 0 && (c-'A') <= 26) {
nchars[c - 'A']++;
tot++;
}
else if ((c - 'a') >= 0 && (c - 'a' <=26)) {
nchars[c-'a']++;
tot++;
}
}
/* Turn into a value out of HISTHEIGHT so it will fit screen */
/* when printing character frequencies as a histogram */
for (i = 0; i < 26; i++)
nchars = ( nchars * HISTHEIGHT ) / tot;
for (i = HISTHEIGHT; i > 0; i--) {
for (j = 0; j < 26; j++)
if (nchars[j] >= i)
printf("***");
else
printf(" ");
printf("\n");
}
printf(" A B C D E F G H I J K L M N O P Q R S T U V W X Y Z\n");
return 0;
}
***************************OUTPUT*********************
[ilan@pillemer Ch1]$ ./a.out < /usr/share/dict/words
***
***
***
***
***
***
***
***
***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** *** ****** ***
*** *** *** ****** ******
*** *** *** ****** ******
*** *** *** ****** *********
*** *** *** ****** *********
*** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** *** ****** *********
*** *** *** *** *** ****** *********
*** ********* *** *** ****** ************
*** ********* *** *** ****** ************
*** ********* *** *** ********* ************
*** ********* ****** *************** ************
*** ********* ****** *************** ************
*** ********* ********* *************** ************
*** ********* ********* *************** ************
*************** ********* *************** ************ ***
*************** ********* *************** ************ ***
*************** ********* *************** ************ ***
*************************** *************** ************ ***
*************************** ****************** ****************** ***
*************************** ****************** ****************** ***
*************************** ****************** ***************************
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
[ilan@pillemer Ch1]$