473,583 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

K&R2, section1.6, exercise 1-13

this programme runs without any error but it does not do what i want
it to do:

------------- PROGRAMME --------------
/* K&R2, section 1.6 Arrays; Exercise 1-13.

STATEMENT:
Write a program to print a histogram of the lengths of words in its
input.
It is easy to draw the histogram with the bars horizontal; a vertical
orientation is more challenging.
Method:

1.) we will store lenth of each wordn an array.
2.) for keeping simplicity, array size is 1000, i.e.
it can hold only 1000 words.
3.) after EOF is encountered then the "a Hostogram of starts ( * )
"will be
printed on the screen.

4.) each line of histogram will contain number of "stars" == length
of that word.

*/

#include <stdio.h>

#define IN 1
#define OUT 0
#define MAXWORDS 1000

int main()
{
int c;
int i = 0;
int aindex = 0;
int j = 0; /* "i,j,count" are general index counters */

int nc = 0; /* length of word or number of characters in a word */

int lwords[MAXWORDS + 1];

int nw = 0; /* number of words */

/* length of each word is stored in this array
and will be printed in the end */

int state = IN;

while( ((c = getchar()) != EOF) && (nw <= MAXWORDS) )
{
++nc;

if(c == ' ' || c == '\t' || c == '\n')
{
state = OUT;
--nc;
}

else if(state == OUT)
{
lwords[aindex++] = nc;
++nw;
state = IN;
}

}

printf("---------- printing HISTOGRAM -----------\n");

for(i = 0; i < aindex; ++i)
{
for(j = 0; j < lwords[i]; ++j)
putchar('*');

putchar('\n');
}

return 0;
}
------------- OUTPUT ---------------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-13.c
[arch@voodo kr2]$ ./a.out
like
---------- printing HISTOGRAM -----------
[arch@voodo kr2]$

Mar 21 '07 #1
19 2387
I don't recall if you are supposed to have access to ispunct() and
isspace() by that point in the book (K&R2 1.6) or not. Functions
isn't until 1.7 but we have been using some of the library functions
right from the get-go. But for a word counter, they (the is*()
functions and strtok()) seem like natural tools. Also, the evil and
cantankerous strtok() can be used.

A simple way to start might be to use the previous incarnation of the
word-count program on page 20.

Mar 21 '07 #2
On Mar 21, 11:42 am, "user923005 " <dcor...@connx. comwrote:
I don't recall if you are supposed to have access to ispunct() and
isspace() by that point in the book (K&R2 1.6) or not. Functions
isn't until 1.7 but we have been using some of the library functions
right from the get-go. But for a word counter, they (the is*()
functions and strtok()) seem like natural tools. Also, the evil and
cantankerous strtok() can be used.
you are using some really STRANGE words like "strtok".

A simple way to start might be to use the previous incarnation of the
word-count program on page 20.
my code *is* the modified-reincarnation of that programme. without
this modification, it does not work

Mar 21 '07 #3

Change the code like this, maybe you can get you want.

if(c == ' ' || c == '\t' || c == '\n')
{
state = OUT;
--nc;
}
if(state == OUT)
{
lwords[aindex++] = nc;
++nw;
state = IN;
nc = 0;
}

Mar 21 '07 #4
On Mar 21, 1:27 pm, "Gawain" <xia...@gmail.c omwrote:
Change the code like this, maybe you can get you want.

if(c == ' ' || c == '\t' || c == '\n')
{
state = OUT;
--nc;
}

if(state == OUT)
{
lwords[aindex++] = nc;
++nw;
state = IN;
nc = 0;
}
actually i did that just 2 second before i saw your post :-)

but it does not work :-(. i will try more after Lunch.

see you after Lunch....

Mar 21 '07 #5
On Mar 21, 1:30 pm, "arnuld" <geek.arn...@gm ail.comwrote:
On Mar 21, 1:27 pm, "Gawain" <xia...@gmail.c omwrote:
Change the code like this, maybe you can get you want.
if(c == ' ' || c == '\t' || c == '\n')
{
state = OUT;
--nc;
}
if(state == OUT)
{
lwords[aindex++] = nc;
++nw;
state = IN;
nc = 0;
}

actually i did that just 2 second before i saw your post :-)

but it does not work :-(. i will try more after Lunch.

see you after Lunch....

That should work .. Gawain has suggested two changes:
1. "else if" changed to "if"
2. assign "nc = 0" under the above condition.

Did you try with both these changes ?

PS: My apologies if this post does not appear properly.
Right now, my google interface is not displaying the
cursor as I type :-(

Mar 21 '07 #6
On Mar 21, 2:24 pm, p_cricket_...@y ahoo.co.in wrote:

That should work .. Gawain has suggested two changes:
1. "else if" changed to "if"
SORRY, i overlooked "if". i thought it was same as my code "else if".

2. assign "nc = 0" under the above condition.
i did that with "else if" and it did not work.
Did you try with both these changes ?
with this code changed to "if" and "nc = 0". it works :-) except of a
problem. here is the output:

[arch@voodo kr2]$ ./a.out
like---------- printing HISTOGRAM -----------
[arch@voodo kr2]$ ./a.out
like
---------- printing HISTOGRAM -----------
****
[arch@voodo kr2]$ ./a.out
like this about a
---------- printing HISTOGRAM -----------
****
****

*****
*
[arch@voodo kr2]$
it produces extra "newlines" for "tabs" entered.

how can i make it consistent ?

PS: My apologies if this post does not appear properly.
Right now, my google interface is not displaying the
cursor as I type :-(
i thought i was the only one who is facing this weired problem. i got
company now :-)

Mar 21 '07 #7
i checked the solution at this page:

http://clc-wiki.net/wiki/K%26R2_solu..._1:Exercise_13

it is way-way complicated for me to understand.

any help ?

Mar 21 '07 #8
arnuld said:
i checked the solution at this page:

http://clc-wiki.net/wiki/K%26R2_solu..._1:Exercise_13

it is way-way complicated for me to understand.

any help ?
Which part don't you understand?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 21 '07 #9
On Mar 21, 6:16 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:

Which part don't you understand?
nearly, 90% of the solution. it is weired, something like a "syntax-
mess". i will ask in parts:

1.) what exactly these variables are doing:

long lengtharr[MAXWORDLEN + 1]; /* i know this is an array */
int firstletter = 1;
long thisval = 0;
long maxval = 0;
int thisidx = 0;
int done = 0;

2.) what is this code doing:

if(wordlen 0) /* i know what this line is doing */
{
thisval = ++lengtharr[wordlen - 1];
if(thisval maxval)
{
maxval = thisval;

Mar 21 '07 #10

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

Similar topics

12
2155
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which give me strange behavior. Here is the code I've written: /****************************************************************************** * K&R2...
16
2263
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of blanks as a single one */ int main() { int c;
2
2283
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers yet. i have created a solution myself by modifying the example programme of section 1.19. i tried to find the source-code of K&R2 using Google. i...
8
4750
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
4
1557
by: arnuld | last post by:
as i said, i have restarted the book because i overlooked some material. i want to have some comments/views on this solution. it runs fine, BTW. ------------------ PROGRAMME -------------- /* K&R2 section 1.5.3, exercise 1-8 write a programme to count blanks, tabs and newlines */
16
1792
by: arnuld | last post by:
i have created solution which compiles and runs without any error/ warning but it does not work. i am not able to understand why. i thought it is good to post my code here for correction before looking at CLC-Wiki for K&R2 solutions: --------------- PROGRAMME ------------ /* K&R2 section 1.5.3, exercise 1-9 STATEMENT: write a programme...
2
1661
by: arnuld | last post by:
i was not even able to understand how should i proceed tow rite solution for this exercise: "Write a program to print a histogram of the frequencies of different characters in its input." then i tried this: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_14 i did not understand the following in Histogram solution:
4
1837
by: arnuld | last post by:
any suggestions for improvement: -------------- PROGRAMME ------------- /* K&R2: section 1.5.3 exercise 1-10 STATEMENT: write a programme to copy its input to output, replacing each TAB by '\t', BACKSPACE by '\b' and each backslash by '\\'. */
16
1720
by: arnuld | last post by:
i am not able to make it work. it compiles without any error but does not work: what i WANTED: 1.) 1st we will take the input in to an array. (calling "getline" in "main") 2.) we will print that input array on terminal. (in "main") 3.) we will reverse the array. (calling "reverse" in "main") 4.) we will print that reversed array. (in...
88
3707
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without invoking undefined behaviour triggered by overflow? Remember that the constants in limits.h cannot be used.
0
7888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7811
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8314
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7922
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8185
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.