#include "header.h" int compare(const void * a, const void * b){ charPair x, y; x = *((charPair *)a); y = *((charPair *)b); return y.count - x.count; } int main(){ charPair cPair[256][256]; unsigned int i, j, first, second; //char fname[100]; //FILE *fp; time_t t1, t2; clock_t c1, c2; //initializing pairs for(i =0; i < 256; i++) { for(j =0; j < 256; j++) { cPair[i][j].char1 = i; cPair[i][j].char2 = j; cPair[i][j].count = 0; } } //printf("Enter the name of the file:"); //scanf("%s",fname); //fp=fopen(fname, "r");// Read in an existing file. time(&t1);// Start timer. c1 = clock(); if((first=getchar())!=EOF) { while((second=getchar())!=EOF){ cPair[first][second].count += 1; first= second; };// When computer gets a character, add one to its number. } time(&t2); //Stop timer.0 c2=clock(); printf ("\telapsed CPU time: %f\n", (float) (c2 - c1)/CLOCKS_PER_SEC); //Sorting Array to have high counts first qsort(cPair, 256*256, sizeof(charPair), compare); // for(i=0; i < NOut; i++){ printf("(%c,%c): %d\n", cPair[i/256][i%256].char1, cPair[i/256][i%256].char2, cPair[i/256][i%256].count); } /* for(i =0; i < 256; i++) { for(j =0; j < 256; j++) { if(cPair[i][j].count > 0) printf("(%c,%c): %d\n", cPair[i][j].char1, cPair[i][j].char2, cPair[i][j].count); } } */ printf("Use %d seconds.\n",(int)(t2-t1)); //fclose(fp); // Close the file. return 0; }