Algorithm Design.

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
CS 111: Introduction to Programming Midterm Exam NAME _________________ UIN __________________ 10/30/08 1.Who is our hero? 2.Why is this person our hero?
 Control structures  Algorithm & flowchart  If statements  While statements.
CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Program Design and Development
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Fundamentals of Python: From First Programs Through Data Structures
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 5 Structured Program.
The University of Texas – Pan American
Fundamentals of Python: First Programs
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Fundamentals of C and C++ Programming Control Structures and Functions.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
CPS120: Introduction to Computer Science Operations Lecture 9.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Visual Basic Programming
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Controlling Program Flow with Decision Structures.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Chapter 7 JavaScript: Control Statements, Part 1
Chapter 4 – C Program Control
GC211Data Structure Lecture2 Sara Alhajjam.
Introduction to Computer Programming
JavaScript: Control Statements.
JavaScript: Control Statements I
How to develop a program?
Structured Program
3 Control Statements:.
` Structured Programming & Flowchart
Introduction to Problem Solving and Control Statements
Computer Science Core Concepts
EPSII 59:006 Spring 2004.
Chapter 4 - Program Control
Basic Concepts of Algorithm
Presentation transcript:

Algorithm Design

Review Polya’s Method -- How to Solve It Understand the problem Devise a plan  Carry out the plan Look back The plan you make to solve the problem is called an algorithm A computer program is an expression of an algorithm in a computer language Programming enables us to use the computer as a problem solving tool

Definition: algorithm Well-defined computational procedure that takes some value or set of values as input and produces some value or set of values as output that is, a sequence of computational steps that transform the input into the output Can also view an algorithm as a tool for solving a well-specified computational problem the problem statement specifies in general terms the desired input/output relationship the algorithm describes a specific computational procedure for achieving that relationship CLRS, Introduction to Algorithms

Definition: program General a series of steps to be carried out or goals to be accomplished for example, a program of study Computer science a sequence of instructions a computer can interpret and execute that tells the computer how to perform a specific task or directs its behavior

Stages of program development Problem analysis and specification Data organization and algorithm design Program coding Execution and testing Program maintenance

Problem specification and analysis description of the problem’s input what information is given and which items are important in solving the problem description of the problem’s output what information must be produced to solve the problem Analysis generalize specification to solve given problem and related problems of same kind divide complex problems into subproblems

Data organization Data organization representation of input, output, intermediate values intermediate values hold information derived from input or other intermediate values that we want to remember for later on assignment of names to values, which may assume different values or remain constant The names we assign to values are called variables A variable type describes the values it can take on such as integer (int) or boolean (boolean)

Assignment statements Variables are assigned values using assignment statements Assign the value of an expression to a variable: Variables that appear on the right side of an assignment statement must have previously defined values The value resulting from evaluation of the expression is assigned to the variable on the left side of the <variable> = <expression>

Examples The equals sign should be interpreted as “is assigned the value of ” or “is replaced by” Variables with previously assigned values can appear on both sides of the assignment statement x=15; y=30; z=x+y; a=80; b=90; average=(a+b)/2; pi=3.14159; sum=0; sum=sum+1;

Algorithm design and refinement Basic description of an algorithm get input values compute output values for the given input return output values Algorithm refinement adding problem specific details computation of output values Check correctness of algorithm after steps are designed sample data mathematical analysis

Control structures Determine flow of execution of a program’s instructions Sequential execution instructions follow one another in a logical progression Selective execution provides a choice depending upon whether a logical expression is true or false Repetitive execution the same sequence of instructions is to be repeated a number of times We can construct any algorithm using combinations of control structures

Sequential execution statement 1 statement 2  statement n

== , != , < , <= , > , >= Selective execution Allows program to take alternate logical paths Decisions are based on the value of a logical expression logical expressions evaluate to true or false Relational operators are used to make comparisons in a logical expression == , != , < , <= , > , >= Expression Value 5==5 true 5!=5 false 3<8 Expression Value 2>=8 false 7!=5 true 9==1

Selective execution: if Executes statements when the logical expression is true Multiple statements are enclosed in curly braces { } otherwise only the first statement following the if is executed If logical expression is false statements are not executed computer proceeds to next statement in the program if( logical-expression ) { … statements … } if( logical-expression ) statement

if selection expression true statements false

Selective execution: if-else Executes one set of statements when logical expression is true and different set of statements when expression is false Used to select between two cases Multiple statements are enclosed in curly braces if( logical-expression ) { … statements1 … } else { … statements2 … }

if-else selection statements2 false expression true statements1

Selective execution: if else-if else distinguish between three or more cases example: convert numerical grade to A-F If a logical expression is true, the remainder of the statements are bypassed good design - check likeliest cases first if(average  90) grade = A; else if(average  80) grade = B; else if(average  70) grade = C; else if(average  60) grade = D; else grade = F;

Repetitive execution: for-loop Repetition controlled by a counter Statements executed once for each value of a variable in a specified range start and end values are known Initial statement: assign start value of counter Test: logical expression comparing counter to end value Update statement: assign new value to counter for( initial-statement; test; update-statement ) { … statements … }

for-loop repetition initial statement update statements statement expression update statement true statements false

Example: for-loop If a=3, b=7, and x=10 prior to loop execution, what is the value of x when the loop terminates? k is the counter variable a, b, x must have assigned values k++ increments k by one  for(k=a; k<b; k++) { x=x+k; }

Repetitive execution: while-loops Repetition controlled by a logical expression statements executed while the logical expression is true, loop exits when logical expression is false some variable in the logical expression must change with each repetition otherwise, we loop forever! while( logical-expression ) { … statements … }

while loop repetition expression true statements false

Example: while-loops k=a; while(k<b) { x=x+k; k=k+1; } If a=3, b=7, and x=10 prior to loop execution, what is the value of x when the loop terminates?

For-loop or while-loop? When to use a for-loop: always for counting! you know how many times to execute the loop When to use a while-loop: number of repetitions needed is unknown for(k=a; k<b; k++) { x=x+k; } k=a; while(k<b) { x=x+k; k=k+1; a for loop can always be written as a while loop

Shampoo algorithm Q: Why did the computer scientist die in the shower? A: He followed the instructions on the shampoo bottle. Problem with shampoo algorithm: no terminating condition! Shampoo algorithm has what is called an infinite loop How to fix?

Detecting infinite loops Problem: compute sum of positive integers  n Assume n is an input value. Are the following while-loops correct or incorrect? Why? sum=0; while(n>0) sum=sum+n; sum=0; while(n>0) { sum=sum+n; n=n+1; } k=1; sum=0; while(sum<n) { sum=sum+k; k=k+1; }