영어로 읽는 코딩

07 [C] 배열(array)

#include <stdio.h>

/*count digits, white space, others*/

int 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);

}

/*The C Programming Language (second edition, Prentice Hall)
by Brian W. Kernighan and Dennis M. RitchieS*/

 

* 배열 설명

The declaration

int ndigit[10];

declares ndigit to be an array of 10 integers. Array subscripts always start at zero in C, so the elements are ndigit [ 0 ] , ndigit [ 1 ], ... , ndigit [ 9 ]. This is reflected in the for loops that initialize and print the array. A subscript can be any integer expression, which includes integer variables like i, and integer constants.

 

* 문자와 숫자의 아스키 코드값

This particular program relies on the properties of the character representation of the digits. For example, the test

if (c >= ’ 0 ' && c <= ’ 9 ’ ) …

determines whether the character in C is a digit. If it is, the numeric value of that digit is

c – ‘ 0 ’

This works only if ’ 0 ’, ’ 1 ’ , …, ’ 9 ’ have consecutive increasing values. Fortunately, this is true for all character sets.

By definition, chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions. This is natural and convenient; for example, c - ' 0 ' is an integer expression with a value between 0 and 9 corresponding to the character ’ 0 ’ to ’ 9 ’ stored in C, and is thus a valid subscript for the array ndigit.

 

* 코드 설명

The decision as to whether a character is a digit, white space, or something else is made with the sequence

if (c >= ‘ 0 ’ && c <= ' 9 ’)

++ndigit[c- ’ 0 ’ ] ;

   else if (c == ’ ’ || c == ’ \n’ || c == ’ \t ’ )

++nwhite;

   else

++nother;

 

* 조건문

The Pattern

    if ( condition 1 )

statement1

    else if ( condition)

statement2

    else

         statement3

occurs frequently in programs as a way to express a multi-way decision. The conditions are evaluated in order from the top until some condition is satisfied; at that point the corresponding statement part is executed, and the entire construction is finished. (Any statement can be several statements enclosed in braces.) If none of the conditions is satisfied, the statement after the final else is executed if it is present. If the final else and statement are omitted, as in the word count program, no action takes place. There can be any number of

else if (condition)

statement

groups between the initial if and the final else.

As a matter of style, it is advisable to format this construction as we have shown; if each if were indented past the previous else, a long sequence of decisions would march off the right side of the page.

 

 

[The C Programming Language (second edition, Prentice Hall) by Brian W. Kernighan and Dennis M. RitchieS]

 

댓글

댓글 본문