I'm working with the Classic K&R Book "The C Programming Language", Second Edition.
I have this problem with the exercise in page 24 about Arrays.
Exercise say (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 running the program the following output.
At this point the book is confusing because it does not say that the person typing ...
The output of this program on itself is digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345
Ok, Now I'll put my copied code from the book
This is my exercise copied and paste 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 anything like:
abc def ghi jkl 123
I obtain this output:
After Var c -1 Var i 10 Var nwhite 4 Var nother 13 Digits are =0 1 1 1 0 0 0 0 0 0
Resume:
My code only differs from the original in the last lines becose I use this printf to see the value of variables are taking.
And that I rename /|| c == '\n'/ because a dont want to count it like a nwhite.
The rest I think thats are equal and appears to work fine likes book example.
Question:
Why the values of the example they are telling me ?
My Question is that I do not understand this output means or that it related to the information I have entered:
Input: abc def ghi jkl 123
Output: Digits are =0 1 1 1 0 0 0 0 0 0
Final:
I would appreciate any explanation on this point that the book is not clear for my and not really understand that these values are showing.
Added 05/06/2014:
SOLUTION TO MY DOUBT.
Firts
First of all I want to thank everyone for their help with the issue (links of the book), I thought it was in the public domain. I'll take this into account for future post.
Many thanks to: WhozCraig and Amir for their invaluable input and thanks to them finally able to understand the exercise.
And of course, to make sure I understood the next step is to perform verification that attached to help future readers to clarify this post:
To verify that I understood I made the following test:
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 the 0, and now whe have 2(ones) 1(two) 2(threes) 1(four), I have intentionally omitted five to checking 0(fives is OK) and finally 1(six), and the next for 7,8 and 9 are empy.
Thats is !!!
a.out <file.c. IDK if Eclipse has any such functionality, but (assuming you're in Windows) you could try opening a command prompt and doingmyprogram.exe <myprogram.cif both of those files are in the directory.