473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

generate histogram based on word length

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[i]; 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!

Mar 25 '06 #1
4 4245
ra********@gmail.com wrote:

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[i]; 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};

--
pete
Mar 25 '06 #2
ra********@gmail.com wrote:
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[i]; 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

Mar 25 '06 #3

pete wrote:
ra********@gmail.com wrote:

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[i]; 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!

Mar 25 '06 #4
ra********@gmail.com wrote:

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[i]; 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[i] = 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[i]) {
for (j = 0; j <= length[i]; 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).

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Mar 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Oracle3001 | last post by:
Hi All, I am trying to use JAI to build a histogram of an image i have. I have posted the code below, and the error I get at runtime. I have taken the code from the offical java examples, so I am...
1
by: bleh | last post by:
....to include a removeData(datatoremove) function, to mirror the existing addData(datatoadd) function. If anybody knows of somewhere where this has been done already, if you could point me in...
7
by: WreckingCru | last post by:
I've been assigned to design a function that takes in a string and creates a word histogram of the letters. So - "hello hi" should give frequencies of h:2, l:2 etc.... I've also told to use maps...
27
by: ext_u | last post by:
Ok I thought I would try to take the program one thing at a time. (If you remember my last post I am trying to make a histogram with data on the size of each word) Anways first .. I obviously...
11
by: c19h28o2 | last post by:
Hi, Guy's I know there are several posts about this, however I do not want to read them as answers are undoubtedly posted! Here is my attempt but I'm slightly stuck. I'm not looking for the...
5
by: arnuld | last post by:
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...
12
by: arnuld | last post by:
i was able to create a solution for a Horizontal-Histogram. i was completely unable to understand this Vertical-Histogram phenomenon. even though i have looked at the solution at this page: ...
2
by: fatenn | last post by:
I need make from the Oracle Text index of the "word-frequency histogram", this is list of the tokens in this index, where each token contains the list of documents that contain that token and...
0
by: Fredrik Lundh | last post by:
aditya shukla wrote: Extracting is easy; you can do something like data = for line in open("test.txt"): line = line.split("|") data.append(float(line))
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.