473,776 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3806
leonard greeff <bi*****@xtra.c o.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.l earn.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
2169
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 Frame (File LeftEx8_2.html) a series of buttons, which when clicked prompt the user to enter information for background color, text color, link color, title and some text.
12
2337
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; while(c != EOF ) {
8
3087
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 follows: "Write a program to 'fold' long input lines into two or more shorter
16
2281
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;
11
4019
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 answer I'm looking for a point in the right direction..... #include <stdio.h>
19
2407
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 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
9
3044
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. I am not even done with the first chapter and I am already ripping out my hair trying to get the exercises completed. It is driving me crazy and making me very very angry. Here is an example of what I am talking about: Chapter 1.6 Arrays...
1
2797
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 use when querying the database. Right now it is only looking for a match on one of those dropdowns and not all 3. can anyone help? Here is the code: <form BOTID="0" METHOD="POST" action="businessforsale_interface/Results/test3.asp">
88
3796
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
9628
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10289
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10061
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8952
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.