Array: Difference between revisions
Content added Content deleted
(not a task; don't use {{header}}) |
m (→C: lang tag / - nbsp) |
||
Line 37: | Line 37: | ||
We wish to open a text file and compute letter frequency |
We wish to open a text file and compute letter frequency |
||
<lang c> FILE *any_text; |
|||
/* declare array */ |
/* declare array */ |
||
int frequency[26]; |
int frequency[26]; |
||
/* declare a computed index */ |
/* declare a computed index */ |
||
int ch; |
int ch; |
||
|
|||
any_text = fopen ("a_text_file.txt", "rt"); |
any_text = fopen ("a_text_file.txt", "rt"); |
||
|
|||
/* init the freq table: */ |
/* init the freq table: */ |
||
for (ch = 0; ch < 26; ch++) |
for (ch = 0; ch < 26; ch++) |
||
frequency[ch] = 0; |
frequency[ch] = 0; |
||
|
|||
ch = fgetc(any_text); |
ch = fgetc(any_text); |
||
while (!feof(any_text)) { |
while (!feof(any_text)) { |
||
Line 55: | Line 55: | ||
frequency[ch-'A'] += 1; |
frequency[ch-'A'] += 1; |
||
ch = fgetc(any_text); |
ch = fgetc(any_text); |
||
} |
}</lang> |
||
===[[J]]=== |
===[[J]]=== |