HistogramO'WordLengths

The histogram exercise done in class 2012.12.6 code format="c" $ cat h2.c
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) define OCElementType char *
 * 5) include "OrderedCollection.c"

char *words[100000];   // This pair is a de-facto structure int wordCount=0;         // An OrderedCollection would be the data structure of choice

printWord(int ix) { printf("word[%i]=\"%s\"\n", ix, words[ix]); }

int findAny(char seek, int lix, int wordLen) { // answer the index of the first one found, -1 if none int ix; for(ix=0; ix<wordCount; ix++) { if(strlen(words[ix]) == wordLen) { // printf("ix=%i len=%i lix=%i seek=%c word=%s.\n",ix, wordLen, lix, seek, words[ix]); if ((words[ix])[lix] == seek) return(ix); }   }    return(-1); }

struct OrderedCollection *findAllSuchThat(char seek, int lix, int wordLen) { int ix, cnt=1; struct OrderedCollection *rslt; rslt = newOC; for(ix=0; ix<wordCount; ix++) { if(strlen(words[ix]) == wordLen) { if ((words[ix])[lix] == seek) { //               printf("(%i/%i=%c) %4i:",lix, wordLen, seek, cnt++); //               printWord(ix); addLast(rslt, words[ix]); }       }    }    return(rslt); }

printAllSuchThat(char seek, int lix, int wordLen) { // use when at least one has been found int ix, cnt=1; for(ix=0; ix 0) { if (findAny(ch,lix2,wordLen2) > 0) { fnd1 = findAllSuchThat(ch,lix1,wordLen1); fnd2 = findAllSuchThat(ch,lix2,wordLen2); sz1 = sizeOC(fnd1); sz2 = sizeOC(fnd2); total += (combo = sz1 * sz2); printf("%c == %i/%i (%i) == %i/%i (%i)\t\t#=%i total=%i \n",                      ch, lix1, wordLen1, sz1, lix2, wordLen2, sz2, combo, total); free(fnd1); free(fnd2); }       }    }    return(total); }

main{ FILE *inFile; char bufr[30]; int sz, fix, ix; struct OrderedCollection *found; int wordLengths[30] ={0};

inFile=fopen("/usr/share/dict/american-english","r"); printf("My file = %i\n",inFile);

while (fgets(bufr,30,inFile)!=0) { if (!isupper (bufr[0]) ) { bufr[(sz = strlen(bufr)) -1] = '\0'; words[wordCount] = malloc(sz); strcpy(words[wordCount], bufr); wordCount++; }   }    printf("Read %i words\n", wordCount); //   printWord(0); //   printWord(1); //   printWord(500); printWord(wordCount-1);

//   fix = findAny('b', 3, 5); //   printf("fix=%i ", fix); //   if (fix > 0) printWord(fix); // //     fix = findAny('x', 2, 5); //     printf("fix=%i ", fix); //     if (fix > 0) printAllSuchThat('x', 2, 5); // //     found = findAllSuchThat('x', 2, 5); //     printf("found %i solutions\n", sizeOC(found)); countCrosses(1,11, 9,13); for(ix=0; ix<wordCount; ix++) { wordLengths[strlen(words[ix])]++; }   for(ix=0; ix<30; ix++) { printf("%2i %i\n", ix, wordLengths[ix]); } }

code And the end of the output: code 0 1 1  26 2  68 3  588 4  2389 5  4999 6  8504 7  11596 8  12678 9  11816 10  9859 11  7293 12  4884 13  2875 14  1481 15  804 16  343 17  163 18  63 19  28 20  7 21  3 22  4 23  1 24  0 25  0 26  0 27  0 28  0 29  0 code Exercise for the student: make a complete list of letters which appear in exactly one word of a given length at a given position.