Ceriousmall
2/13/2011 6:36:00 AM
On Feb 11, 4:51 pm, "io_x" <a...@b.c.invalid> wrote:
> "Ceriousmall" <> ha scritto nel messaggionews:948c9c67-82b2-471c-b52d-515c26b61d32@e9g2000vbi.googlegroups.com...
> your code i did not understand much but
> your program has a bug for me
> if the input is somethig like
> aaaa\n
> hh^Z
> where <ctrl-z>==^Z is the end of the input
> it not find the 2 letter word hh
>
> 1| *
> +------------------------------------------------------------
> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20++
>
> instead this is my prog output with the same input:
> +-------------------- -------------------
> 1:
> 2:-
> 3:
> 4:-
> 5:
> etc
> there are 2 words (for the thefinition of word in text file)
> one of 2 char and one of 4 char
.
.
.
Ok this is my revisited code, I believe this should solve the bug as
stated above............
/* Program prints a histogram of the length of words in its input */
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define LIMIT 20 /* horizontal scale & word size limits */
#define VERLIMIT 1000 /* verticle scale limit */
int main(void)
{
int c, i, x;
int nchar, mark_scale, terminate;
int mark[LIMIT+1];
long wordlength[LIMIT+1];
nchar = 0;
mark_scale = FALSE;
terminate = FALSE;
for (i = 0; i <= LIMIT; ++i) {
wordlength[i] = 0;
mark[i] = FALSE;
}
while ((c = getchar()) && terminate == FALSE) {
if (c >= '0' && c <= '9')
printf("digit %d not valid for a word. . .\n", c-'0');
else if (c == ' ' || c == '\n' || c == '\t') {
if (nchar > 0 && nchar < LIMIT)
++wordlength[nchar];
else if (nchar >= LIMIT)
++wordlength[LIMIT];
nchar = 0;
}
else
++nchar;
if (c == EOF)
terminate = TRUE;
}
for (i = VERLIMIT; i >= 1; --i) {
for (x = 1; x <= LIMIT; ++x)
if (wordlength[x] == i)
mark_scale = TRUE;
if (mark_scale != FALSE)
printf("%4d|", i);
for (x = 1; x <= LIMIT; ++x)
if (wordlength[x] == i) {
printf("%3c", 'Ü');
mark[x] = TRUE;
}
else if (mark[x] == FALSE)
printf("%3c", ' ');
else
printf("%3c", 'Ü');
printf("\n");
}
printf(" +");
for (i = 1; i <= LIMIT*3; ++i)
printf("-");
printf("\n ");
for (i = 1; i <= LIMIT; ++i)
printf("%3d", i);
printf("++\n");
return 0;
}
_______________________________________________
Ceriously
I'm really beginning to C the code now. . . . .