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

Similar documents
Review: Repetition Structure

Introduction to Programming

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

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

Chapter 3 Structured Program Development

Structured Program Development

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

Structured Program Development in C

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

CS 108 Computing Fundamentals. October/November Array Bootcamp

Repetition Structures

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

Chapter 5: Control Structures

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

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

2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park

Loops / Repetition Statements

Control Structure: Loop

Repetition Structures II

Welcome! COMP s1. Programming Fundamentals

Fundamentals of Programming Session 7

Loops / Repetition Statements

Introduction. C provides two styles of flow control:

CS1100 Introduction to Programming

Control Structure: Selection

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

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

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Introduction to C Programming

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

Comments. Comments: /* This is a comment */

CpSc 1111 Lab 4 Formatting and Flow Control

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

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

CMPT 102 Introduction to Scientific Computer Programming

Chapter 2 - Control Structures

CS110D: PROGRAMMING LANGUAGE I

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

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Computer Programming. Decision Making (2) Loops

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

A Look Back at Arithmetic Operators: the Increment and Decrement

Fundamentals of Programming Session 8

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

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

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

Introduction to Computing Lecture 10 Arrays (Part 1)

Arrays. CSE / ENGR 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures.

C Programming Unit I - Introduction. Learning Objectives. Objectives. MCA-103, Programming in C

Fundamental of Programming (C)

Fundamentals of Programming

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2013 Midterm I Key

CS112 Lecture: Repetition Statements

BRANCHING if-else statements

LAB 5: REPETITION STRUCTURE(LOOP)

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

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Welcome! COMP s1. Programming Fundamentals

CpSc 1111 Lab 5 Formatting and Flow Control

ROCK PAPER SCISSORS Rock Paper Scissors Lab Project Using C or C++

Chapter 5: Control Structures II (Repetition)

CS112 Lecture: Loops

Loops. CSCI 112: Programming in C

Introduction to Programming. Lecture 2: Introduction to C

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

Chapter 7: Javascript: Control Statements. Background and Terminology. Background and Terminology. Background and Terminology

Lecture 8. Daily Puzzle

Arrays. CSE 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures. Arrays

1 EEE1008 C Programming

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

Lab 2: Structured Program Development in C

Algorithms, Part 2 of 3. Topics Problem Solving Examples Pseudocode Control Structures

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2016 Midterm I

Repetition Algorithms

Chapter 3: Arrays and More C Functionality

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

EC312 Chapter 4: Arrays and Strings

Fundamentals of Programming. Lecture 3: Introduction to C Programming

DECISION CONTROL AND LOOPING STATEMENTS

LAB 5: REPETITION STRUCTURE(LOOP)

What we have learned so far

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

Conditions and Logical Expressions. C Programming

Array. Arrays. Declaring Arrays. Using Arrays

CS113: Lecture 4. Topics: Functions. Function Activation Records

In this chapter you will learn:

for Loop Lesson 1 Outline

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

Introduction to Computing Lecture 09: Functions (Part II)

do, while and for Constructs

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Decision Making and Loops

Arrays. Systems Programming Concepts

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

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

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

3 The L oop Control Structure

Transcription:

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

Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop 2

A repetition structureallows the programmer to specify that an action is to be repeated while some condition remains true. There are three repetition structures in C, the whileloop forloop do-while loop 3

while ( condition ) { statement(s) } The braces are not required if the loop body contains only a single statement. However, they are a good idea and are required by the 104 C Coding Standards. 4

while ( cookies > 0 ) { children = children + 1 ; cookies = cookies - 2 ; } 5

Always place braces around the body of a while loop. Advantages: Easier to read Will not forget to add the braces if you go back and add a second statement to the loop body Less likely to make a semantic error Indent the body of a while loop 3 to 4 spaces -- be consistent! 6

int children = 0; while ( children < 1 ) { printf( Enter a positive number.\n"); scanf("%d", &children); } printf("finally, got something positive\n"); 7

Problem: Write a program that calculates the average exam grade for a class of 10 students. What are the program inputs? the exam grades What are the program outputs? the average exam grade 8

Let s write this program together using pseudocode... What is the algorithm for finding the average exam grade for the class of 10? What values will the program need to keep track of while it is doing this calculation? 9

<total> = 0 <grade_counter> = 1 While ( <grade_counter> <= 10 ) Display Enter a grade: Read <grade> <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 End_while <average> = <total> / 10 Display Class average is:, <average> 10

1. #include <stdio.h> 2. int main ( ) { 3. int grade, average ; 4. int total = 0 ; 5. int counter = 1 ; 6. while ( counter <= 10 ) { 7. printf ( Enter a grade : ) ; 8. scanf ( %d, &grade) ; 9. total = total + grade ; 10. counter = counter + 1 ; 11. } 12. average = total / 10 ; 13. printf ( Class average is: %d\n, average) ; 14. return 0 ; 15. } 11

What would happen the next time we had an exam if one of the students had dropped? There wouldn t be 10 grades any more We would like it to work with any class size. A better way : Ask the user how many students are in the class. Use that number in the condition of the while loop and when computing the average. 12

<total> = 0 <grade_counter> = 1 Display Enter the number of students: Read <num_students> While (<grade_counter> <= 10) Display Enter a grade: Read <grade> <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 End_while <average> = <total> / 10 Display Class average is:, <average> 13

<total> = 0 <grade_counter> = 1 Display Enter the number of students: Read <num_students> While (<grade_counter> <= 10) Display Enter a grade: Read <grade> <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 End_while <average> = <total> / 10 Display Class average is:, <average> 14

<total> = 0 <grade_counter> = 1 Display Enter the number of students: Read <num_students> While (<grade_counter> <= <num_students>) Display Enter a grade: Read <grade> <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 End_while <average> = <total> / <num_students> Display Class average is:, <average> 15

1. #include <stdio.h> 2. intmain ( ) { 3. int numstudents, counter, grade, total, average ; 4. total = 0 ; 5. counter = 1 ; 6. printf( Enter the number of students: ) ; 7. scanf( %d, &numstudents) ; 8. while ( counter <= numstudents) { 9. printf( Enter a grade : ) ; 10. scanf( %d, &grade) ; 11. total = total + grade ; 12. counter = counter + 1 ; 13. } 14. average = total / numstudents; 15. printf( Class average is: %d\n, average) ; 16. return 0 ; 17. } 16

Why do we write programs? So the user can perform some task The more versatile the program, the more difficult it is to write. BUT it is more useable. The more complex the task, the more difficult it is to write. But that is often what a user needs. Always consider the user first. 17

We could let the user keep entering grades When he s done, he enters some special value that signals to the program that he s done. This special signal value is called a sentinel value. We have to make sure that the value we choose as the sentinel is an illegalvalue. For example, we can t use 0as the sentinel in our example because it is a legal value for an exam score. 18

When we use a sentinel value to control a while loop, we have to get the first value from the user before we encounter the loop so that it will be tested and the loop can be entered. This is known as a priming read. We have to give significant thought to the initialization of variables, the sentinel value, and getting into the loop. 19

<total> = 0 <grade_counter> = 1 Display Enter a grade: Read <grade> While ( <grade>!= -1 ) <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 Display Enter another grade: Read <grade> End_while <average> = <total> / <grade_counter> Display Class average is:, <average> 20

1. #include <stdio.h> 2. int main ( ) { 3. int counter, grade, total, average ; 4. total = 0 ; 5. counter = 1 ; 6. printf( Enter a grade: ) ; 7. scanf( %d, &grade) ; 8. while (grade!= -1) { 9. total = total + grade ; 10. counter = counter + 1 ; 11. printf( Enter another grade: ) ; 12. scanf( %d, &grade) ; 13. } 14. average = total / counter ; 15. printf ( Class average is: %d\n, average) ; 16. return 0 ; 17. } 21

1. #include <stdio.h> 2. 3. int main ( ) { 4. int counter ; /* counts number of grades entered */ 5. 6. int grade ; /* individual grade */ int total; /* total of all grades */ 7. int average ; /* average grade */ 8. 9. /* Initializations */ 10. total = 0 ; 11. counter = 1 ; 12. 13. /* Priming read to get initial grade from user */ 14. printf( Enter a grade: ) ; 15. scanf( %d, &grade) ; 22

16. /* Get grades until user enters -1. Compute grade total 17. and grade count. */ 18. while (grade!= -1) { 19. total = total + grade ; 20. counter = counter + 1 ; 21. printf( Enter another grade: ) ; 22. scanf( %d, &grade) ; 23. } 24. 25. /* Compute and display the average grade */ 26. average = total / counter ; 27. printf ( Class average is: %d\n, average) ; 28. 29. return 0 ; 30. } 23

1. #include <stdio.h> 2. int main ( ) { 3. int number ; 4. printf ( Enter a positive integer : ) ; 5. scanf ( %d, &number) ; 6. while ( number <= 0 ) { 7. printf ( \nthat s incorrect.try again.\n ) ; 8. printf ( Enter a positive integer: ) ; 9. scanf ( %d, &number) ; 10. } 11. printf ( You entered: %d\n, number) ; 12. return 0 ; 13. } 24