Struggling with K&R

J

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.
 
F

Flash Gordon

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.
 
C

Chris Dollin

JFS said:
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 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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.
 
C

Christopher Benson-Manica

[comp.lang.c] JFS said:
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.
 
M

Malcolm McLean

JFS said:
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.
 
S

santosh

JFS said:
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:

As the authors point out, the book isn't really meant for someone completely
new to programming. However, it was my first programming book, (I did a bit
of BASIC a long while back, but that's negligible), and I found
it "doable". I'll grant you that many of the exercises are *far* more
challenging that what you'll find in books whose title include phrases
like "Teach Yourself ..." or "Complete Idiot's Guide ..." etc., but that's
a good thing, IMO, it forces you to sit down and think systematically, if
necessary, work out a design on paper, all good habits for later on.
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 the horizontal one first. It's simpler. It's simply a sequence of a
chosen character that you output to give a visual representation for the
relative magnitude of various items of your output.

An example:

Word length 3: ============================
Word length 4: ======================
Word length 5: ===============
etc.

A vertical histogram is a bit more complicated.
"Exercise 1-14 Write a program to print a histogram of the frequencies
of different characters in its output."

Break it into two steps. First calculate internally the frequency of
different characters. This can be done with an array and few other
variables, and incrementing them as you scan the input. Once you have the
data, printing them out is a separate problem, best done in a separate
function.
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.

Don't give up so easily. Attack the problem a step at a time. First just
write and compile an "empty" program. Then write a program that includes a
few simple char and int objects and assigns value to them, increments them
in a while or for loop. The try getting input from the user and assign to
these objects and learn to print them out with formatted output functions.

Then define arrays and try initialising them and assigning input into them
in a loop, and printing the value out. By this point you should be able to
write a "bare bones" program to calculate the frequency of characters in
it's input. Worry about the histogram later.

K&R _is_ difficult for a complete beginner, but it's doable, and the payoff
is much greater than many other books that ostensibly teach C programming.

BTW, the clc wiki contains solved examples for many exercises in K&R. You
might take a look at one or two to get an idea of how to structure your
program for similar problems.

Also, there is a separate book _The C Answer Book_ by Clovis and Tondo that
is solely composed of solutions to _all_ the exercises in K&R2. Get that
book too, if you can.
 
U

user923005

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/features/dictionary/DictionaryResults.aspx?refid=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.
 
R

Richard Heathfield

user923005 said:

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.

Step 0:
Current dictionary:
(null)
New definitions:
histogram: a statistical graph in which frequency distribution is shown
by means of rectangles.
-----------------------------------------------------------------------
Step 1:
Current dictionary:
histogram
New definitions:
a: the first letter in the modern English alphabet as in the Roman
by: at the side of
distribution: the act or process of distributing
frequency: resort
graph: a symbolic diagram
in: expressing the relation of a thing to that which surrounds,
encloses, includes or conditions it, with respect to place, time or
circumstances, or to that which is assumed, held, maintained, or the
relation of a right or possession to the person who holds or enjoys it

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".
 
C

Christopher Benson-Manica

[comp.lang.c] Richard Heathfield said:
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.
 
R

Richard Heathfield

Christopher Benson-Manica said:
[comp.lang.c] Richard Heathfield said:
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.
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.
 
C

CBFalconer

Christopher said:
.... 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.
 
A

Army1987

user923005 said:

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top