C Programming Language Example Arrays

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

+7
c arrays
source share
2 answers

Output Analysis:

 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 
  • c -1 : you ended your read cycle with c equal to EOF, which is -1 on your platform.
  • i 10 : the for (i = 0; i < 10; ++i) loop for (i = 0; i < 10; ++i) broke when i < 10 no longer true. The last time it was touched was the final increment of ++i , which caused i be 10 and has not been touched since.
  • nwhite 4 : There are four spaces in the character sequence you entered without a new line (because you excluded them): abc def ghi jkl 123
  • nother 13 : A new line is considered a β€œdifferent” char for your modification, numbers are not. Therefore, abcdefghijkl\n is the thirteen values ​​that contributed to this accumulation.

The last element remains:

 Digits are =0 1 1 1 0 0 0 0 0 0 

When you go through the input loop, when the char digit is encountered ( '0'..'9' ), its ASCII value (or, if you work on iSeries or zSeries, its EBCDIC value), less char '0' value creates zero-based index into an array of 10 integers. This works because the standard requires that each of these code points for digits is consistent and continuous, and all platforms (including EBCDIC) honor this. The counter at this index in the array increases with each meeting of the corresponding char digit. Upon completion, the resulting contents of the array are dumped to stdout. You had one digit from '1' , '2' and '3' . The first slot in your array is for '0' , the next for '1' , etc. 0 1 1 1 ... represents the number of zeros, ones, two, three, etc.

For more information on ASCII values, see this table . If so, to see the differences with EBCDIC, see this table , and note that in both, although different real numerical values, the complete sequence '0'..'9' is continuous.

Hope this helps. Good question, by the way. Especially for your first post.

+2
source share

I am reading this book right now and here, as I understand it.

The result you get is correct:

c = -1 , because the output of the while loop when (c = getchar()) reaches EOF (knowing that EOF is -1), and remember that you save getchar () in c before comparing with EOF since the brackets have more high priority. Therefore, the condition: c = EOF, then it compares getchar () with EOF and exits the while loop

i = 10 because you are executing a loop using a for loop, and when i reaches 10, it should stop. So the last store of values ​​in i is 10.

 0 1 1 1 0 0 0 0 0 0 

means you have

 0 Zero 1 ONE 1 TWO 1 THREE 0 FOUR etc... 

PS: This is a great book!

+1
source share

All Articles