Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.

Slides:



Advertisements
Similar presentations
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Advertisements

 Control structures  Algorithm & flowchart  If statements  While statements.
CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 Algorithm Discovery and Design Chapter 2 Topics: Representing Algorithms Algorithmic Problem.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Loops. COMP104 Loops / Slide 2 Shortcut Assignment * C++ has a set of operators for applying an operation to a variable and then storing the result back.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
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.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Programming Loops (continued).
C++ Iterative Constructs
REPETITION CONTROL STRUCTURE
while Repetition Structure
A mechanism for deciding whether an action should be taken
PROGRAM CONTROL STRUCTURE
Variables A piece of memory set aside to store data
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
COMS 261 Computer Science I
Intro to Programming Week # 4 Control Structure Lecture # 8
Conditional Construct
Algorithm Discovery and Design
3 Control Statements:.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 1
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
Presentation transcript:

Algorithms JPC and JWD © 2002 McGraw-Hill, Inc.

2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for solving a problem. Properties of an algorithm: - input: zero or more values - output: at least one value - definiteness: each step is precisely defined - finiteness: each step in the algorithm should be solved in specific period of time - effectiveness: each step is capable to solved e.g 0/3 can’t be solved

Algorithms(Cont.) We can specify the different between algorithm and program according to computing theory there is a different between them..in algorithm the previous five condition should be applied and it can be describe using natural language (pseudo code) Or(flowchart) Where in the programs all condition should be applied accept the forth one, not all program applied forth condition because the operating system will describe the program in programming language, it design to control the implementation of several jobs, when there is no specific job the operation system does not finish it jobs, it will continue and waiting for new job to be entered. 3

Pseudo code examples If student’s grade is greater than or equal to 60 Print “passed” Else Print “faild” Another example: N=0 For each person in the room Set n=n+1

C++ Iterative Constructs(LOOP) Three constructs while statement for statement do-while statement

While Syntax

While Semantics

Computing an Average int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average;

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; Suppose input contains: listSize 4

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i Suppose input contains: listSize 0

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum Suppose input contains: listSize 0 0

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum Suppose input contains: listSize 0 0

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize 0 0 1

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize 1 1 1

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize 1 1 5

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: listSize

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum Suppose input contains: listSize 3 10 average 2.5 4

Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum average Suppose input contains: listSize

The For Statement Syntax for (ForInit ; ForExpression; PostExpression) Action Example for (int i = 0; i < 3; i++) { cout << "i is " << i; }

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i 0

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i 0

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i 0

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i 0

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i 1

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i 1

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 1

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 1

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 2

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 2

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i is 2 i 2

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i is 2 i 2

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i is 2 i 3

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i is 2 i 3

Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i is 2 all done i 3

The Do-While Statement Syntax do Action while (Expression) Semantics Execute Action If Expression is true then execute Action again Repeat this process until Expression evaluates to false Action is either a single statement or a group of statements within braces Action true false Expression

Example: int i=10; do { cout << "i is " << i; i++; } while (i<20);