Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

Similar documents
CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

C Programming for Engineers Structured Program

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Chapter 3 Structured Program Development

CS110D: PROGRAMMING LANGUAGE I

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Structured Program Development

Computer Programming I - Unit 5 Lecture page 1 of 14

Controls Structure for Repetition

Structured Program Development in C

Introduction. C provides two styles of flow control:

Chapter 3 Structured Program Development in C Part II

Repetition Algorithms

Lab 2: Structured Program Development in C

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Loops / Repetition Statements

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #28. Functions: Examples 2

Chapter 4: Control structures. Repetition

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Repetition and Loop Statements Chapter 5

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Chapter 4: Control structures

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

In this chapter you will learn:

A Look Back at Arithmetic Operators: the Increment and Decrement

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

LAB 5: REPETITION STRUCTURE(LOOP)

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

LAB 5: REPETITION STRUCTURE(LOOP)

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Lecture Transcript While and Do While Statements in C++

CS1100 Introduction to Programming

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Stepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017

Condition Controlled Loops. Introduction to Programming - Python

CMSC 104 -Lecture 9 John Y. Park, adapted by C Grasso

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Control Structures II. Repetition (Loops)

Algorithms and Problem Solving

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Ch.5. Loops. (a.k.a. repetition or iteration)

Chapter 2 - Control Structures

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

Java Coding 3. Over & over again!

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Control Structure: Loop

Add Subtract Multiply Divide

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

CS111: PROGRAMMING LANGUAGE II

Problem Solving With Loops

Fundamentals of Programming Session 7

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Repetition, Looping CS101

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Fundamentals of Programming. Lecture 6: Structured Development (part one)

REPETITION CONTROL STRUCTURE LOGO

Standard File Pointers

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

PROBLEM SOLVING WITH LOOPS. Chapter 7

Loops and Files. of do-while loop

Information Science 1

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

Increment and the While. Class 15

Final Examination Semester 3 / Year 2010

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Special Section: Building Your Own Compiler

AP Programming - Chapter 6 Lecture

Repetition, Looping. While Loop

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Why Is Repetition Needed?

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

CS112 Lecture: Repetition Statements

CS 101, Spring 2016 March 22nd Exam 2

Chapter Two: Program Design Process and Logic

(Python) Chapter 3: Repetition

The while Loop 4/6/16 4

Control Statements: Part 1

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG

cs1114 REVIEW of details test closed laptop period

Loops / Repetition Statements

Programming Lecture 4

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Information Science 1

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Transcription:

1 Programming in C

Looping Subtasks We will examine some basic algorithms that use the while and if constructs. These subtasks include Reading unknown quantity of data Counting things Accumulating (summing) totals Searching for specific values Finding extreme values 2

Looping Subtasks Examples will be based upon common models: Priming Read or Input Count Initialize program state Read the first value (priming read) While (data exists) update program state as needed read next value(s) Output final state The type of state that must be maintained by the program depends on the nature of the problem and can include: indicator (true/false) variables counter variables sum variables previous input value variables Initialize program state While (input count OK) update program state as needed Output final state 3

Counter-Controlled Repetition Number of items is known before loop Suppose the problem becomes: Develop a class-averaging program that will process an arbitrary number of grade scores each time the program is run. 4

Sentinel-Controlled Repetition One way to handle an arbitrary number of input values is to have the user enter a special value to indicate the end of input. Such a value is a sentinel value. Indicates end of valid input Loop ends when sentinel value is read Must choose a sentinel value that cannot be confused with a regular input value. 25 43 67 96 12 58 44-1 5

Sentinel-Controlled Priming Read 6 For sentinel-controlled loops 1. Read before the loop (priming read) 2. Test input to make sure it is not the sentinel value 3. Process 4. Read again at the bottom of the loop Use the following model: read before entering the loop while (value_read!= SENTINEL) { // process read at bottom of loop (before entering loop again) }

Sentinel-Controlled Loop using Priming Read 25 43 67 96 12 58 44-1 7

Sentinel-Controlled Loop using Input Count 25 43 67 96 12 58 44-1 8

Example of sentinel-controlled loop 25 43 67 96 12 58 44 99-1 9

Processing an arbitrary number of pairs Sometimes it is not possible to find a sentinel value We can use End-of-input controlled loops Uses return from scanf Can be fooled by invalid data End-of-file controlled loops Uses function feof 10

End of Data Hardware & Software End-Of-File Keyboard Ctrl-d (Does not work on Mac!) 25 43 67 96 12 58 44 99 Ctrl-d The End Is Here! 11

Redirection Redirection: Read / Write to actual file stdin: cmd < input-file Ex:./a.out < nums.txt stdout: cmd > output-file Ex:./a.out > report.txt stdout (append): cmd >> output-file Ex:./a.out >> report.txt Both: cmd < input-file > output-file Ex:./a.out < nums.txt > report.txt Leave out prompts when designing for redirection 12

Example: End-of-input controlled loop using items read & priming read 25 43 67 96 12 58 44 13

Example: End-of-input controlled loop using just items read 25 43 67 96 12 58 44 14

Example: End-of-input controlled loop using number of items read 25 43 67 96 12 58 44 99 15

Detecting End-of-File Function: feof Syntax: feof(file-pointer) Returns true or false Standard input: feof(stdin) Use in a while loop - while (!feof(stdin)) 16

Example: End-of-file controlled loop 25 43 67 96 12 58 44 End of File 17

Example: end-of-file controlled loop 25 43 67 96 12 58 44 99 End of File 18

Looping Subtask: Counting Example: Find the number of scores in a file Here the program state that must be maintained is a counter that maintains the number of scores that have been read so far. Steps Declare an int variable for the count Initialize the count to zero Increment the count in the body of the loop 19

20 Looping Subtask: Counting

21 Looping Subtask: Counting

22 Looping Subtask: Counting

23 Looping Subtask: Counting

Counting Example What if we want to print the number of passing scores (scores >= 70)? We need a mechanism that allows us to count only if the score is greater than or equal to 70 Use if stmt 24

25 Looping Subtask: Counting

Counting Example What if we want to print the number of passing scores (scores >= 70) and the number of failing scores? Use if -else 26

27 Looping Subtask: Counting

Looping Subtask: Accumulation (Summing) The state that must be maintained is the sum of all values that have been seen so far. Declare a variable to hold the sum (accumulator) Initialize the sum to zero In the body of the loop, add the new value to the sum 28

29 Accumulating Example

Counting & Accumulating Example Problem A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. Hint: Requirements for an average Count of number of items Sum of the items 30

Counting & Accumulating Example Pseudocode: Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average 31

Looping Subtasks: Searching Need a variable to indicate whether or not the program has encountered the target value, call it found Initialize found to 0 (false) Each time through the loop, check to see if the current value equals the target value If so, assign 1 to found 32

Searching Exercise Write a C program that 1. Reads a target score at the beginning of the file 2. Reads a set of scores and determines if the target score is in the set of scores 3. If found prints Target ## was found otherwise prints Target ## was not found 33

34 Looping Subtasks: Searching

Searching Improvement Stop searching if target has been found 35

Looping Subtasks: Finding Extremes Finding Extreme Values (e.g. maximum, minimum) Need a variable (such as maxvalue) to remember the most extreme value encountered so far 25 43 67 96 12 58 44 96 is the max 12 is the min 36

Looping Subtasks: Finding Extremes Finding Extreme Values (e.g. maximum, minimum) Initialize the maxvalue (minvalue) to some value maxvalue: Lower value than any data minvalue: Higher value than any data Or for both: The first data value For each data item Compare the current value to maxvalue (or minvalue) If the current value is > maxvalue (< minvalue), replace maxvalue (minvalue) with the current value. 37

Extremes Exercise Write a C program that 1. Reads a set of scores from a file 2. Determines and prints the maximum score 38

39 Looping Subtasks: Finding Extremes

Programming in C T H E E N D 40