C Programming for Engineers Structured Program

Similar documents
Chapter 3 Structured Program Development in C Part II

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

CS110D: PROGRAMMING LANGUAGE I

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

Repetition Structures

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

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

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

Chapter 3 Structured Program Development

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

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Repetition and Loop Statements Chapter 5

Structured Program Development

Information Science 1

Information Science 1

Programming for Engineers Iteration

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

Structured Program Development in C

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

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

ECE 122. Engineering Problem Solving with Java

Chapter 2 - Control Structures

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?

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

MATH ALGEBRA AND FUNCTIONS 5 Performance Objective Task Analysis Benchmarks/Assessment Students:

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

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

Chapter 2 - Control Structures

Programming for Engineers Arrays

Chapter 2 - Control Structures

Section 2 0: The Rectangular Coordinate System. The Coordinate System

Repetition Algorithms

C++ Programming: From Problem Analysis to Program Design, Third Edition

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

REPETITION CONTROL STRUCTURE LOGO

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

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

LAB 2 INTRODUCTION TO PROGRAMMING

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

Fundamentals of Programming Session 7

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

Loops (while and for)

Control Statements. Musa M. Ameen Computer Engineering Dept.

Chapter 4: Control structures. Repetition

Topic. Section 4.1 (3, 4)

CIS 3260 Intro to Programming in C#

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

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

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

Chapter 5: Loops and Files

Chapter 4: Control structures

Loops / Repetition Statements

Control Structures of C++ Programming (2)

Coding Tutorial. Derrick Hasterok. February 1, 2005

In this chapter you will learn:

JavaScript: Control Statements I Pearson Education, Inc. All rights reserved.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

CSCE 206: Structured Programming in C++

L13-Mon-3-Oct-2016-Sec-1-1-Dist-Midpt-HW Graph-HW12-Moodle-Q11, page 1 L13-Mon-3-Oct-2016-Sec-1-1-Dist-Midpt-HW Graph-HW12-Moodle-Q11

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

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

STUDENT LESSON A12 Iterations

Chapter 4 C Program Control

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

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

Repetition, Looping CS101

Control Structures II. Repetition (Loops)

Example 1: Give the coordinates of the points on the graph.

204111: Computer and Programming

Repetition Structures II

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

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

CS112 Lecture: Repetition Statements

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Review: Repetition Structure

Final Examination Semester 3 / Year 2010

x + 1 = 2 Write x as a fraction in the form a, where a and b are positive integers with no common factor other than 1. Pass on the value of b a.

Chapter 1. Linear Equations and Straight Lines. 2 of 71. Copyright 2014, 2010, 2007 Pearson Education, Inc.

Condition Controlled Loops. Introduction to Programming - Python

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

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

CS112 Lecture: Loops

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

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

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

Controls Structure for Repetition

LESSON 3 CONTROL STRUCTURES

Each point P in the xy-plane corresponds to an ordered pair (x, y) of real numbers called the coordinates of P.

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

Introduction to Programming

Looping. Arizona State University 1

Chapter 4 C Program Control

Jakarta International School 8 th Grade AG1

Programming for Engineers Introduction to C

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Why Is Repetition Needed?

Introduction. C provides two styles of flow control:

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Transcription:

C Programming for Engineers Structured Program ICEN 360 Spring 2017 Prof. Dola Saha 1

Switch Statement Ø Used to select one of several alternatives Ø useful when the selection is based on the value of a single variable or a simple expression Ø values may be of type int or char not double 2

switch Statement switch (controlling expression) { label set 1 statements1 break; label set 2 statements2 break;... label set n statementsn break; 3

switch Statement Example 4

Class Assignment Ø Write a program that takes letter grade A, B, C, D or E and prints the range of score as below. 5

Class Assignment Ø Write a program that takes the x y coordinates of a point in the Cartesian plane and prints a message telling either an axis on which the point lies or the quadrant in which it y is found. QI I QI x QIII QI V Ø Sample lines of output: (-1.0, -2.5) is in quadrant III (0.0, 4.8) is on the y-axis 6

Iteration Statement Ø Repeat a set of actions while some condition remains TRUE Ø Example: While there are more students in class raising their hand Answer a question and let him/her lower the hand While there are more items on my shopping list Purchase next item and cross it off my list Ø Usually, action statements modify variables that affect the condition Ø CAUTION: Check when the condition becomes FALSE Ø Can be single statement or multiple (block) Condition Action 7

While Statement in C while (condition) { } 8

While Statement in C while (condition) { } Ø Previously used Equation: ax 3 +7 Ø Code to compute x 3 times=1; product =1; while ( times <= 3 ) { product = x * product; times = times+1; } // end while 9

Formulating algorithm counter controlled iteration Ø Consider the following problem statement: 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. Ø class average =,-./0 23450-67 893/0298 10

Counter controlled iteration Ø Uses a variable called a counter to specify the number of times a set of statements should execute. Ø Counter-controlled iteration is often called definite iteration because the number of iterations is known before the loop begins executing. 11

Counter controlled iteration - pseudocode 12

Counter controlled iteration C code 13

Counter controlled iteration C code continued 14

Initialization phase Ø A total is a variable used to accumulate the sum of a series of values. Ø A counter is a variable used to count in this case, to count the number of grades entered. Ø An uninitialized variable contains a garbage value the value last stored in the memory location reserved for that variable. 15

Formulating algorithm Sentinel Controlled Iteration Ø Consider the following problem: Develop a class-average program that will process an arbitrary number of grades each time the program is run. Ø In this example, the program must process an arbitrary number of grades. 16

Sentinel Controlled Iteration Ø Use a special value called a sentinel value (also called a signal value, a dummy value, or a flag value) to indicate end of data entry. Ø Sentinel-controlled iteration is often called indefinite iteration because the number of iterations isn t known before the loop begins executing. Ø Sentinel value must be chosen so that it cannot be confused with an acceptable input value. 17

Sentinel controlled iteration - pseudocode 18

Sentinel controlled iteration C code 19

Sentinel controlled iteration C code This would cause an infinite loop if -1 is not input as the first grade. 20

Sentinel controlled iteration - Output 21

Nested Control Statement Ø One control statement within another Ø Consider the following problem statement: In a class of 10 students, get the result of the student from the user (1=pass, 2=fail) and find the number of students who failed and who passed. If more than 8 students passed, print a statement for bonus to the instructor. 22

Nested Control Statement Pseudocode 23

Nested Control Statement C code 24

Nested Control Statement C code 25

Nested Control Statement Output 1 26

Nested Control Statement Output 2 27