k&r2 exercise 1-13 review

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
 
A

Alan Curry

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 = 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 > 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; 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.
 
A

Anthony Irwin

Alan said:
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.
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 = 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top