13
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 <x> = (1/n) x i The population variance measures the spread of values and is given by the formula (1/n) x i 2 - <x> 2 = <x 2 > - <x> 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.

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

Embed Size (px)

Citation preview

Page 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

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

<x> = (1/n) xi

The population variance measures the spread of values and is given by the formula

(1/n) xi2 - <x>2 = <x2> - <x>2

where the summation is over the n values x1, x2, …, xn read. The standard deviation is the square root of the variance.

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

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 readmaximum value read = 4.000000minimum value read = 2.000000sum of all values read = 9.000000mean = 3.000000variance = 0.666667standard deviation = 0.816497

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

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.

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

C Implementation#include <stdio.h>#include <math.h>

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

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

Discussion

if(scanf("%f", &x) == EOF) actionelse . . .

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.

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

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

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

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

1bottom of loop2bottom of loop3out of loop This example

on page 114.

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

Are the integers in standard input sorted?

#include <stdio.h>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.

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

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).

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

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

1bottom of loop23bottom of loopout of loop

This is example on page 116.

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

Continue, another example

#include <stdio.h>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.

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

Exercise, do it now

How the program is run, and what is printed?

#include <stdio.h>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);}

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

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.