Exercise 1-14-2 (vertical - Histogram of frequencies)
Chapter_1 Exercise_1-14-1 | power Exercise_1-15 |
Exercise 1-14 K&R, p. 24
Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input.
histcv.c download
#include <stdio.h> // for getchar(), putchar(), printf(), EOF
// histogram of frequencies of different characters, vertically
int main()
{
int c, i, j;
int max; // max no of occs of diff char types
int digits[10]; // 0..9
int lower[26]; // a..z
int upper[26]; // A..Z
int white[6]; // whitespace: ' ', '\t', '\n', '\f', '\r', '\v'
for (i = 0; i < 10; i++) // initialize
{digits[i] = 0;} // initially, 0 occurrences
for (i = 0; i < 26; i++) // initialize
{lower[i] = upper[i] = 0;}
for (i = 0; i < 6; i++) // initialize
{white[i] = 0;}
while ((c = getchar()) != EOF)
{
if (c == ' ') {white[0]++;}
else if (c == '\t') {white[1]++;}
else if (c == '\n') {white[2]++;}
else if (c == '\f') {white[3]++;}
else if (c == '\r') {white[4]++;}
else if (c == '\v') {white[5]++;}
else if (c >= '0' && c <= '9')
{digits[c-'0']++;}
else if (c >= 'A' && c <= 'Z')
{upper[c-'A']++;}
else if (c >= 'a' && c <= 'z')
{lower[c-'a']++;}
}
printf("\t\tLEGEND:\n");
printf("Spaces: %d ", white[0]);
printf("Tabs: %d ", white[1]);
printf("Newlines: %d ", white[2]);
printf("Form feeds: %d ", white[3]);
printf("Returns: %d ", white[4]);
printf("Vertical tabs: %d ", white[5]);
putchar('\n');
printf("Digits:\t\t");
for (i = 0; i <= 9; i++)
{printf("%d ",i);}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i <= 9; i++)
{printf("%d ",digits[i]);}
putchar('\n');
printf("Lowercase:\t");
for (i = 'a'; i <= 'z'; i++)
{putchar(i);putchar(' ');}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i < 26; i++)
{printf("%d ",lower[i]);}
putchar('\n');
printf("Uppercase:\t");
for (i = 'A'; i <= 'Z'; i++)
{putchar(i);putchar(' ');}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i < 26; i++)
{printf("%d ",upper[i]);}
putchar('\n');
putchar('\n');
printf("\t\tHISTOGRAM:\n");
/*
printf("Spaces:\tTabs:\tNewlines:\tForm feeds:\tReturns:\tVertical tabs:\n");
max = 0;
for (i = 0; i < 6; i++)
{
if(max < white[i]) {max = white[i];}
}
for (i = 0; i < max; i++)
{
for (j = 0; j < 6; j++)
{
if (white[j] > i) {putchar('|');putchar('\t');}
else {putchar('\t');} // indent
}
putchar('\n');
}
*/
printf("Digits:\n");
for (i = 0; i <= 9; i++) {printf("%d ", i);}
putchar('\n');
max = 0; // reset max
for (i = 0; i <= 9; i++)
{
if(max < digits[i]) {max = digits[i];}
}
for (i = 0; i < max; i++)
{
for (j = 0; j <= 9; j++)
{
if (digits[j] > i) {putchar('|');putchar(' ');}
else {putchar(' ');putchar(' ');} // indent
}
putchar('\n');
}
printf("Lowercase letters:\n");
for (i = 0; i < 26; i++)
{putchar('a'+i);putchar(' ');}
putchar('\n');
max = 0; // reset max
for (i = 0; i < 26; i++)
{
if(max < lower[i]) {max = lower[i];}
}
for (i = 0; i < max; i++)
{
for (j = 0; j < 26; j++)
{
if (lower[j] > i) {putchar('|');putchar(' ');}
else {putchar(' ');putchar(' ');} // indent
}
putchar('\n');
}
printf("Uppercase letters:\n");
for (i = 0; i < 26; i++)
{putchar('A'+i);putchar(' ');}
putchar('\n');
max = 0; // reset max
for (i = 0; i < 26; i++)
{
if(max < upper[i]) {max = upper[i];}
}
for (i = 0; i < max; i++)
{
for (j = 0; j < 26; j++)
{
if (upper[j] > i) {putchar('|');putchar(' ');}
else {putchar(' ');putchar(' ');} // indent
}
putchar('\n');
}
}
/*
gcc histcv.c -o histcv
./histcv < histcv.c // input source file
LEGEND:
Spaces: 784 Tabs: 0 Newlines: 155 Form feeds: 0 Returns: 0 Vertical tabs: 0
Digits: 0 1 2 3 4 5 6 7 8 9
Occurrences: 37 4 13 2 2 2 15 0 0 7
Lowercase: 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
Occurrences: 89 5 96 28 118 79 12 68 212 17 0 36 25 81 46 85 1 134 60 151 56 9 30 21 4 6
Uppercase: 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
Occurrences: 6 0 0 3 3 3 2 1 1 0 0 3 1 3 5 0 0 3 3 3 2 2 0 0 0 3
HISTOGRAM:
Digits:
0 1 2 3 4 5 6 7 8 9
| | | | | | | |
| | | | | | | |
| | | | |
...................
Lowercase letters:
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
| | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | |
...................................
Uppercase letters:
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
| | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | |
| |
| |
|
./histcv < histcv // input binary file
LEGEND:
Spaces: 77 Tabs: 28 Newlines: 27 Form feeds: 13 Returns: 9 Vertical tabs: 18
Digits: 0 1 2 3 4 5 6 7 8 9
Occurrences: 18 8 19 4 9 9 8 2 16 12
Lowercase: 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
Occurrences: 75 23 39 42 77 22 20 25 61 0 6 37 24 54 39 19 2 69 57 93 36 3 5 13 18 6
Uppercase: 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
Occurrences: 22 18 18 29 15 10 19 91 22 2 0 23 14 9 7 5 1 8 7 22 11 2 2 9 2 2
HISTOGRAM:
Digits:
0 1 2 3 4 5 6 7 8 9
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | |
| | | |
....................
Lowercase letters:
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
| | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
..........................
Uppercase letters:
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
| | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | |
................................
*/
Note:
The histogram for whitespaces is commented out, as spaces displayed vertically fill
the terminal window. If you let it in, you can run the program with:
./histcv < histcv.c | less
then hit Enter or the space bar, or save the result to a file:
./histcv < histcv.c > output.txt
Chapter_1 Exercise_1-14-1 | BACK_TO_TOP | power Exercise_1-15 |
Comments
Post a Comment