Re: good algorithms come with practice and reading good code/books?
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Sat, 30 Dec 2006 08:58:05 -0800
On 30 Dec 2006 01:53:10 -0800, "vlsidesign" <fordgwf@xxxxxxxxx> wrote:
I am a newbie and going through "The C programming language" by
Kernighan & Richie on my own time (I'm not a programmer but I want to
learn because it can save me time in my normal job, and it is kind of
fun). As I go through the book, I seek to do all the exercises because
they are very useful, and good, but it seems like I am just stumbling
through somewhat. In particular, I don't really know how to think about
"catching errors", or how my thought process should be. Right now, I
just try to come up with an algorithm by following the book and
improvising. I'll then try to test my code by throwing different things
at it (for instance, different types of input in different forms) and
see if it breaks, I then try to figure out why it broke and then patch
in the fix. My time is somewhat limited and I cannot at this time take
a C class at a junior college, but I was wondering if this is just part
of learning? Do I just need to continue reading various texts,
practicing, and studying code and I will acquire error checking and
more robust code over time? Any suggestions, hints, words of advice,
would be greatly appreciated. Thanks very much.
Here is my program by the way:
#include <stdio.h>
//program that counts the number of words and total chars
// but without whitespace, and newlines
// tracks going in/out of words for purpose of counting
#define IN 1 //inside a word
#define OUT 1 //outside a word
I don't think you want both to be 1.
main ()
{
int c; //var that holds char read from stdin
int state; //flag for in/out of words
int nc; //counts chars per word
int tnc; //counts total chars minus any whitespace, and newlines
int nw; //total number of words
//initialize variables
c = 0;
low = 0;
state = 0;
nc = 0;
tnc = 0;
nw = 0;
//read char at a time until end of file (ctrl-d)
It's only a comment but remove the parenthetical phrase. ctrl-d is
unix specific (other systems hare different conventions) and it is not
equivalent to end of file (but merely a method for signaling end of
file from a certain input device). If you had redirected your input
to a file using the "<" shell convention, ctrl-d would have no special
meaning.
while ( (c = getchar()) != EOF ) {
if ( c == '\n' || c == '\t' || c == ' ') {
You might find the isspace function useful here.
state = OUT;
You never make use of the value in state. See next comment.
if (nc > 0) ++nw;
What happens if the first two words are separated by three spaces?
(Hint: you should only increment nw when state is set to IN.)
nc = 0;
} else {
state = IN; //if you are not OUT then you are IN
You should avoid // comments on usenet. A compiler invoked in C90
mode may not accept them. Equally importantly, if they wrap as a
result of message line length, they produce syntax errors. Both
problems have the effect of reducing the number of people who can (or
are willing) to help solve your problem.
++nc;
++tnc;
}
}//while
printf("Number of words %d .. Number of non-whitespace chars %d \n",
nw, tnc);
}//main
Remove del for email
.
- References:
- good algorithms come with practice and reading good code/books?
- From: vlsidesign
- good algorithms come with practice and reading good code/books?
- Prev by Date: Re: MNC Hyderabad is Looking for C++ Developer...
- Next by Date: Re: MNC Hyderabad is Looking for C++ Developer...
- Previous by thread: Re: good algorithms come with practice and reading good code/books?
- Next by thread: Re: good algorithms come with practice and reading good code/books?
- Index(es):
Relevant Pages
|