generate histogram based on word length

R

randomtalk

hello, I'm doing an exercise in "The C Programming Language, Second
Edition". I just can't get one of the first chapter problem correct (I
do realize however that there are answer exist on the internet for the
book, but i want to know what's wrong with my program). So here is my
program:

#include <stdio.h>
#define LENGTH 255
int main()
{
int c, length[LENGTH], curLength, i, j;
while((c = getchar()) != EOF)
{
if((c == ' ') || (c == '\t') || (c== '\n'))
{
length[curLength]++;
curLength = 0;
}
else
curLength++;
}
for(i = 0; i < LENGTH; i++)
{
for(j = 0; j <= length; j++)
{
printf("=");
}
printf("\n");
}
}

What I don't get is that how come it would print innumerable =s (It
should print in rows and each row has =s symbolize how many words
contain that many letter, if no words has that many letter, it should
print an empty row). It seems to be to be stuck in a very long if not
infinite loop, but i can't find the culprit..

Can someone kindly point out my mistake? Thank you very much!
 
P

pete

hello, I'm doing an exercise in "The C Programming Language, Second
Edition". I just can't get one of the first chapter problem correct (I
do realize however that there are answer exist on the internet for the
book, but i want to know what's wrong with my program). So here is my
program:

#include <stdio.h>
#define LENGTH 255
int main()
{
int c, length[LENGTH], curLength, i, j;
while((c = getchar()) != EOF)
{
if((c == ' ') || (c == '\t') || (c== '\n'))
{
length[curLength]++;
curLength = 0;
}
else
curLength++;
}
for(i = 0; i < LENGTH; i++)
{
for(j = 0; j <= length; j++)
{
printf("=");
}
printf("\n");
}
}

What I don't get is that how come it would print innumerable =s (It
should print in rows and each row has =s symbolize how many words
contain that many letter, if no words has that many letter, it should
print an empty row). It seems to be to be stuck in a very long if not
infinite loop, but i can't find the culprit..

Can someone kindly point out my mistake? Thank you very much!


int c, i, j, curLength = 0, length[LENGTH] = {0};
 
C

Charles Mills

hello, I'm doing an exercise in "The C Programming Language, Second
Edition". I just can't get one of the first chapter problem correct (I
do realize however that there are answer exist on the internet for the
book, but i want to know what's wrong with my program). So here is my
program:

#include <stdio.h>
#define LENGTH 255
int main()
{
int c, length[LENGTH], curLength, i, j;
while((c = getchar()) != EOF)
{
if((c == ' ') || (c == '\t') || (c== '\n'))
{
length[curLength]++;
curLength = 0;
}
else
curLength++;
}
for(i = 0; i < LENGTH; i++)
{
for(j = 0; j <= length; j++)
{
printf("=");
}
printf("\n");
}
}

What I don't get is that how come it would print innumerable =s (It
should print in rows and each row has =s symbolize how many words
contain that many letter, if no words has that many letter, it should
print an empty row). It seems to be to be stuck in a very long if not
infinite loop, but i can't find the culprit..

Can someone kindly point out my mistake? Thank you very much!


curLength and length[LENGTH] are uninitialized. Try this at the start
of main:
int c, curLength = 0, i, j;
int length[LENGTH] = { 0 };

Also, check that curLength does not exceed LENGTH.

To be strictly conforming you should write 'int main(void)' or 'int
main(int argc, const char *argv[])' and return something from main --
probably zero. If your compiler supports warnings, turn them on
(typically the command line option -Wall will do that).

-Charlie
 
R

randomtalk

pete said:
hello, I'm doing an exercise in "The C Programming Language, Second
Edition". I just can't get one of the first chapter problem correct (I
do realize however that there are answer exist on the internet for the
book, but i want to know what's wrong with my program). So here is my
program:

#include <stdio.h>
#define LENGTH 255
int main()
{
int c, length[LENGTH], curLength, i, j;
while((c = getchar()) != EOF)
{
if((c == ' ') || (c == '\t') || (c== '\n'))
{
length[curLength]++;
curLength = 0;
}
else
curLength++;
}
for(i = 0; i < LENGTH; i++)
{
for(j = 0; j <= length; j++)
{
printf("=");
}
printf("\n");
}
}

What I don't get is that how come it would print innumerable =s (It
should print in rows and each row has =s symbolize how many words
contain that many letter, if no words has that many letter, it should
print an empty row). It seems to be to be stuck in a very long if not
infinite loop, but i can't find the culprit..

Can someone kindly point out my mistake? Thank you very much!


int c, i, j, curLength = 0, length[LENGTH] = {0};


wow, that worked. I have to remember to initialize my variables next
time, thank you very much!
 
C

CBFalconer

hello, I'm doing an exercise in "The C Programming Language, Second
Edition". I just can't get one of the first chapter problem correct
(I do realize however that there are answer exist on the internet
for the book, but i want to know what's wrong with my program). So
here is my program:

#include <stdio.h>
#define LENGTH 255
int main()
{
int c, length[LENGTH], curLength, i, j;
while((c = getchar()) != EOF)
{
if((c == ' ') || (c == '\t') || (c== '\n'))
{
length[curLength]++;
curLength = 0;
}
else
curLength++;
}
for(i = 0; i < LENGTH; i++)
{
for(j = 0; j <= length; j++)
{
printf("=");
}
printf("\n");
}
}

What I don't get is that how come it would print innumerable =s (It
should print in rows and each row has =s symbolize how many words
contain that many letter, if no words has that many letter, it should
print an empty row). It seems to be to be stuck in a very long if not
infinite loop, but i can't find the culprit..

Can someone kindly point out my mistake? Thank you very much!


Here it is with some corrections:

#include <stdio.h>
#define LENGTH 255

int main(void)
{
int c, length[LENGTH], curLength, i, j;

for (i = 0; i < LENGTH; i++) length = 0;

curLength = 0;
while ((c = getchar()) != EOF) {
if ((c == ' ') || (c == '\t') || (c== '\n')) {
length[curLength]++;
curLength = 0;
}
else curLength++;
}
for (i = 1; i < LENGTH; i++)
if (length) {
for (j = 0; j <= length; j++) printf("=");
printf(" %d\n", i);
}
return 0;
}

The corrections largely have to do with initialization and handling
of boundary cases (the zero length word).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top