A
Anthony Irwin
Hi All,
I have done the horizontal version of exercise 1-13 and just wanted to
make sure that I had done it right as I was not fully sure what a
histogram was.
I also wanted to check to see if anyone had any suggestions on how to
make it better and if I had any bugs I was not aware of.
The output of the program is below. some of the * will probably wrap:
$ cat exercise_1-13.c | ./exercise_1-13
1 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * *
2 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * *
3 * * * * * * * * * * * * * * *
4 * * * * * * * * * * * * * *
5 * * * * * * * * * * *
6 * * * * * *
7 * * *
8 * * * *
9 * * * * *
10 * *
11 * *
12 * *
13 * * *
14 * *
15 * *
16 *
-------------------------
exercise_1-13.c
-------------------------
/* 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. */
#include <stdio.h>
#define MAX 22
main() {
int i, ii, number, space, c, nbr_array[MAX];
number = space = c = i = ii = 0;
for (i = 0; i < MAX; i++)
nbr_array = 0;
while((c = getchar()) != EOF) {
if (space > 0) {
if (c != ' ' && c != '\t') {
space = 0;
number = 1;
}
}
else {
if (c != ' ' && c != '\t' && c != '\n')
++number;
if (c == ' ' || c == '\t')
space = 1;
if (space > 0 || c == '\n')
++nbr_array[number - 1];
}
}
for (i = 0; i < MAX; i++) {
if (nbr_array > 0) {
printf("\n%d", i + 1);
for (ii = 0; ii < nbr_array; ii++) {
printf(" *");
}
}
}
printf("\n");
}
-------------------------------
Thanks for any input you may have.
Kind Regards,
Anthony Irwin
I have done the horizontal version of exercise 1-13 and just wanted to
make sure that I had done it right as I was not fully sure what a
histogram was.
I also wanted to check to see if anyone had any suggestions on how to
make it better and if I had any bugs I was not aware of.
The output of the program is below. some of the * will probably wrap:
$ cat exercise_1-13.c | ./exercise_1-13
1 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * *
2 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * *
3 * * * * * * * * * * * * * * *
4 * * * * * * * * * * * * * *
5 * * * * * * * * * * *
6 * * * * * *
7 * * *
8 * * * *
9 * * * * *
10 * *
11 * *
12 * *
13 * * *
14 * *
15 * *
16 *
-------------------------
exercise_1-13.c
-------------------------
/* 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. */
#include <stdio.h>
#define MAX 22
main() {
int i, ii, number, space, c, nbr_array[MAX];
number = space = c = i = ii = 0;
for (i = 0; i < MAX; i++)
nbr_array = 0;
while((c = getchar()) != EOF) {
if (space > 0) {
if (c != ' ' && c != '\t') {
space = 0;
number = 1;
}
}
else {
if (c != ' ' && c != '\t' && c != '\n')
++number;
if (c == ' ' || c == '\t')
space = 1;
if (space > 0 || c == '\n')
++nbr_array[number - 1];
}
}
for (i = 0; i < MAX; i++) {
if (nbr_array > 0) {
printf("\n%d", i + 1);
for (ii = 0; ii < nbr_array; ii++) {
printf(" *");
}
}
}
printf("\n");
}
-------------------------------
Thanks for any input you may have.
Kind Regards,
Anthony Irwin