Re: modified "histogram" exercise from K&R2



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.

.



Relevant Pages

  • Re: While Loops (I think)...
    ... > across an exercise that I can't get to work. ... > Sub Main ... The While loop will execute until a given condition becomes true. ... 'Deal with the valid input ...
    (microsoft.public.dotnet.languages.vb)
  • Re: C program question
    ... >successfully the first part of the exercise, ... This does not sum the integers from n to 2*n. ... code prints 12. ... Once you have the loop for positive values, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: While Loops (I think)...
    ... The exercise asks that you create a game that makes the user guess a number from 1-100, and you tell the user "lower" or "higher" as they input their guesses, until they guess the correct number, at which point you then tell the user "Correct". ... I tried using the While Loop, and it "sort of" worked, but after a while of inputting numbers, it ... to enable other people to see where the problem lies. ...
    (microsoft.public.dotnet.languages.vb)
  • Beginners question.
    ... In one of the tutorials an exercise is the following... ... write a loop that traverses the previous list and prints ... to issue the variable, myitem, with a value that was the length of item. ...
    (comp.lang.python)
  • Re: TI-Burr-Brown parts shartage?
    ... You can exercise and train your temperament just as you can ... plausible explanation at the time. ... Stephen Goldstein's explanation did involve incompetent management - ...
    (sci.electronics.design)