By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,636 Members | 1,814 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,636 IT Pros & Developers. It's quick & easy.

Struggling with K&R

P: n/a
JFS
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
In this chapter a program that counts the digits, white spaces, and
others is introduced.. They go through the defining of an array (ndigit
= [10], yadda yadda), fairly simple stuff. They also give a very good
explaination of statement and condition testing for if's and else's.
These are the exercises.

"Exercise 1-13 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."

Ok , I am going through the book very slowly and I think I have just
enough comprehension to get the first part of the exercise right. BUT
they have gone over absolutley nothing about histograms , graphs , or
even damn tables of data. How in the world do they expect me to be able
to print the output in a histogram?

"Exercise 1-14 Write a program to print a histogram of the frequencies
of different characters in its output."

I can't even comprehend that one. It really makes me feel like a moron.

Honestly I don't even have enough comprehension to write a program that
measures the lengths of words. I thought I could go about it but
everything I tried doesn't work at all and I just can't do it. I am
beginning to think I am an idiot and programming may not be right for
me. Hell , I can't even finish the exercises in Chapter 1 of this book.

Aug 23 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
JFS wrote:
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.
[Aside: make sure you have the second edition. The first is pre-ANSI/ISO.]
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.
That won't help. No-one, I hope, is holding a gun to your head saying
DO YOUR EXERCISES. Step back. Take a walk, have a non-psychoactive
drink, read a good book. Grumps make for bad decisions.
Here is an example of what I am talking about:

Chapter 1.6 Arrays
In this chapter a program that counts the digits, white spaces, and
others is introduced.. They go through the defining of an array (ndigit
= [10], yadda yadda), fairly simple stuff. They also give a very good
explaination of statement and condition testing for if's and else's.
These are the exercises.

"Exercise 1-13 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."

Ok , I am going through the book very slowly and I think I have just
enough comprehension to get the first part of the exercise right. BUT
they have gone over absolutley nothing about histograms , graphs , or
even damn tables of data. How in the world do they expect me to be able
to print the output in a histogram?
They're not expecting anything fancy. A histogram just has bars
(representing the amount of something) associated with the something.
Like [use a fixed-width font]:

digits xxxxxxxxxxxxxxxxxxxxxxx
letters xxxxxxxxxxx
whites xxx
punct xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxx

which you can do with the materials already provided, since a `for`
loop will output the x's.
"Exercise 1-14 Write a program to print a histogram of the frequencies
of different characters in its output."

I can't even comprehend that one. It really makes me feel like a moron.
Same deal; you're just counting each character rather than kinds of
characters.
Honestly I don't even have enough comprehension to write a program that
measures the lengths of words. I thought I could go about it but
everything I tried doesn't work at all and I just can't do it.
If you've tried to write something and it doesn't work, ask a
specific question & someone here will likely lend a hand. But
/show code/; don't hide it away even if you think it's horrid.
Without seeing what kind of mistake you're making, it's hard
to suggests a fix or identify a misunderstanding.

--
Unfixed Width, Sadly Hedgehog
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Aug 23 '07 #2

P: n/a
[comp.lang.c] JFS <an*******@dev.nullwrote:
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:
As someone who (admittedly) has written a significant amount of code
in a state of some frustration, I promise you that the "very very
angry" point is a great time for a coffee break, a nap, or calling it
a day. Zen makes for good coding (I rather suck at Zen as well,
unfortunately.)
"Exercise 1-13 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."
Ok , I am going through the book very slowly and I think I have just
enough comprehension to get the first part of the exercise right. BUT
they have gone over absolutley nothing about histograms , graphs , or
even damn tables of data. How in the world do they expect me to be able
to print the output in a histogram?
They SUSpect, but do not EXpect, that you know what a histogram is.
Wikipedia can help:

http://en.wikipedia.org/wiki/Histogram

You can see that the question is about two things - computing the
lengths of various words, and outputting them in some format or other.
Simple output might look like

Word length Count
1 1
3 1
4 1
5 1

given the input, "I like ice cream". The printing of this in ASCII
art is a separate problem that you can feel free to view as optional.
Honestly I don't even have enough comprehension to write a program that
measures the lengths of words. I thought I could go about it but
everything I tried doesn't work at all and I just can't do it. I am
beginning to think I am an idiot and programming may not be right for
me. Hell , I can't even finish the exercises in Chapter 1 of this book.
Well, I certainly don't want to call you an "idiot", although I can't
guarantee that you will enjoy programming. Why not post the code
you've tried for these questions? We can comment on it and hopefully
alleviate some of your frustration. K&R is a good, thorough, and
comprehensive book, but many note that it can be difficult for
beginners, as you are finding.

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Aug 23 '07 #3

P: n/a
JFS wrote, On 23/08/07 21:38:
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:
You need to step back, chill, and look again when you are calm.
Chapter 1.6 Arrays
In this chapter a program that counts the digits, white spaces, and
others is introduced.. They go through the defining of an array (ndigit
= [10], yadda yadda), fairly simple stuff. They also give a very good
explaination of statement and condition testing for if's and else's.
These are the exercises.

"Exercise 1-13 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."

Ok , I am going through the book very slowly and I think I have just
enough comprehension to get the first part of the exercise right. BUT
they have gone over absolutley nothing about histograms , graphs , or
even damn tables of data. How in the world do they expect me to be able
to print the output in a histogram?
Do you know what a histogram is? It's a maths concept, not a computing
concept, so don't expect computer books to explain it. Here is some
stuff about them, but I've not validated it
http://en.wikipedia.org/wiki/Histogram

At to printing one, just use "ASCII art" i.e. draw it with characters. E.g.

5 #####
7 #######
2 ##
"Exercise 1-14 Write a program to print a histogram of the frequencies
of different characters in its output."

I can't even comprehend that one. It really makes me feel like a moron.
First write a program to just count the number of each type of character
and work from there.
Honestly I don't even have enough comprehension to write a program that
measures the lengths of words. I thought I could go about it but
everything I tried doesn't work at all and I just can't do it. I am
beginning to think I am an idiot and programming may not be right for
me. Hell , I can't even finish the exercises in Chapter 1 of this book.
Without seeing any of your attempts we can't really guide you. You might
just have one thing muddled which can be sorted, your you might not
think in the right way for programming, or anything in between.

If you post some attempts at writing programs people will make
suggestions and try to help.
--
Flash Gordon
Aug 23 '07 #4

P: n/a

"JFS" <an*******@dev.nullwrote in message
news:sl********************@nospam.invalid...
>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
In this chapter a program that counts the digits, white spaces, and
others is introduced.. They go through the defining of an array (ndigit
= [10], yadda yadda), fairly simple stuff. They also give a very good
explaination of statement and condition testing for if's and else's.
These are the exercises.

"Exercise 1-13 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."

Ok , I am going through the book very slowly and I think I have just
enough comprehension to get the first part of the exercise right. BUT
they have gone over absolutley nothing about histograms , graphs , or
even damn tables of data. How in the world do they expect me to be able
to print the output in a histogram?

"Exercise 1-14 Write a program to print a histogram of the frequencies
of different characters in its output."

I can't even comprehend that one. It really makes me feel like a moron.

Honestly I don't even have enough comprehension to write a program that
measures the lengths of words. I thought I could go about it but
everything I tried doesn't work at all and I just can't do it. I am
beginning to think I am an idiot and programming may not be right for
me. Hell , I can't even finish the exercises in Chapter 1 of this book.
Let's break it down.

You feed the program a lot of input from stdin. This could be the keyboard,
but probably you'll want to pipe a text file to it.

Now if we define a "word" as consecutive letters, ignoring awkward cases
like "it's", and a non-word as anything that is not a letter, we can read
the input character by character, and maintain two states. If we are in
state "word", and we get a latter, we add one to the length of the current
word. If we are in state word and get a non-letter, we store the length and
change state to non-word. If we are in state non-word and get a non-letter,
we stay in state non-word and ignore it. If we get a letter, we change state
to word and set the length of the current word to one.

We can also say that anything over 20 letters just gets counted as "over
20".

So, start with main

#include <stdio.h>

int main(void)
{
int ch;

while( (ch = fgetc(fp)) != EOF)
fputc(ch, stdout);
return 0;
}

This program just echoes the input to the output. Get that working first, so
you know you have IO, and are in business.

#define NON_WORD 0
#define WORD 1

int main(void)
{
int ch;
int state = NON_WORD;

while( (ch = fgetc(fp)) != EOF)
{
if(state == NON_WORD)
{
if(isalpha(ch))
{
state = WORD;
printf("\nWORD\n");
}
}
else if(state == WORD)
{
if(!isalpha(ch))
{
state = NON_WORD;
printf("\nNON_WORD\n");
}
}

fputc(ch, stdout);
}
return 0;
}

Now get that working. It should print out WORD / NON_WORD as it detects
words in the input.

Now add a length variable. Modify it so that when it changes to non-word, it
prints out the length. You will need to add some more logic.

The add an array of 21 lengths to hold all the counts. Make the alst hold
every word of twenty letters or above. So your logic is

if(length < 20)
word[length]++;
else
word[20]++;

Then print out the counts, in digits, and make sure they are right. Be
careful about the very last word, you might need some extra logic.

Finally you need the histogram. Get the rest working, then we'll tackle
ASCII art.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Aug 23 '07 #5

P: n/a
On Aug 23, 1:38 pm, JFS <anonym...@dev.nullwrote:
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
In this chapter a program that counts the digits, white spaces, and
others is introduced.. They go through the defining of an array (ndigit
= [10], yadda yadda), fairly simple stuff. They also give a very good
explaination of statement and condition testing for if's and else's.
These are the exercises.

"Exercise 1-13 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."

Ok , I am going through the book very slowly and I think I have just
enough comprehension to get the first part of the exercise right. BUT
they have gone over absolutley nothing about histograms , graphs , or
even damn tables of data. How in the world do they expect me to be able
to print the output in a histogram?
Look up what 'histogram' means and then do it.
"Exercise 1-14 Write a program to print a histogram of the frequencies
of different characters in its output."
http://encarta.msn.com/encnet/featur...fid=1861618113

Recursive exercise:
For every word in the above definition you do not understand, look up
that word.
Continue until every word in every definition is understood.
Then you will understand exactly what a histogram is.
I can't even comprehend that one. It really makes me feel like a moron.
Will Rogers said it best: "Everybody is ignorant. Only in different
areas."
Honestly I don't even have enough comprehension to write a program that
measures the lengths of words.
I doubt if this is true. You seem coherent enough and finished a
prior simple exercise.
I thought I could go about it but
everything I tried doesn't work at all and I just can't do it. I am
beginning to think I am an idiot and programming may not be right for
me. Hell , I can't even finish the exercises in Chapter 1 of this book.
All you need is a dictionary for the unfamiliar words.

I think you are making it harder than it really is for yourself. Of
course, you cannot proceed if you do not understand the term. Instead
of pulling your hair out, figure out what the term means. The terms
used are all simple terms. You are smart enough to understand them.

Aug 23 '07 #6

P: n/a
[comp.lang.c] Richard Heathfield <rj*@see.sig.invalidwrote:
user923005 said:
>For every word in the above definition you do not understand, look up
that word.
Continue until every word in every definition is understood.
Then you will understand exactly what a histogram is.
Baulking at the idea of using an Encarta definition, I tried my trusty
Chambers instead, taking the first definition in each case.
Richard, you are truly a font of knowledge - I was fully prepared to
point out that you had misspelled "balk", but lo and behold, it's just
another case of the Queen shipping her words to America without the
'u's. Maybe a Chinese supplier is to blame?
a: the first letter in the modern English alphabet as in the Roman
Ah, but here you have exceeded the bounds of the exercise, unless we
are to presume that the definition of 'a' is not understood. (Then
again, if the President doesn't know what "is" means, perhaps this
supposition is not so far-fetched after all.)
Alas (and quite possibly alack, too)! We are already undone, for mutual
recursion has struck into the very heart of our exercise: "a" is
defined in terms of "in", and "in" is defined in terms of "a".
An interesting (if time consuming) exercise would be to devise a set
of "axiom words", whose definitions are assumed, with which to define
all other words.

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Aug 24 '07 #7

P: n/a
Christopher Benson-Manica said:
[comp.lang.c] Richard Heathfield <rj*@see.sig.invalidwrote:
>Baulking at the idea of using an Encarta definition, I tried my
trusty Chambers instead, taking the first definition in each case.

Richard, you are truly a font
or fount
of knowledge - I was fully prepared to
point out that you had misspelled "balk", but lo and behold, it's just
another case of the Queen shipping her words to America without the
'u's. Maybe a Chinese supplier is to blame?
No, it's for historical reasons: the Americans never really recovered
from heavy U-boat activity during World War II.
>a: the first letter in the modern English alphabet as in the Roman

Ah, but here you have exceeded the bounds of the exercise, unless we
are to presume that the definition of 'a' is not understood.
It's a character constant. It doesn't need to be defined. :-)
An interesting (if time consuming) exercise would be to devise a set
of "axiom words", whose definitions are assumed, with which to define
all other words.
Or perhaps to establish which set of words is defined in a mutually
recursive manner.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 24 '07 #8

P: n/a
Christopher Benson-Manica wrote:
>
.... snip ...
>
An interesting (if time consuming) exercise would be to devise a
set of "axiom words", whose definitions are assumed, with which
to define all other words.
I think Euclid did something of the sort a few years back.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 25 '07 #9

P: n/a
On Fri, 24 Aug 2007 01:45:41 +0000, Richard Heathfield wrote:
user923005 said:

<snip>
>Recursive exercise:
For every word in the above definition you do not understand, look up
that word.
Continue until every word in every definition is understood.
Then you will understand exactly what a histogram is.

Baulking at the idea of using an Encarta definition, I tried my trusty
Chambers instead, taking the first definition in each case.
[...]
Alas (and quite possibly alack, too)! We are already undone, for mutual
recursion has struck into the very heart of our exercise: "a" is
defined in terms of "in", and "in" is defined in terms of "a".
Re-reads words from the eighth to the eleventh after the colon.
If the OP doesn't understand either "a" or "in" he'll have much
greater troubles reading K&R...
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 26 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.