468,783 Members | 1,572 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,783 developers. It's quick & easy.

k&r2 exercise 1-13 review

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[i] = 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[i] 0) {
printf("\n%d", i + 1);
for (ii = 0; ii < nbr_array[i]; ii++) {
printf(" *");
}
}
}

printf("\n");
}
-------------------------------

Thanks for any input you may have.

Kind Regards,
Anthony Irwin
Mar 30 '07 #1
3 1549
In article <13*************@corp.supernews.com>,
Anthony Irwin <no****@nowhere.nomailwrote:
>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.
Overall it's pretty good, especially if you've only read as far as chapter 1.
The results aren't quite right though.
>
#include <stdio.h>
#define MAX 22

main() {
Most of us prefer the more verbose "int main(void)" these days.
int i, ii, number, space, c, nbr_array[MAX];
number = space = c = i = ii = 0;
"number" and "space" could have been more descriptively named, or commented.
Eventually I figured out that they represent "current_word_length" and
"currently_in_word".
for (i = 0; i < MAX; i++)
nbr_array[i] = 0;

while((c = getchar()) != EOF) {
if (space 0) {
That test (space 0) is kind of strange. "space" is never set to any value
other than 0 and 1. It's a natural boolean, so you could simply use
"if (space) {" instead.
if (c != ' ' && c != '\t') {
Here's the bug I referred to. Newline should also be considered a word
separator. If you run your program with an input that contains 2 lines with
just a few letters on each line, you'll see that it incorrectly counts the 2
lines as a single word.
space = 0;
number = 1;
}
}
else {
if (c != ' ' && c != '\t' && c != '\n')
Here you treated space, tab, and newline as equivalent...
++number;
if (c == ' ' || c == '\t')
Here you've omitted the newline again...
space = 1;
if (space 0 || c == '\n')
And then stuck the newline test as an afterthought? Be consistent: put all
three of the tests for ' ' and '\t' and '\n' together. There's no reason to
treat newline differently from the other two.
++nbr_array[number - 1];
What happens here if the input contains a word longer than MAX? The array
index will be too big and the program will no longer behave predictably. It
might crash or just print out bogus results. Before you get too far in your
studies, you should develop a habit of checking for invalid input. Make the
program print an error message when number MAX instead of doing the
increment, or make it increment a separate counter of "huge words" and print
that counter out as the last line of the histogram.
}
}

for (i = 0; i < MAX; i++) {
if (nbr_array[i] 0) {
printf("\n%d", i + 1);
Printing the newline at the beginning instead of the end is a weird choice,
but it still separates lines properly so it's not too bad. You just get an
extra blank line at the top.
for (ii = 0; ii < nbr_array[i]; ii++) {
printf(" *");
}
Right here, after the inner for loop, would be the most logical place to
print a newline after the row of stars.
}
}

printf("\n");
And if you did the \n after the inner loop, you wouldn't need this one at the
end.
>}
--
Alan Curry
pa****@world.std.com
Mar 30 '07 #2
Alan Curry wrote:
Overall it's pretty good, especially if you've only read as far as chapter 1.
The results aren't quite right though.
I am only in chapter 1 of k&r but I have looked at some other c books
before k&r.
>#include <stdio.h>
#define MAX 22

main() {

Most of us prefer the more verbose "int main(void)" these days.
Yes that is true but I am trying to stick with what k&r have in their
book for their exercises. they haven't even done return 0; yet in the
main function but I believe they cover that in the section on functions
a couple more pages in.
> int i, ii, number, space, c, nbr_array[MAX];
number = space = c = i = ii = 0;

"number" and "space" could have been more descriptively named, or commented.
Eventually I figured out that they represent "current_word_length" and
"currently_in_word".
Good point I guess it could be hard for other readers. I sort of had
space for if it is in a space and number for the number of characters in
the current word but it's not obvious to those that did not write the
code or to me later down the track.
> for (i = 0; i < MAX; i++)
nbr_array[i] = 0;

while((c = getchar()) != EOF) {
if (space 0) {

That test (space 0) is kind of strange. "space" is never set to any value
other than 0 and 1. It's a natural boolean, so you could simply use
"if (space) {" instead.
In C is any number 1 true or is only 1 true?

> ++nbr_array[number - 1];

What happens here if the input contains a word longer than MAX? The array
index will be too big and the program will no longer behave predictably. It
might crash or just print out bogus results. Before you get too far in your
studies, you should develop a habit of checking for invalid input. Make the
program print an error message when number MAX instead of doing the
increment, or make it increment a separate counter of "huge words" and print
that counter out as the last line of the histogram.
Yeah I totally didn't think about exceeding the array size. I now check
that number < MAX before adding the number to the array.
Thanks for the suggestions and spending the time to look at the code.

Kind Regards,
Anthony Irwin
Mar 31 '07 #3
On Mar 30, 6:04 pm, Anthony Irwin <nos...@nowhere.nomailwrote:
Alan Curry wrote:
[snip]
In C is any number 1 true or is only 1 true?
Any non-zero value is true and zero is false.
Mar 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Chris Readle | last post: by
16 posts views Thread by Josh Zenker | last post: by
2 posts views Thread by arnuld | last post: by
4 posts views Thread by arnuld | last post: by
16 posts views Thread by arnuld | last post: by
2 posts views Thread by arnuld | last post: by
4 posts views Thread by arnuld | last post: by
16 posts views Thread by arnuld | last post: by
88 posts views Thread by santosh | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.