getInput.c file
Modify the output stream of the program runProgram and add functions to compute statistics in the file statistics.c. Currently the program reads in a stream of integers from standard input. It assumes that there is 1 integer per line. The program starts reading when it encounters a non-positive number or character. YOU will need to add below statistics to statistics.c (and edit add prototypes to statistics.h), so that each statistics is computed in a separate function, also you will need to refine the output (in the function drivers() in the file getInput.c), so it looks exactly as depicted below" Input file (read from standard Input) 2 4 1 3 XXX Outputs exactly (line up the ":"): Sum: 10.00 Average: 2.50 Sum of Squares : 30.0 Sample Variance: 1.25 Minimum: 1 Maximum: 4 Before you implement - runProgram prints out the correct Average of 2.50 as a running average, but not in the proper output format. Below is the output of the program before starting. The content of input04.txt is listed by the cat command below: {csci-odin: ingrid:86} cat input04.txt 2 4 1 3 XXX {csci-odin: ingrid:84} runprogram Entered ---> 2 -> Running Average ---> 2.00 -> Entered ---> 4 -> Running Average ---> 3.00 -> Entered ---> 1 -> Running Average ---> 2.33 -> Entered ---> 3 -> Running Average ---> 2.50 -> End of input Only getInput.c and statistics.c and statistics.h need to modified. In getInput.c you only modify the function driver(). getInput.c statistics.h statistics.c 1 #include
4 #define MAX_LINE 80 // functions from statistics.c, prototype is in statistic.h #include "statistics.h" 12 10 /* 11 * getIntegerLine() returns a positive integer from a line of input 13 14 * Assumption: assumes proper input. 15 */ 16 int getIntegerLine() 17 { 18 char line[MAX_LINE); 19 int return_user_int=-1; 20 21 fgets( line, sizeof( line ), stdin); 22 while( sscanf( line, "%d", &return_user_int ) != 1 ) 23 { 24 // Not a positive integer indicates end of input 25 return -1; 26 } 27 return return_user_int; 28 29 30 31 /* 32 * prompts user for input. 33 */ 34 void promptList() 35 { 36 printf(" Assumes proper input " ); 37 printf( " Type a list of non-negative integers, below, one integer per line " ); 38 printf( Indicate the END of input with any letter (a-z): ->" ); 39} 40 41 42 43 void driver() 44 { 45 int anumber=-1; 46 47 promptlist(); 48 49 while( (anumber = getIntegerLine()) >= 0 ) 50 { 51 52 printf("Entered ---> %d -> ", anumber ); 53 54 printf("Running Average ---> %4.21f -> ", running_average( anumber ) ); 55 } 56 57 printf("End of input " ); 58 59] This is Statistics.c file Moba TextEditor File Edit Search View Format Encoding Syntax Special Tools statistics.h statistics.c getInput.c 1 2 3 double running_average int add_number ) 4{ 5 static double total_numbers = 0; static double total_sum = 0; 7 total_sum = total_sum + (double) add_number; total_numbers += 1; 6 8 9 10 11 return total_sum / total_numbers; 12} 13 This is Statistics.h file IVIUUD TEXLLUILUT File Edit Search View Format Encoding Syntax Special Tools statistics.h statistics.c getInput.c 1 \// only include declaration once, by defining prepprocessor 'dummy variable' 2 #ifndef STATISTICS_H 3 #define STATISTICS_H 4 5 6 extern double running_average int add_number ); 8 9 10 #endif 11 12 Modify the output stream of the program runProgram and add functions to compute statistics in the file statistics.c. Currently the program reads in a stream of integers from standard input. It assumes that there is 1 integer per line. The program starts reading when it encounters a non-positive number or character. YOU will need to add below statistics to statistics.c (and edit add prototypes to statistics.h), so that each statistics is computed in a separate function, also you will need to refine the output (in the function drivers() in the file getInput.c), so it looks exactly as depicted below" Input file (read from standard Input) 2 4 1 3 XXX Outputs exactly (line up the ":"): Sum: 10.00 Average: 2.50 Sum of Squares : 30.0 Sample Variance: 1.25 Minimum: 1 Maximum: 4 Before you implement - runProgram prints out the correct Average of 2.50 as a running average, but not in the proper output format. Below is the output of the program before starting. The content of input04.txt is listed by the cat command below: {csci-odin: ingrid:86} cat input04.txt 2 4 1 3 XXX {csci-odin: ingrid:84} runprogram Entered ---> 2 -> Running Average ---> 2.00 -> Entered ---> 4 -> Running Average ---> 3.00 -> Entered ---> 1 -> Running Average ---> 2.33 -> Entered ---> 3 -> Running Average ---> 2.50 -> End of input Only getInput.c and statistics.c and statistics.h need to modified. In getInput.c you only modify the function driver(). getInput.c statistics.h statistics.c 1 #include 4 #define MAX_LINE 80 // functions from statistics.c, prototype is in statistic.h #include "statistics.h" 12 10 /* 11 * getIntegerLine() returns a positive integer from a line of input 13 14 * Assumption: assumes proper input. 15 */ 16 int getIntegerLine() 17 { 18 char line[MAX_LINE); 19 int return_user_int=-1; 20 21 fgets( line, sizeof( line ), stdin); 22 while( sscanf( line, "%d", &return_user_int ) != 1 ) 23 { 24 // Not a positive integer indicates end of input 25 return -1; 26 } 27 return return_user_int; 28 29 30 31 /* 32 * prompts user for input. 33 */ 34 void promptList() 35 { 36 printf(" Assumes proper input " ); 37 printf( " Type a list of non-negative integers, below, one integer per line " ); 38 printf( Indicate the END of input with any letter (a-z): ->" ); 39} 40 41 42 43 void driver() 44 { 45 int anumber=-1; 46 47 promptlist(); 48 49 while( (anumber = getIntegerLine()) >= 0 ) 50 { 51 52 printf("Entered ---> %d -> ", anumber ); 53 54 printf("Running Average ---> %4.21f -> ", running_average( anumber ) ); 55 } 56 57 printf("End of input " ); 58 59] This is Statistics.c file Moba TextEditor File Edit Search View Format Encoding Syntax Special Tools statistics.h statistics.c getInput.c 1 2 3 double running_average int add_number ) 4{ 5 static double total_numbers = 0; static double total_sum = 0; 7 total_sum = total_sum + (double) add_number; total_numbers += 1; 6 8 9 10 11 return total_sum / total_numbers; 12} 13 This is Statistics.h file IVIUUD TEXLLUILUT File Edit Search View Format Encoding Syntax Special Tools statistics.h statistics.c getInput.c 1 \// only include declaration once, by defining prepprocessor 'dummy variable' 2 #ifndef STATISTICS_H 3 #define STATISTICS_H 4 5 6 extern double running_average int add_number ); 8 9 10 #endif 11 12