Exercise 1-13
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
Horizontal version:
#include <stdio.h>
#define min(a, b) ((a)<(b)?(a):(b))
#define N 11
int isspace(int ch) {
if (ch == ' ' || ch == '/t' || ch == '/n') {
return 1;
}
else
return 0;
}
int main(void)
{
int i, j, c;
int length = 0;
int inwordflag = 0;
int counter[N] = {0};
while((c = getchar()) != EOF) {
if (!isspace(c)) {
inwordflag = 1;
length++;
}
if (isspace(c) && inwordflag) {
inwordflag = 0;
length = min(length, N);
counter[length - 1]++;
length = 0;
}
}
/* if there is no space after the end of the last word */
if (inwordflag && c == EOF) {
length = min(length, N);
counter[length - 1]++;
}
/* print the result */
for (i = 1; i <= N; i++) {
printf("%s%-2d ", (i<=10?" ":">="), i);
j = counter[i-1];
while (j-- > 0) {
printf("*");
}
printf("%d", counter[i-1]);
printf("/n");
}
return 0;
}
Vertical version:
#include <stdio.h>
#define min(a, b) ((a)<(b)?(a):(b))
#define N 11
int isspace(int ch) {
if (ch == ' ' || ch == '/t' || ch == '/n') {
return 1;
}
else
return 0;
}
/* find the max value from a int array */
int findmax(int data[], int len)
{
int i, maxvalue = data[0];
for (i = 1; i < len; i++) {
if (data[i] > maxvalue) {
maxvalue = data[i];
}
}
return maxvalue;
}
int main(void)
{
int i, j, c;
int length = 0;
int inwordflag = 0;
int counter[N] = {0};
int maxvalue = 0;
while((c = getchar()) != EOF) {
if (!isspace(c)) {
inwordflag = 1;
length++;
}
if (isspace(c) && inwordflag) {
inwordflag = 0;
length = min(length, N);
counter[length - 1]++;
length = 0;
}
}
/* if there is no space after the end of the last word */
if (inwordflag && c == EOF) {
length = min(length, N);
counter[length - 1]++;
}
/* print the result */
maxvalue = findmax(counter, N) + 1;
printf("%5s^/n", " ");
for (i = maxvalue; i >= 1; i--) {
printf("%4d | ", i);
for (j = 0; j < N; j++) {
if (counter[j] >= maxvalue) {
printf("%-3s", "*");
}
else
printf("%-3s", " ");
}
printf("/n");
maxvalue--;
}
printf("%5s%s"," ","+ ");
for (i = 1; i <= N; i++) {
printf("%s", "---");
}
printf(">/n%7s"," ");
for (i = 1; i < N; i++) {
printf("%-3d", i);
}
printf(">%-d", i - 1);
printf("/n");
return 0;
}
Exercise 1-14
Write a program to print a histogram of the frequencies of different characters in its input.
/* print a histogram of the frequencies of different characters
* in the input */
#include <stdio.h>
#define min(a, b) ((a)<(b)?(a):(b))
#define N 27
int isletter(int ch) {
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
return 1;
}
else
return 0;
}
/* change a upper case char to lower case */
int tolower(int ch) {
if (ch >= 'A' && ch <= 'Z') {
return ch + 32;
}
else return ch;
}
/* find the max value from a int array */
int findmax(int data[], int len)
{
int i, maxvalue = data[0];
for (i = 1; i < len; i++) {
if (data[i] > maxvalue) {
maxvalue = data[i];
}
}
return maxvalue;
}
int main(void)
{
int i, j, c;
int counter[N] = {0};
int maxvalue = 0;
while((c = getchar()) != EOF) {
if (isletter(c)) {
counter[tolower(c) - 'a']++;
} else
counter[N - 1]++;
}
/* print the result */
maxvalue = findmax(counter, N) + 1;
printf("%5s^/n", " ");
for (i = maxvalue; i >= 1; i--) {
printf("%4d | ", i);
for (j = 0; j < N; j++) {
if (counter[j] >= maxvalue) {
printf("%-3s", "*");
}
else
printf("%-3s", " ");
}
printf("/n");
maxvalue--;
}
printf("%5s%s"," ","+ ");
for (i = 1; i <= N; i++) {
printf("%s", "---");
}
printf(">/n%7s"," ");
for (i = 0; i < N - 1; i++) {
printf("%-3c", i + 'a');
}
printf("other");
printf("/n");
return 0;
}