/********************************************************** File: rmsd.c Author: Scot Cunningham Usage: rmsd [infile] This file calculates an Root Mean Square Differentiation (RMSD) between two identical sized vectors. The input is two vectors that have been pasted together. For example, to compare the vector {1,3,5,9} with {2,4,6,8}, the input would be the following: 1 2 3 4 5 6 7 8 A tab should separate each element. If a filename is not specified, input is obtained from stdin. Compile with -lm option to get math libraries **********************************************************/ #include #include #include int rmsd (FILE *fp) { char s[256]; char *token; char *search = " "; /* tab is the delimiter */ int i; int numvalues=0; int a,b; int diff; int sumdiff=0; /* This loop is the meat of the algorithm */ while (fgets(s,256,fp)) { token = strtok(s, search); a=atoi(token); token = strtok(NULL, search); b=atoi(token); diff = a - b; sumdiff += diff * diff; ++numvalues; } return sqrt(sumdiff/numvalues); } int main(int argc, char** argv) { float dist; FILE *fp; /* See if a filename is specified */ if (argc < 2){ fp = stdin; }else{ if ((fp = fopen(argv[1],"r")) == NULL) { fprintf(stderr, "Unable to open file %s\n",argv[1]); exit(1); } } /* Calculate the distance */ dist = rmsd(fp); if (argc > 0) fclose(fp); /* Output the distance to stdout */ printf("%d\n", (int)dist); return dist; }