#include #include #include #include #include int main( int argc, char * argv[] ) { FILE * rawFile; FILE * outFile; int i=1, index=0; char * oneLine=NULL; int dropArr[10]; /* index from 0 to 13*/ double doubleVal=0.0; float dropRate=0.0; double dVar=0.0, average=0.0; if ( argc != 3 ) { fprintf(stderr, "usage: %s inputfile outputfile \n", argv[0] ); exit( EXIT_FAILURE ); } if( (rawFile = fopen( argv[1], "r" )) < 0 ) { fprintf(stderr, "Error opening %s \n", argv[1] ); exit( EXIT_FAILURE ); } if( (outFile = fopen( argv[2], "w" )) < 0 ) { fprintf(stderr, "Error opening %s \n", argv[2] ); exit( EXIT_FAILURE ); } /* initialize the dropArr*/ for( i=0; i<10; i++) dropArr[i] = 0; /* allocate memory for oneLine */ oneLine = malloc( 64 ); while ( (oneLine = fgets( oneLine, 64, rawFile )) != NULL ) { if ( strstr( oneLine, "." ) ) { sscanf( oneLine, "%f", &dropRate ); index=-1; continue; } /* get elapse time in the array */ index++; sscanf( oneLine, "%d", &dropArr[index]); printf("num is: %d\n", dropArr[index]);/* for debugging purpose */ if ( index == 9 ) { /* this is the last elapse data, put the arry in the file */ /* calculate the average time and the throughput */ /* BE SURE TO USE DOUBLE TYPE WHEN YOU DO CALCULATION :w*/ average=0.0; for( i=0; i<10; i++) { doubleVal = (double) dropArr[i]; average += doubleVal/10; fprintf( outFile, " %d", dropArr[i] ); } /* average dropArr[11]*/ fprintf( outFile, " %f", average ); printf( "average: %f\n", average ); /* average devication dropArr[12]*/ dVar =0.0; for( i=0; i<10; i++ ) { doubleVal = (double) (dropArr[i]-average); doubleVal = doubleVal*doubleVal; dVar += doubleVal/10; } dVar = sqrt( dVar ); fprintf( outFile, " %f\n", dVar ); printf( "Var: %f\n", dVar ); }/* end of index=10*/ }/* end of while */ return 0; }/* end of main */