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

Slides:



Advertisements
Similar presentations
For loops For loops are controlled by a counter variable. for( c =init_value;c
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Some loop programs 1: Read in 10 integers and output their sum
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Chapter 5: Control Structures II (Repetition)
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Control Structures RepetitionorIterationorLooping Part I.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
PGT C Programming1 Week 4 – Repetition Structures / Loops.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Modulus Operator #include main() { int a, b, q, r; printf(" a b a/b a%b\n"); while (scanf("%d%d", &a, &b) != EOF) { q = a / b; /* quotient */ r = a % b;
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
CSE 220 – C Programming Loops.
Chapter 6: Loops.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Lecture 13 & 14.
Iteration statement while do-while
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Looping.
CS1100 Computational Engineering
- Additional C Statements
A First Book of ANSI C Fourth Edition
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Loops in C.
Chapter 2.1 Repetition.
CMPT 102 Introduction to Scientific Computer Programming
Computer Programming Techniques Semester 1, 1998
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
Dale Roberts, Lecturer IUPUI
ECE 103 Engineering Programming Chapter 18 Iteration
Presentation transcript:

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

Expected Results Sample input (Enter and Control-D) (or a.out < input.dat) The output should be 3 data items read maximum value read = minimum value read = sum of all values read = mean = variance = standard deviation =

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.

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

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.

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

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.

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.

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

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.

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.

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); }

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.