473,651 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Giving the histogram a shot...

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 need to determine what a word actually is.
I wrote this program on my own without looking at the book or any
other resource once.

#include <stdio.h>
main()
{
int c;
int nword, nother;

nword = nother = 0;

while ((c = getchar()) != EOF)
{
if (c == ' ' ¦¦ '\t' ¦¦ '\n')
++nother;
else
++nword;
}
printf("Words = %d\nOther = %d", nword, nother);
}

I am basically just trying to tell the computer anything that is not a
blank space, a tab , or a newline is a word...
BUT everytime I run the program it adds to only the nother variable.
I can't figure out why, no matter what I type. I thought I had
written this program well and I even sketched it out on paper
beforehand , hehe.

I know you guys will probably find a horribly noobish mistake , but
please remember I started learning C all of like 48 hours ago.
Nov 13 '05 #1
27 2592
ext_u <ex***********@ hotmail.com> wrote in message
news:32******** *************** ***@posting.goo gle.com...
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 need to determine what a word actually is.
I wrote this program on my own without looking at the book or any
other resource once.

#include <stdio.h>
main()
int main()
{
int c;
Encouragement:

Very good. Many novices make the mistake of
defining a 'char' for getchar() to store data
in. It must be 'int' as you have it, so it
can store EOF which is not guaranteed to fit
in a char.

int nword, nother;

nword = nother = 0;
Informative:

Rather than defining and then assigning after the fact,
you can give your variable initial values at definition time:

int nword = 0;
int nother = 0;

I recommend defining only one variable per line.
The reasons for this will become evident as you progress
(essentially makes the code easier to read and maintain,
and prevents possibly 'silly' mistakes, especially when
you start to work with pointers).


while ((c = getchar()) != EOF)
{
if (c == ' ' ¦¦ '\t' ¦¦ '\n')
++nother;
else
++nword;
}
printf("Words = %d\nOther = %d", nword, nother);
}

I am basically just trying to tell the computer anything that is not a
blank space, a tab , or a newline is a word...
More encouragement:

Well, I'm sure you realize that's not the ultimate goal,
but I'm glad to see you simplify things so you can get
*something* working.
BUT everytime I run the program it adds to only the nother variable.
I can't figure out why, no matter what I type. I thought I had
written this program well and I even sketched it out on paper
beforehand , hehe.
That is a very good idea, although in this case, it doesn't
help. :-( Your problem is a misunderstandin g of operator
syntax.

I know you guys will probably find a horribly noobish mistake , but
please remember I started learning C all of like 48 hours ago.


Yes, you did make a (very common) novice mistake. Don't feel bad,
many others have done this. The problem is with your statement:

if (c == ' ' ¦¦ '\t' ¦¦ '\n')
++nother;

The comparison operator (==) takes exactly two operands.
You supplied 'c' as the 'left-hand' operand, and the
expression, ( ' ' || '\t' || '\n' ) as the 'right-hand'
operand. The 'logical or' operator (||) returns true
if either of its operands yields a nonzero (true) value.
None of ' ', '\t', or '\n' have a value of zero, so 'or-ing'
any or all of them together will always yield nonzero (true).

To express 'if c is equal to any of ' ', '\t', or '\n', you
need to express three distinct comparisons, 'or-d' together.
Write:

if (c == ' ' || c == '\t' || c == '\n')
++nother;
else
++nword;

You made a very good try. Great work.

-Mike


Nov 13 '05 #2

Mike Wahler <mk******@mkwah ler.net> wrote in message
news:ds******** *******@newsrea d4.news.pas.ear thlink.net...

After looking at Kevin's reply, and seeing how mine
appears, it seems you might be using the wrong characters
to express logical 'or'. On a U.S. PC keyboard, it's the
shifted 'backslash' key. If you have some other keyboard,
I don't know which it is. Better check that out.

[snip]
Yes, you did make a (very common) novice mistake. Don't feel bad,
many others have done this. The problem is with your statement:

if (c == ' ' ¦¦ '\t' ¦¦ '\n')
++nother;
I copy/pasted the above from your post.

The comparison operator (==) takes exactly two operands.
You supplied 'c' as the 'left-hand' operand, and the
expression, ( ' ' || '\t' || '\n' ) as the 'right-hand'
operand. The 'logical or' operator (||) returns true
if either of its operands yields a nonzero (true) value.
None of ' ', '\t', or '\n' have a value of zero, so 'or-ing'
any or all of them together will always yield nonzero (true).

To express 'if c is equal to any of ' ', '\t', or '\n', you
need to express three distinct comparisons, 'or-d' together.
Write:

if (c == ' ' || c == '\t' || c == '\n')
++nother;
else
++nword;


Note how the 'or' operator appears different here.

-Mike

Nov 13 '05 #3
ext_u wrote:

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)
Don't keep starting new threads about the same thing. By posting
a reply in the original thread, and snipping what isn't germane,
you don't have to remind people about it. And it also makes it
easier for them to look back, if needed.
Anways first .. I obviously need to determine what a word actually is.
I wrote this program on my own without looking at the book or any
other resource once.
Good.

#include <stdio.h>
main()
get in the habit of writing "int main(void)" or
"int main(int argc; char *argv)"
{
int c;
int nword, nother;

nword = nother = 0;

while ((c = getchar()) != EOF)
{
if (c == ' ' ¦¦ '\t' ¦¦ '\n')
This should be the logical or of three logical statements. As it
is it won't do what you want. Try:

if ((c == ' ') || (c == '\t') || (c == '\n'))

think about it, and you will see the difference. Some may say the
parentheses are redundant, but it makes the statement perfectly
clear.

BTW, better to use an indentation of 3 or 4 spaces, 8 is too
much. So don't use tabs (if you are using them).
++nother;
else
++nword;
}
printf("Words = %d\nOther = %d", nword, nother);
}

I am basically just trying to tell the computer anything that is not a
blank space, a tab , or a newline is a word...
You are saying that every char is a word, unless it is ...
BUT everytime I run the program it adds to only the nother variable.
I can't figure out why, no matter what I type. I thought I had
written this program well and I even sketched it out on paper
beforehand , hehe.

I know you guys will probably find a horribly noobish mistake , but
please remember I started learning C all of like 48 hours ago.


You are doing fine.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #4
ext_u wrote:
I know you guys will probably find a horribly noobish mistake , but
please remember I started learning C all of like 48 hours ago.


<snip>

We're interested in four things:

[1] Determining if a character is part of a word
[2] Finding the first character of each word.
[3] Finding the first character after a word
[4] Counting the characters in a word

I took a try at the problem; and wrote main first; and just
assumed that I could write a word_char() function - so all I
needed to worry about were [2], [3], and [4]. I added EOF to your
list of getchar() input values that could not appear in a word
and then added the word_char() function to satisfy [1].

#include <stdio.h>

int word_char(int c)
{ return (c != ' ') && (c != '\t') && (c != '\n') && (c != EOF);
}

int main(void)
{ int c, chars=0, count[64], i, in_word=0;

for (i=0; i<64; i++) count[i] = 0;

do
{ c = getchar();
if (!in_word && word_char(c))
{ in_word = 1;
chars = 1;
}
else if (in_word)
{ if (word_char(c)) ++chars;
else
{ ++count[chars];
in_word = 0;
}
}
} while (c != EOF);

for (i=1; i<64; i++)
{ if (count[i])
printf("There were %d words with %d letters\n",
count[i], i);
}
return 0;
}

The program makes the assumption that there won't be any words
with more than 64 characters, and may behave /very/ badly if a
longer word is encountered; but I wanted to write a simple "quick
and dirty" example. I made the assumption that you've already
encountered the do {} while () loop construction.

HTH
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

Nov 13 '05 #5
Mike Wahler <mk******@mkwah ler.net> wrote:
[...]
Yes, you did make a (very common) novice mistake. Don't feel bad,
many others have done this. The problem is with your statement:

if (c == ' ' ?? '\t' ?? '\n')
++nother;

The comparison operator (==) takes exactly two operands.
You supplied 'c' as the 'left-hand' operand, and the
expression, ( ' ' || '\t' || '\n' ) as the 'right-hand'
operand.
At risk of over-complicating the thread, == has higher precedence than
|| (for the OP, the table on page 53 of K&R2 is worth bookmarking...) ,
so the operands of the == operator in this case are c and ' '. In fact,
you rely on this precedence later in your message:

[...] Write:

if (c == ' ' || c == '\t' || c == '\n')


- Kevin.

Nov 13 '05 #6
CBFalconer wrote:
ext_u wrote:
.... snip ...
#include <stdio.h>
main()


get in the habit of writing "int main(void)" or
"int main(int argc; char *argv)"


Make that last "char **argv". Someone e-mailed me but didn't
bother to put the correction up here. And no, the compiler won't
diagnose it.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #7
In article <3F************ ***@yahoo.com>, cb********@yaho o.com says...
CBFalconer wrote:
ext_u wrote:
... snip ...
#include <stdio.h>
main()


get in the habit of writing "int main(void)" or
"int main(int argc; char *argv)"


Make that last "char **argv". Someone e-mailed me but didn't
bother to put the correction up here. And no, the compiler won't
diagnose it.


You sure you want that semicolon up there instead of a comma? :-)

Nov 13 '05 #8
ext_u wrote:
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) if (c == ' ' ¦¦ '\t' ¦¦ '\n')


This doesn't mean what you thought it meant. It means

if c == ' '
or '\t' != 0
or '\n' != 0

and, since neither the tab character nor the newline character are
equal to 0, the condition is always true. You want

if (c == ' ' || c == '\t' || c == '\n') ...
or
#include <ctype.h>

if (isspace( c )) ...

if you're prepared to accept form feed, carraige return, and vertical
tab as separators as well.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #9
Randy Howard wrote:
cb********@yaho o.com says...
CBFalconer wrote:
ext_u wrote:
>

... snip ...
>
> #include <stdio.h>
> main()

get in the habit of writing "int main(void)" or
"int main(int argc; char *argv)"


Make that last "char **argv". Someone e-mailed me but didn't
bother to put the correction up here. And no, the compiler won't
diagnose it.


You sure you want that semicolon up there instead of a comma? :-)


Woops. Yes, that one would get diagnosed. :-)

I found something from ext_u on my spam trap. If he has trouble
with DJGPP the place to go is comp.os.msdos.d jgpp.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #10

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

Similar topics

0
6695
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 really puzzled why it doesn't work public PlanarImage thresholding (PlanarImage source) { // set up the histogram int bins = { 256 }; double low = { 0.0D };
1
3065
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 that direction I'd be much obliged... TIA
12
2842
by: KraftDiner | last post by:
Hi, I wrote a C++ class that implements an n dimensional histogram in C++, using stl maps and vectors. I want to code this up now in Python and would like some input from this group. The C++ class was VERY simple.. std::map<std::vector<unsigned short>, unsigned long> histo; Say for example I want a 3D histogram then std::vector<unsigned short> would contains
5
11768
by: Enigma Curry | last post by:
I'm playing around with matplotlib for the first time. I'm trying to make a very simple histogram of values 1-6 and how many times they occur in a sequence. However, after about an hour of searching I cannot make the histogram stay within the bounds of the grid lines. Here is my example: pylab.grid() x_values= pylab.hist(x_values,6)
2
4271
by: Daniel Nogradi | last post by:
How does one do a histogram on only a part of an image? This is what I found in the PIL documentation about histogram( ): """ im.histogram(mask) =list Returns a histogram for those parts of the image where the mask image is non-zero. The mask image must have the same size as the image, and be either a bi-level image (mode "1") or a greyscale image ("L"). """
5
2914
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 compile-error BUT it has a semantic BUG: what i WANT: I want it to produce a "horizontal histogram" which tells how many characters were in the 1st word, how many characters were in the second word by writing equal number of stars, *, at the...
12
4477
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: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_13 still i am not able to make sense of many-many things like (on the same page) for my solution i have written this in documentation,to give reader some hint about what i thought before...
1
4629
by: avenger3200 | last post by:
Hello everyone, I am trying to make a histogram for a project in my class and I really need some help. Here is the question that my instructor provided: Create 1000 Random numbers between 0-100. Create a histogram of the values as a list. Make the bin range 10. Your book goes through a more complicated, but similar exercises. Here is the code that I have so far: import random list =
0
1462
by: Kurt Smith | last post by:
On Fri, Jul 25, 2008 at 5:02 PM, aditya shukla <adityashukla1983@gmail.comwrote: the 'bins' argument to pylab.hist() is supposed to be an integer or a list of the bins' lower edges. The default value is 10, more than that gives smaller bins, as one would expect. Take a look at the pylab.hist documentation (you can do 'print pylab.hist.__doc__' from the command interpreter). You should have no problem plotting a hist of floats. Try...
0
8695
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8576
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
7296
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...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5609
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
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
2
1585
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.