/* Normalize.c Reads a file of numbers in ASCII format and prints their normalized values to standard output. The name of the file should be given as a command line argument. */ #include void usage(); main(int argc,char **argv){ float value,maxvalue,minvalue; int i; FILE* infile; if (argc!=2) usage(); if(!(infile=fopen(argv[1],"r"))){ fprintf(stderr,"Error: Couldn't open input file %s\n",argv[1]); exit(1); } if(fscanf(infile,"%f",&value)!=1){ fprintf(stderr,"Error: file %s was empty\n",argv[1]); exit(1); } /* First, we'll scan the whole file to find the min and max values. */ maxvalue=minvalue=value; while(fscanf(infile,"%f",&value)==1){ maxvalue=(value>maxvalue)?value:maxvalue; minvalue=(value>minvalue)?value:minvalue; } fclose(infile); if(!(infile=fopen(argv[1],"r"))){ fprintf(stderr,"Error: Couldn't re-open input file %s\n",argv[1]); exit(1); } range=max-min; while(fscanf(infile,"%f",&value)==1){ printf("%f\n",fabs((value-min)/range)); } fclose(infile); }