Re: good algorithms come with practice and reading good code/books?



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
.



Relevant Pages

  • good algorithms come with practice and reading good code/books?
    ... I am a newbie and going through "The C programming language" by ... //program that counts the number of words and total chars ... int c; //var that holds char read from stdin ... int tnc; //counts total chars minus any whitespace, ...
    (comp.lang.c)
  • creating a testfile for catv
    ... control chars and non-ascii chars: ... int main ... void filecopy ... Am I correct to think that the first 32 chars are control chars? ...
    (comp.lang.c)
  • Re: Character Operators
    ... the chars are promoted to ints and so the difference between two chars is an int. ... The code you posted wouldn't be used except in converting characters to numerics. ... It has everything to do with the numerical base being used, and hexadecimal is the only commonly used numerical base that includes letters as digits. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Strange way to convert char to int (?)
    ... >> chars are automatically promoted to ints before doing arithmetic. ... >> This is a fast way of converting a single char digit to binary int. ... I think this is bad way to convert characters to the numbers they ... since this is game, the code should be very fast. ...
    (comp.lang.java.programmer)
  • Re: Insert a byte
    ... i have in binary array of chars ... i'm not very good as programmer (use the cpu like someone can use a ... programmers) write using the language i use should improve code generation ...
    (alt.lang.asm)