I'm working through the K&R C programming book, and I just completed exercise 1-13 where you print a histogram of the lengths of words that are input. My horizontal histogram is fine, but my vertical histogram has a '%' sign appearing at the end of it, and I can't figure out why that keeps happening. Here is the loop that I think is causing the issue:
for (i = 0; i < array_length; i++) {
printf("%-4d", i + 1);
}
If the array_length is 7, I will have a '%' sign after the 7th position like this:
|
| |
| |
| |
| |
| |
| | | | |
| | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
1 2 3 4 5 6 7 %
If I remove this loop, the vertical bars print fine, I just don't get the numbering below each of them.
What is going on here? As far as I can guess, the printf is running one extra time with garbage data in 'i', but I can't figure out why or how this is happening.
I'm compiling with gcc on Ubuntu, if that makes any difference.
Here is the complete code for your reference:
#include <stdio.h>
#define IN 1
#define OUT 0
#define ARRAY_SIZE 1000
int main() {
int c, current_word_length, state, i, j, current_word, largest, array_length, top;
int word_lengths[ARRAY_SIZE];
current_word_length = current_word = largest = 0;
state = OUT;
for (i = 0; i < ARRAY_SIZE; i++) {
word_lengths[i] = 0;
}
printf("Enter up to 1000 words and this program will calculate their length.\n");
printf("Please enter a newline, tab, or space before the last word entered, then EOF to print summary.\n\n");
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\t' || c == '\n') {
if (state == IN) {
word_lengths[current_word++] = current_word_length;
if (current_word_length > largest) {
largest = current_word_length;
}
current_word_length = 0;
state = OUT;
}
} else {
state = IN;
current_word_length++;
}
}
array_length = current_word;
printf("\nWord Lengths Histogram Horizontal:\n\n");
for (i = 0; i < array_length; i++) {
printf("%-4d: ", i + 1);
for (j = 0; j < word_lengths[i]; j++) {
printf("=");
}
printf("\n");
}
printf("\nWord Lengths Histogram Vertical:\n\n");
top = largest;
while (top > 0) {
for (i = 0; i < array_length; i++) {
if (word_lengths[i] >= top) {
printf("%-4c", '|');
} else {
printf("%-4c", ' ');
}
}
printf("\n");
top--;
}
for (i = 0; i < array_length; i++) {
printf("%-4d", i + 1);
}
return 0;
}
And here is what I expect to occur using this input string "Bacon ipsum dolor amet frankfurter landjaeger ham":
Enter up to 1000 words and this program will calculate their length.
Please enter a newline, tab, or space before the last word entered, then EOF to print summary.
Bacon ipsum dolor amet frankfurter landjaeger ham
Word Lengths Histogram Horizontal:
1 : =====
2 : =====
3 : =====
4 : ====
5 : ===========
6 : ==========
7 : ===
Word Lengths Histogram Vertical:
|
| |
| |
| |
| |
| |
| | | | |
| | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
1 2 3 4 5 6 7
OK, as pointed out by nos and chqrlie in the comments, I need to add a newline at the end of my program so that the shell prompt will appear on a newline AFTER the program has finished running.
Adding the putchar() call at the end of main() fixed my issue:
for (i = 0; i < array_length; i++) {
printf("%-4d", i + 1);
}
putchar('\n');
return 0;
} // end main()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.