Presentation is loading. Please wait.

Presentation is loading. Please wait.

Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.

Similar presentations


Presentation on theme: "Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical."— Presentation transcript:

1 Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical summary of the data that includes the largest value input, the smallest value input, the sum of all values read, the mean (average) of all values input, the population variance, and the standard deviation. The mean is = (1/n)  x i The population variance measures the spread of values and is given by the formula (1/n)  x i 2 - 2 = - 2 where the summation is over the n values x 1, x 2, …, x n read. The standard deviation is the square root of the variance.

2 Expected Results Sample input 2.0 3.0 4.0 (Enter and Control-D) (or a.out < input.dat) The output should be 3 data items read maximum value read = 4.000000 minimum value read = 2.000000 sum of all values read = 9.000000 mean = 3.000000 variance = 0.666667 standard deviation = 0.816497

3 Solution (in words) Check if we reached the End-Of-File immediately. If so, print a message and exit. If not at the end of file, initialize the count to 1, and max, min, sum, sum of squares by the value just read. Set up a loop to read a value, and then –Count it. –If it is larger than any value seen so far, we record it as new high. –If it is smaller than any value seen so far, we record it as a new low. –We add to the sum. –We add the square to sum of squares After reading all values, we compute the mean, variance, and standard deviation and print the statistical summary.

4 C Implementation #include main() { float x, max, min, sum, sum2, mean, variance; int count; if( scanf("%f", &x) == EOF ) printf(" 0 data items read\n"); else { max = min = sum = x; count = 1; sum2 = x * x; while ( scanf("%f", &x) != EOF ) { ++count; if (x > max) max = x; if (x < min) min = x; sum += x; sum2 += x * x; } printf("%d data items read\n", count); printf("maximum value read = %f\n", max); printf("minimum value read = %f\n", min); printf("sum of all values read = %f\n", sum); mean = sum / count; printf("mean = %f\n", mean); variance = sum2/count - mean * mean; printf("variance = %f\n", variance); printf("standard deviation = %f\n", sqrt(variance)); } This is pg96.c

5 Discussion if(scanf("%f", &x) == EOF) action else... Is the same as n = scanf("%f", &x); if(n == EOF)... The value n will be the number of items read, or EOF if end-of-file is reached. The use in while(scanf()!=EOF){... is similar.

6 Control Flow while (expr) { action } –Loop until expr becomes false. do { action } while (expr); –Loop but check at the end. for (init; condition; update) { action } –More general loop. if ( ), or if ( ) action1 else action2. –Conditional execution. More on control flow break, continue, goto, switch

7 Break (out of loop) The break statement causes an immediate exit from the innermost while, do while, for, and switch statements. Example: for(i = 1, i <= 10; i++) { printf("%d\n", i); if (i == 3) break; printf("bottom of loop\n"); } printf("out of loop\n"); The output is 1 bottom of loop 2 bottom of loop 3 out of loop This example on page 114.

8 Are the integers in standard input sorted? #include main() { int i, /* int in standard input */ after_i, /* number follows i */ eof_flag; /* signal EOF */ scanf("%d", &i); while( (eof_flag = scanf("%d", &after_i) ) != EOF ) { if (i > after_i) /* order OK? */ break; /* if not, terminate */ else /* if OK, update i */ i = after_i; } if (eof_flag != EOF) printf("\nfile is not sorted\n"); else printf("\nfile is sorted\n"); } This is program on page 115.

9 Continue Continue terminates the current iteration and immediately starts next iteration of the loop. –More precisely, if we encounter a continue statement in a while loop, we immediately jump to the top of the loop and test the expression to determine whether to execute the body of the loop again. –In do while, we immediately jump to the bottom of the loop and test the expression to determine whether to execute the body of loop again. –In for loop, if we encounter a continue statement, the execution proceeds immediately to the update part (expr3).

10 Continue/Example for(i = 1; i <= 3; i++) { printf("%d\n", i); if (i == 2) continue; printf("bottom of " "loop\n"); } printf("out of loop"); the output is 1 bottom of loop 2 3 bottom of loop out of loop This is example on page 116.

11 Continue, another example #include main() { float x, sum; int count; sum = 0.0; count = 0; while(scanf("%f", &x) != EOF) { if (x <= 0.0) continue; /* skip */ sum += x; count++; } if (count > 0) printf("\naverage=%f\n", sum/count); else printf("\nno positive " "number read\n"); } This is program on page 117.

12 Exercise, do it now How the program is run, and what is printed? #include main() { int i, j, k, x, y, z; k = -3; x = 2; y = 100; z = 7; for(i = 0; i > k; --i) { j = !i; if (j) continue; else break; printf("%d\n", x/y); } if (y > z) printf("%d, %d, %c\n", x%y, z--, y); }

13 Reading/Home Working Read Chapter 4, page 114 to 117. Work on Problems –Section 4.1, page 117, exercise 1, 3, 5. Check your answers in the back of the textbook. Do not hand in.


Download ppt "Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical."

Similar presentations


Ads by Google