Fundamentals of Computer Science. Sentinel Based Repetition

Similar documents
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Chapter 6. Section 6.4 Altering the Flow of Control. CS 50 Hathairat Rattanasook

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

Chapter 4 C Program Control

A Look Back at Arithmetic Operators: the Increment and Decrement

ECET 264 C Programming Language with Applications. C Program Control

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

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Computers Programming Course 7. Iulian Năstac

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

Subject: PIC Chapter 2.

Chapter 4 C Program Control

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

C Functions. 5.2 Program Modules in C

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

Loops / Repetition Statements

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

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Worksheet 4 Basic Input functions and Mathematical Operators

Dept. of CSE, IIT KGP

Introduction to Programming

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 5. Aykut Erdem, Erkut Erdem, Fuat Akal

usedmeter = currentmeter - previousmeter kwhcharge = KWH1 CHARGE * usedmeter

Control Structure: Loop

CS110D: PROGRAMMING LANGUAGE I

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

EECE.2160: ECE Application Programming Fall 2017

Q1: C input/output; operators / 46 Q2: Conditional statements / 34 Q3: While and do-while loops / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Programming in C. Session 4. Seema Sirpal Delhi University Computer Centre

Review for Test 1 (Chapter 1-5)

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

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

Chapter 4: Control structures. Repetition

بسم اهلل الرمحن الرحيم

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Computer Programming I - Unit 5 Lecture page 1 of 14

Repetition Structures

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

Loop structures and booleans

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Chapter 4: Control structures

Data types, variables, constants

Structures in C. C allows you to declare a struct. Once you do so, you can create variables of this type. Here is the general syntax:

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

COMP 208 Computers in Engineering

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

CompSci 125 Lecture 11

General Certificate of Education Advanced Subsidiary Examination June 2010

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

Fundamentals of Programming Session 7

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

Repetition and Loop Statements Chapter 5

Repetition Structures II

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CSCI 2132 Software Development. Lecture 8: Introduction to C

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Programming and Data Structures Mid-Semester - Solutions to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Chapter 13 Control Structures

Module 4: Decision-making and forming loops

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Information Science 1

do, while and for Constructs

Chapter 2. Introduction to C language. Together Towards A Green Environment

Problem Solving and 'C' Programming

Decision Making and Loops

LESSON 3 CONTROL STRUCTURES

printf("this program adds the value 10 to a given integer number.\n\n");

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

Chapter 13. Control Structures

Fundamentals of Computer Programming Using C

Syntax of for loop is as follows: for (inite; terme; updatee) { block of statements executed if terme is true;

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

SELECTION STATEMENTS:

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

Chapter 3 Structured Program Development

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

Chapter 5: Control Structures

CMPT 102 Introduction to Scientific Computer Programming

Name Section: M/W T/TH Number Definition Matching (8 Points)

Lecture 7 Tao Wang 1

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

C++ Final Exam 2017/2018

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

Chapter 6. Decision and Control Statements

Strings. Daily Puzzle

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Fundamentals of Programming Session 9

Slides adopted from T. Ferguson Spring 2016

C Programming for Engineers Structured Program

Technical Questions. Q 1) What are the key features in C programming language?

8. Characters and Arrays

AMCAT Automata Coding Sample Questions And Answers

Transcription:

Sentinel Based Repetition 59

syntax: The while Loop Statement while (expression) statement a while loop statement is (almost) identical to a for loop statement that has no initialization or increment expressions in theory, for loops and while loops are interchangeable in practice, each loop statement is used for a specific form of repetition 60

Repetition Control Structures two distinct forms of repetition definite repetition implies that the precise number of loop repetitions is known when the loop starts its execution indefinite repetition implies this number is not known sentinel based repetition is one kind of indefinite repetition a sentinel is a special value entered by the user during a loop repetition that indicates end of input data the body of a sentinel based loop is repeatedly executed until the sentinel is entered sentinel based repetition is usually coded with the while loop statement 61

Coding Sentinel Based Repetition priming the loop: obtain first input while (input!= sentinel) { process most recently obtained input obtain next input } the loop and a half construct : while (1) { obtain next input if (input == sentinel) break; process most recently obtained input } 62

Logical Operators Operation Operator or and && not! to determine if diceroll is 7 or 11: diceroll == 7, 11 (incorrect) diceroll == 7 diceroll == 11 (correct) to determine if height is between 11.9 and 12.1: 11.9 < height < 12.1 (incorrect) 11.9 < height && height < 12.1 (correct) 63

Textbook Reading the while loop statement is introduced in section 3.7 sentinel based repetition is covered in section 3.9 section 4.9 discusses the effect of a break statement inside of a loop (ignore the continue statement) logical operators are found in section 4.10 section 4.11 is concerned with the most common error made by C programmers section 4.12 summarizes control structures 64

Wheel of Fortune Program in the TV game show Wheel of Fortune, players try to solve a puzzle consisting of partially revealed text characters the winning player gets to play a bonus round in which the following letters are initially revealed: R, S, T, L, N, and E the program allows the user to enter the solution to the puzzle, and then displays the partially revealed puzzle a player would see 65

Wheel Program: Sample Output 66

Wheel Program: Analysis input: a single line of text control structure: sentinel based repetition the return key ( \n ) actsasthesentinel one character is scanned and processed in each loop repetition output: one character per loop repetition, depending on the input character letters R, S, T, L, N, ande (and spaces) are echoed otherwise, an asterisk (*) is printed 67

Wheel Program: Algorithm print directions repeat the following until the return key is entered scan in a single character test for loop termination print out a character 68

Wheel.c: First Half /* Wheel.c * ======= * This program accepts a single line of text input. * It echos the line revealing the letters * R, S, T, L, N, and E (and spaces), but * replaces all other characters by asterisks ( * ). */ #include <stdio.h> int main() { char ch; /* the most recently character scanned in */ /* print directions */ printf("enter the solution to the puzzle.\n" "The puzzle to be solved by the\n" "player will appear below it.\n\n"); 69

Wheel.c: Second Half while (1) { scanf("%c", &ch); /* input next character */ if (ch == \n ) break; /* test for loop termination */ } /* process character */ if (ch == R ch == S ch == T ch == L ch == N ch == E ch == ) printf("%c", ch); else printf("*"); } return 0; 70