Re: modified "histogram" exercise from K&R2
- From: "arnuld" <geek.arnuld@xxxxxxxxx>
- Date: 22 Mar 2007 03:09:34 -0700
On Mar 22, 2:28 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
arnuld said:
this is a programme that counts the "lengths" of each word and then
prints that many of stars(*) on the output . it is a modified form of
K&R2 exercise 1-13. the programme runs without any compile-error BUT
it has a semantic BUG:
Here's your printing loop, with an extra printf added. It won't solve
your problem for you, but it will help you to understand what's going
on, and /that/ will help you to solve your problem:
for(i = 0; i < arr_index; ++i)
{
printf("i = %d, lwords[i] = %d\n", i, lwords[i]);
for(j = 0; j < lwords[i]; ++j)
putchar('*');
putchar('\n');
}
this is the output now:
[arch@voodo kr2]$ ./a.out
like and
---------- printing HISTOGRAM -----------
i = 0 lwords[i] = 4
****
i = 1 lwords[i] = 0
i = 2 lwords[i] = 3
***
[arch@voodo kr2]$
so i see, inside while this loop was not working properly:
if(state == OUT)
{
lwords[arr_index++] = nc;
++nw;
state = IN;
nc = 0;
}
only the 1st 2 lines in the loop caused the trouble, so i added a
"check" for spaces:
if(c != ' ' || c != '\t' || c != '\n')
{
lwords[arr_index++] = nc;
++nw;
}
state = IN;
nc = 0;
}
but this also did not work. even thought i have stopped "tabs,
newlines and extra spaces but still i can see them as newlines in
output.
so i thought '\t' || 'n' || ' ' will always get nc == 0, hence tried
this approach:
if(state == OUT)
{
if(nc != 0)
{
lwords[arr_index++] = nc;
++nw;
}
state = IN;
nc = 0;
}
}
and it worked correctly:
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-13.c
[arch@voodo kr2]$ ./a.out
like and
---------- printing HISTOGRAM -----------
i = 0 lwords[i] = 4
****
i = 1 lwords[i] = 3
***
[arch@voodo kr2]$ ./a.out
like
ab ab
a
---------- printing HISTOGRAM -----------
i = 0 lwords[i] = 4
****
i = 1 lwords[i] = 2
**
i = 2 lwords[i] = 2
**
i = 3 lwords[i] = 1
*
[arch@voodo kr2]$
(Incidentally, your program claims to generate a histogram. It doesn't.
That isn't what a histogram *is*.)
OK, a histogram is like a graph with x and y axis, like your solution
at CLC-Wiki for exercise 1-13 but at the present, that is beyond my
level of understanding. i can read it but can not write it, no matter
how may times i read this.
perhaps, as i said in other thread, your solution for that exercise
needs a section like "documentation for newbies". i understood some
part of it after you gave me LONG and CLEAR explanation but i can't
ask every time to write such explanations for me and suck all of your
precious time. it is only and only me who did not understand that
solution. i checked all the archives of comp.lang.c. nobody asked this
solution before. i am the only one and i really *hate* it eating the
family/personal time of people like you.
you solved the trouble. thanks for the "printf" statement. i also
thought of that but i was always putting it inside the "while" loop
and that generated error every time i tried. (i used to use such
"printing to screen" statements in debugging trivial Lisp code i used
to write)
thank you.
.
- References:
- modified "histogram" exercise from K&R2
- From: arnuld
- Re: modified "histogram" exercise from K&R2
- From: Richard Heathfield
- modified "histogram" exercise from K&R2
- Prev by Date: Re: What is "typedef struct" ?
- Next by Date: Re: What is "typedef struct" ?
- Previous by thread: Re: modified "histogram" exercise from K&R2
- Next by thread: horizontal histogram (was: modified "histogram" exercise from K&R2)
- Index(es):
Relevant Pages
|