473,549 Members | 2,733 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 2577
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
6688
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...
1
3058
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
2829
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...
5
11762
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
4267
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...
5
2906
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...
12
4471
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...
1
4626
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...
0
1454
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...
0
7720
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
7960
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
7475
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
6048
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...
1
5372
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...
0
5089
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
3501
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...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.