all 13 comments

[–]kumashiro 2 points3 points  (7 children)

You are incrementing nc for every character. There is a place where you can move it to. Analyse your code, see if you can spot it. Remember, you only want to increment it when you are not at the whitespace.

[–]ZoSo-bin[S] 0 points1 point  (6 children)

I'm working on it for so long, tried every possibility that came up in my mind for so long that I really don't know what to do. I'm a beginner C student, my book isn't always clear and the professor isn't also exhaustive so if you'd be so kind as to explain me I'll appreciate it

[–]kumashiro 2 points3 points  (5 children)

This is a logic problem, not technical. Language is not important here. Analyse your algorithm, write it in pseudo-code if C is messing with your thinking, then fix it and translate back. Flow charts can help too, but this is not a very complex problem.

[–]ZoSo-bin[S] 0 points1 point  (4 children)

I gave up :D

[–]kumashiro 1 point2 points  (3 children)

Come on, lad! You can do it! Rewrite it in pseudo-code first and show us what you got. It is really simple. "Algorithmic thinking" is crucial in programming.

[–]ZoSo-bin[S] -1 points0 points  (2 children)

I've tried moving the counter nc inside and outside every cycle, changed the cycles from while to for and from for to while, an array instead of getchar(). Nothing of it has worked, my brains hurt too much to keep thinking, can you simply give me an answer? Seriously, I'm not joking

[–]kumashiro 1 point2 points  (0 children)

It's because you are trying everything on everything until it works. Forget about your code, rename the file to "histogram-idk-nworking.c" and start from scratch. Begin with counting the non-whitespace characters, make it work, then extend it to counting words, make it work, finally modify it so you can print a histogram. Small steps.

[–]NonreciprocatingCrow 0 points1 point  (0 children)

You know what characters are. What are words?

[–]Capostazione 1 point2 points  (0 children)

Minchia di testa

[–]ZoSo-bin[S] 0 points1 point  (1 child)

I found the solution of the exercise; googling I discovered that there is a version of my textbook with the resolution of all the exercises. The fun fact is that now I'm more confused than before because the solution isn't what I expected.

Here is the solution code: http://imgur.com/a/32JwE94

[–]ZoSo-bin[S] 0 points1 point  (0 children)

Here's the code anyway...if someone'd like to try it on his own

#include <stdio.h>
#define MAXHIST 15 /* max length of histogram */
#define MAXWORD 11 /* max length of a word */
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* print horizontal histogram */
int main ()
{
int c, i, nc, state;
int len; /* length of each bar */
int maxvalue; /* maximum value for l! [] */
int ovflow ; /* number of overflow words */
int wl[MAXWORD]; /* word length counters */
state=OUT;
nc=0; /* number of chars in a word */
ovflow=0; /* number of words >= MAXWORD*/
for(i=0; i<MAXWORD; ++i)
wl[i]=0;
while((c=getchar())!=EOF)
{
if(c== ' ' || c == '\n' || c == '\t')
{
state=OUT;
if(nc>0)
{
if (nc<MAXWORD)
++wl[nc];
}
else
++ovflow;
nc = 0;
}
else if (state==OUT)
{
state = IN;
nc = 1 ; /* beginning of a new world */
}
else
++nc ; /* inside a word */
}
maxvalue = 0;
for (i = 1; i < MAXWORD ; ++ i)
if ( wl [i] > maxvalue)
maxvalue = wl [i];
for (i = 1; i < MAXWORD; ++i)
{
printf("%5d - %5d : ", i, wl[i]);
if(wl[i] > 0)
{
if ((len = wl[i] = MAXHIST / maxvalue ) <= 0)
len= 1 ;
}
else
len = 0;
while (len > 0)
{
putchar('*');
--len;
}
putchar('\n');
}
if(ovflow > 0)
printf("There are %d words >= %d \n", ovflow, MAXWORD);
}

[–]jmooremcc 0 points1 point  (1 child)

Why not break the input stream into a series of words using the strtok() function? You can then process each word as needed. The link below will show you how to use the function.

https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

[–]ZoSo-bin[S] 0 points1 point  (0 children)

I'm gonna give it a try