Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

CS107: Introduction to Computer Science Lecture 2 Jan 29th.
Bernstein’s Conditions. Techniques to Exploit Parallelism in Sequential Programming Hierarchy of levels of parallelism: Procedure or Methods Statements.
PSEUDOCODE & FLOW CHART
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
 Control structures  Algorithm & flowchart  If statements  While statements.
Computer Science 101 Overview of Algorithms. Example: Make Pancakes Prepare batter Beat 2 eggs Add 1 tablespoon of brown sugar Add 1 cup of milk Add 2.
ITEC113 Algorithms and Programming Techniques
Chapter 2 The Algorithmic Foundations of Computer Science
Chapter 2: Design of Algorithms
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Pseudocode.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, C++ Version, Fourth Edition.
BBS Yapısal Programlama (Structured Programming)1 From problem to program In “real world”… Problem in Natural language Top Down Design in pseudo-code.
CHAPTER 4: ALGORITHM 4.1 Introduction stages identified in the program development process: 1. Problem analysis and specification 2. Data organization.
Pseudocode.
Review Algorithm Analysis Problem Solving Space Complexity
Adapted from slides by Marie desJardins
High-Level Programming Languages: C++
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
An ordered sequence of unambiguous and well-defined instructions that performs some task and halts in finite time Let's examine the four parts of this.
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
INTRODUCTION TO ALGORITHMS PROGRAMMING. Objectives Give a definition of the term algorithm Describe the various parts of the pseudocode algorithm or algorithm.
Chapter 2 - Algorithms and Design
CSEB114: Principle of programming
By the end of this session you should be able to...
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Flowcharts.
Writing Program Code in BASIC Write a program to prompt for and accept values into TWO variables, numx and numy. The program should square the value stored.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
ITEC113 Algorithms and Programming Techniques
Even more problems.. Mean (average) I need a program that calculates the average of student test scores. I need a program that calculates the average.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
CSCI-100 Introduction to Computing
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Invitation to Computer Science 5 th Edition Chapter 2 The Algorithmic Foundations of Computer Science.
Problem-solving with Computers. 2Outline  Computer System  5 Steps for producing a computer program  Structured program and programming  3 types of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Algorithms and Pseudocode
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
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.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
Design Document Sample Given two concentrated circles with different radii, calculate the area which falls inside the big circle but outside the small.
Starter What does the following code do?
CNG 140 C Programming (Lecture set 3)
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
CHAPTER 5A Loop Structure
CS1371 Introduction to Computing for Engineers
Introduction To Flowcharting
Introduction to Computer Programming
Lecture 2: Introduction to Algorithms
COMS W1004 Introduction to Computer Science and Programming in Java
Introduction to pseudocode
1) C program development 2) Selection structure
Algorithm Discovery and Design
Algorithm Discovery and Design
Introduction to Algorithms and Programming
Flowcharts and Pseudo Code
Programming Concepts and Database
WJEC GCSE Computer Science
Presentation transcript:

Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written) * conditional (if) * iterative (while)

How to Represent an Algorithm An algorithm could be represented with natural language formal programming language pseudocode

Developing an Algorithm for Area and Circumference We need to: get the radius from the user compute and print area compute and print circumference A sequential algorithm will do the job.

How to Write the Algorithm in Pseudocode Decide on names for the items in the problem and use them consistently. e.g.: –area –circumference Use the following primitive operations: –get a value (e.g. get radius) –print a value or message (e.g. print area) –set the value of an item (e.g. set radius to the value input) –arithmetic operations (+ - / *), square root

Pseudocode for the Area and Circumference Algorithm print “Enter the value for radius” get radius // get the radius from the user set area to (3.14 * radius * radius) print area set circumference to (2 * 3.14 * radius) print Note: You also need to prompt the user to type in the value for the radius

An Algorithm with a Conditional Operation Give the user a choice of seeing the area or the circumference. Get the radius Ask if the user wants area or circumference Compute and print the requested measure

Additional Primitive Needed for a Conditional Operation Use the same primitives as before plus the following : if (some condition is true) { some action(s) should be done } else { some other action(s) should be done }

Pseudocode with a Condition print “Enter the value for radius” get radius print “Type A for area or C for circumference.” get response if (response = A) { set area to (314 * radius * radius) print area } else { set circumference to (2 * 3.14 * radius) print circumference }

Practice with Conditional Algorithms In class: write algorithms in pseudocode. Get two numbers and print the larger one.

Conditional Algorithms with More Than Two Choices Nested If statement: if first condition //do first thing else if second condition //do second thing else //do something else end if

An Algorithm with an Iterative Operation Compute area or circumference for as many circles as the user wants This can be done two ways: 1.Ask the user each time whether or not to repeat the computation for another circle. 2.Ask the user how many times to do it, then repeat the computation that many times

Additional Primitive Needed for an Iterative Operation while (some condition is true) { some action(s) }

Pseudocode with an Iteration Method 1: Ask if you should repeat set answer to C while (answer = C) {print “Enter a value for radius” get radius print “Type A for area or C for circumference.” get response if (response = A) // etc. – same code as before print “Type C to do another circle or Q to quit.” get answer }

Pseudocode with an Iteration Method 2: Ask how many times print “How many circles do you want? “ get number while (number > 0) {print “Enter a value for radius” get radius print “Type A for area or C for circumference.” get response if (response = A) // etc. – same code as before set number to (number – 1) }

Iteration Method 3: Check a value Check some changing value to decide whether or not to repeat Example: Read two numbers x and y. Repeatedly subtract y from x and print x as long as x is positive.

Pseudocode with an Iteration Method 3: Check a value get x, y while (x > 0) { set x to (x – y) print x }

Things to Note - Summary The steps in the algorithms are normally carried out one after the other in the given sequence. However, the conditional and Iterative operations (primitives) alter this normal flow.

Formulating algorithms Look at the givens (the input information – about the objects and their properties; give suitable names to them; Circle – object; radius - property) Examine what is/are to be produced (the output information – properties of objects; give suitable names for them as well) Remember the available primitives (actions) are: –accepting an input, –assigning the input value to something,

Formulating algorithms Remember the available primitives (continued): –Carrying out arithmetic operations (+ - * /; some functions like square-root etc.), –Checking a ‘condition’ and doing something if it is true and something else if it is not true, –Repeat a set of instructions as specified, (iterative) – Delivering the result (one or more output information - properties of items; give suitable names to them).

Formulating algorithms Produce (express) each output in terms of the givens (inputs) using a solution procedure you came up with based on your knowledge of how the output is related to the input. (Get to the unknowns using the givens.) Use the three constructs in the above process for producing output info in terms of input info. In addition to the input and output, use intermediary variables, where necessary, to help implement the solution procedure.