I work with the classic K & R book "C Programming Language", second edition.
I have this problem with the exercise on page 24 about arrays.
Exercise (Copy and Paste from PDF):
#include <stdio.h> /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ++ndigit[c-'0']; else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digits ="); for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); }
In this case, the book says that after starting the program the following conclusion.
At this moment, the book is confusing because it does not say that a person is printing ...
The output of this program itself is numbers = 9 3 0 0 0 0 0 0 0 1, space = 123, other = 345
Ok now i will post my copied code from the book
This is my exercise copied and pasted from Eclipse:
#include <stdio.h> #include <stdlib.h> int main(void) { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) { ndigit[i] = 0; } while ((c = getchar()) != EOF) { if (c >= '0' && c <= '9') ++ndigit[c - '0']; else if (c == ' ' /*|| c == '\n'*/|| c == '\t') ++nwhite; else ++nother; } printf(" After "); printf(" Var c %d \n", c); printf(" Var i %d \n", i); printf(" Var nwhite %d \n", nwhite); printf(" Var nother %d \n", nother); printf(" Digits are = "); for (i = 0; i < 10; ++i) printf(" %d ", ndigit[i]); return EXIT_SUCCESS; }
When I run it and type something like:
abc def ghi jkl 123
I get this output:
After Var c -1 Var I 10 Var nwhite 4 Var nother 13 Digits = 0 1 1 1 0 0 0 0 0 0
Summary:
My code only differs from the original in the last lines because I use this printf to view the value of variables.
And what I renamed / || c == '\ n' / because you do not want to consider it as nwhite.
The rest, I think they are equal and seem to work perfectly, for example, an example book.
Question:
Why are the meanings of the example that they tell me?
My question is that I donβt understand this output tool or that it is related to the information I entered:
Entrance: abc def ghi jkl 123
Output: digits = 0 1 1 1 0 0 0 0 0 0
The final:
I would appreciate any explanation on this subject that the book is not clear to mine and I do not quite understand what these meanings show.
Added 05/06/2014:
DECISION TO PROPERTY.
Firts
First of all, I want to thank everyone for their help in this matter (links to the book), I thought it was in the public domain. I will take this into account for future publication.
Thank you very much: WhozCraig and Amir for their invaluable contribution and, thanks to them, were finally able to understand the exercise.
And, of course, to make sure that I understood that the next step is to perform a test attached to future readers to clarify this post:
To make sure I understood, I did the following testing:
In this new case, we introduce the following series:
ab cd ef gh 1234136 After Var c -1 Var i 10 Var nwhite 4 Var nother 9 Digits = 0 2 1 2 1 0 1 0 0 0
And indeed, 0, and now we have 2 (one) 1 (two) 2 (triples) 1 (four), I intentionally missed five to check 0 (fifth OK) and finally 1 (six), and the next for 7.8 and 9 - empy.
This!!!