473,386 Members | 1,962 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,386 software developers and data experts.

help me! -- K&R exercise 1-11

I want to know the correct way to answer exercise 1-11 of K&R.
The only bug that I can find is that nw counts one to many words.
(if there are 8 words, nw will be 9) Am I correct aor is there
more to it

leonard

Nov 13 '05 #1
6 3767
leonard greeff <bi*****@xtra.co.nz> wrote:
I want to know the correct way to answer exercise 1-11 of K&R.
The only bug that I can find is that nw counts one to many words.
(if there are 8 words, nw will be 9)

Are you sure?
Hint: To test a program, I would compile and run it...

Regards

Irrwahn
--
What does this red button do?
Nov 13 '05 #2
leonard greeff wrote:
I want to know the correct way to answer exercise 1-11 of K&R.
The only bug that I can find is that nw counts one to many words.
(if there are 8 words, nw will be 9) Am I correct aor is there
more to it

leonard


Your problem is in the "for" loop. The loop should count
from zero to "nw < 9".

If that doesn't work, post your code along with the error
message from the compiler, or stating the actual results
and the expected results.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Nov 13 '05 #3
Thomas Matthews <Th****************************@sbcglobal.net> wrote:
leonard greeff wrote:
I want to know the correct way to answer exercise 1-11 of K&R.
The only bug that I can find is that nw counts one to many words.
(if there are 8 words, nw will be 9) Am I correct aor is there
more to it

leonard


Your problem is in the "for" loop. The loop should count
from zero to "nw < 9".

If that doesn't work, post your code along with the error
message from the compiler, or stating the actual results
and the expected results.


In the copy of K&R2 I own there is no "for"-loop in sample program
1.5.4, which is the one exercise 1.11 refers to...

Regards

Irrwahn
--
What does this red button do?
Nov 13 '05 #4
leonard greeff wrote:
I want to know the correct way to answer exercise 1-11 of K&R.
The only bug that I can find is that nw counts one to many words.
(if there are 8 words, nw will be 9) Am I correct aor is there
more to it

leonard


This answer was taken from The C answer book by
Clovis L Tondo & Scott E Gimpel
which gives all the answers to K&R

To test the word count program first try no input,
The output should be: 0 0 0 (zero newlines, zero words, zero characters).
Then try a one-character word. The output should be: 1 1 2.
(one new line,one word, two characters-a letter followed by
a new line character). Then try a two character word.
the output should be: 1 1 3 (one new line, one word,
three chacters- two chacters followed by a new line character).
In addition, try 2 one-chacter words (the output should be 1 2 4) and
2 one-character words-one word per line(the output should be 2 2 4).
The kinds of input most likely to uncover bugs are those that test boudary
conditions. Some boundaries are:

-no input
-no words-just new lines
-no words-just blanks, tabs, and newlines
-one word per a line-no blanks and tabs
-word starting at the beginning of the line
-word starting after some blanks
Nov 13 '05 #5
leonard greeff wrote:
I want to know the correct way to answer exercise 1-11 of K&R.
The only bug that I can find is that nw counts one to many words.
(if there are 8 words, nw will be 9) Am I correct aor is there
more to it

leonard


This answer was taken from The C answer book by
Clovis L Tondo & Scott E Gimpel
which gives all the answers to K&R

To test the word count program first try no input,
The output should be: 0 0 0 (zero newlines, zero words, zero characters).
Then try a one-character word. The output should be: 1 1 2.
(one new line,one word, two characters-a letter followed by
a new line character). Then try a two character word.
the output should be: 1 1 3 (one new line, one word,
three chacters- two chacters followed by a new line character).
In addition, try 2 one-chacter words (the output should be 1 2 4) and
2 one-character words-one word per line(the output should be 2 2 4).
The kinds of input most likely to uncover bugs are those that test boudary
conditions. Some boundaries are:

-no input
-no words-just new lines
-no words-just blanks, tabs, and newlines
-one word per a line-no blanks and tabs
-word starting at the beginning of the line
-word starting after some blanks
Nov 13 '05 #6
amanayin wrote:
leonard greeff wrote:

I want to know the correct way to answer exercise 1-11 of K&R.
The only bug that I can find is that nw counts one to many words.
(if there are 8 words, nw will be 9) Am I correct aor is there
more to it

leonard

This answer was taken from The C answer book by
Clovis L Tondo & Scott E Gimpel
which gives all the answers to K&R

To test the word count program first try no input,
The output should be: 0 0 0 (zero newlines, zero words, zero characters).
Then try a one-character word. The output should be: 1 1 2.
(one new line,one word, two characters-a letter followed by
a new line character). Then try a two character word.
the output should be: 1 1 3 (one new line, one word,
three chacters- two chacters followed by a new line character).
In addition, try 2 one-chacter words (the output should be 1 2 4) and
2 one-character words-one word per line(the output should be 2 2 4).
The kinds of input most likely to uncover bugs are those that test boudary
conditions. Some boundaries are:

-no input
-no words-just new lines
-no words-just blanks, tabs, and newlines
-one word per a line-no blanks and tabs
-word starting at the beginning of the line
-word starting after some blanks


ahhh! I see, thanks.

Nov 13 '05 #7

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

Similar topics

14
by: TrvlOrm | last post by:
OK. After much playing around, I managed to get my frame page this far.. see code below. BUT...there are still errors with it, and what I would like to have happened is this: 1) On the Left...
12
by: Merrill & Michele | last post by:
It's very difficult to do an exercise with elementary tools. It took me about fifteen minutes to get exercise 1-7: #include <stdio.h> int main(int orange, char **apple) { int c; c=-5;...
8
by: Mike S | last post by:
Hi all, I noticed a very slight logic error in the solution to K&R Exercise 1-22 on the the CLC-Wiki, located at http://www.clc-wiki.net/wiki/KR2_Exercise_1-22 The exercise reads as...
16
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...
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...
19
by: arnuld | last post by:
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...
9
by: JFS | last post by:
I know most of you have probably read "The C Programming Language" (K&R) at some point. Well here is something that is driving me crazy. The exercises are impossible (most of them) for me to do....
1
by: Webstorm | last post by:
Hi, I hope someone can help me sort this out a bit, Im completely lost. Here is the page I am working on: http://www.knzbusinessbrokers.com/default.asp I have 3 search critera that I need to...
88
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.