Adapted from slides by Marie desJardins

Slides:



Advertisements
Similar presentations
Understanding the Three Basic Structures
Advertisements

LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
 Control structures  Algorithm & flowchart  If statements  While statements.
ITEC113 Algorithms and Programming Techniques
CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 Algorithm Discovery and Design Chapter 2 Topics: Representing Algorithms Algorithmic Problem.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Chapter 2: Algorithm Discovery and Design
Program Design and Development
Computer Science 1620 Programming & Problem Solving.
Chapter 2: Algorithm Discovery and Design
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
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 2: Algorithm Discovery and Design
Review Algorithm Analysis Problem Solving Space Complexity
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)
Invitation to Computer Science, Java Version, Second Edition.
Algorithmic Problem Solving COMP 101 Computational Thinking and Design Thursday, September 4, 2014 Carolyn Seaman Susan Martin University of Maryland,
Chapter 12: How Long Can This Go On?
ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.
Data Structures and Algorithms Introduction to Algorithms M. B. Fayek CUFE 2006.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
ITEC113 Algorithms and Programming Techniques
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Algorithmic Problem Solving IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 10, 2013 Marie desJardins University of Maryland, Baltimore.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
Chapter 7 Problem Solving with Loops
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
INTRODUCTION TO PROGRAMMING. Program Development Life Cycle The program development life cycle is a model that describes the stages involved in a program.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Algorithms and Pseudocode
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Selection Using IF THEN ELSE CASE Introducing Loops.
All materials copyright UMBC unless otherwise noted CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking.
Program design Program Design Process has 2 phases:
ALGORITHMS AND FLOWCHARTS
CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking Prof. Katherine Gibson Based on slides by Shawn Lupoli and Max Morawski at UMBC.
3.1 Fundamentals of algorithms
REPETITION CONTROL STRUCTURE
while Repetition Structure
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
ALGORITHMS AND FLOWCHARTS
Introduction To Flowcharting
ALGORITHMS AND FLOWCHARTS
Introduction to pseudocode
Algorithms & Pseudocode
Understanding the Three Basic Structures
Algorithm Discovery and Design
ALGORITHMS AND FLOWCHARTS
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithm Discovery and Design
Click to add Text Computers & Instructions. Computers are given instructions in the form of computer programs that are created through the development.
Flowcharts and Pseudo Code
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
WJEC GCSE Computer Science
COMPUTING.
Presentation transcript:

Adapted from slides by Marie desJardins

Algorithms An algorithm is an ordered set of unambiguous steps that describes a process Examples from real life: Recipes Project directions - chemistry lab, writing prompt Instruction manual (IKEA)

Algorithms Express Solutions to Computational Problems Algorithms are expressed and implemented using languages Possibilities: natural language, pseudocode, visual and textual programming languages Different languages may be more suitable for solving different problems The choice of language can affect clarity or readability, but not whether a solution exists Algorithms can solve many, but not all, problems Many problems can be solved in a reasonable time Some require heuristic (approximate) approaches to solve them in a reasonable time Some problems cannot be solved using any algorithm Algorithms are evaluated analytically and empirically Many possible criteria (e.g., efficiency, correctness, usability, ...) Different algorithms for the same problem can have different measurements for these criteria

Syntax and Semantics Before you can write an algorithm, you have to have a language in which to express it Basic components of every algorithmic language: “Primitive” actions Conditionals: if <condition> then <actions> Loops: repeat <actions> until <condition> Variables: places to store information Input and output: ways to get information into the algorithm and to return information to the invoker Functions: ways to group instructions into a “macro-action” or “subgoal”

Three main control constructs for algorithm development . When developing algorithms, we have 3 control structures available to us Sequence (i.e. one line after the other) Decision making (e.g. using if/else constructs) Looping (e.g. using while loops)

Example – Sequential . Develop an algorithm that will calculate an hourly employee’s weekly pay Step 1 : Understand the problem Input : pay rate and number of hours Process: pay = rate * hours Output: pay

Algorithm – Calculate pay . Plain English Ask for the pay rate and the number of hours worked. Multiply the pay rate by the number of hours. The result is the pay Pseudocode - looks like code, but not a real language 1. Variables: hours, rate, pay 2. Display “Number of hours worked: ” 3. Get hours 4. Display “Amount paid per hour: ” 5. Get rate 6. pay = hours * rate 7. Display “The pay is $” , pay Notice the importance of order and lack of ambiguity

Example - Flowchart Start Display “Amount paid per hour: ” Display “Number of hours worked: ” Get rate Get hours pay = hours * rate Display “The pay is $”, pay End

Example – Decision Making . Develop an algorithm that will figure out if a number is positive Step 1 : Understand the problem Input : The number Process: Is it greater than zero? Output: Message that number is positive or non-positive

Algorithm Plain English . Plain English Ask for the number. Check if it is greater than zero. If it is, it is a positive number. If not (i.e. else), it is not positive Pseudocode - 1. Variable: num 2. Display “Enter the number: ” 3. Get num if num > 0 Display “It is positive” Else 7. Display “It is not positive”

Flowcharts-Decision Making Example If num > 0 display “Positive” Else (that means 0 or negative) display “Not Positive” num >0? True False Display “positive” Display “Not positive”

Looping Develop an algorithm that will add the numbers from 1 to 10. That is 1 + 2 + 3 + …+ 10 = 55 Step 1 : Understand the problem Input : None needed Process: Add 0 to 1 to get a new sum. Add 2 to the old sum to get a new sum. Add 3 to the old sum to get a new sum……Add 10 to the old sum to get a new sum Output: The new sum after 10 iterations

Algorithm Plain English . Plain English Start count at 1. Add to the sum (originally zero) to get a new sum. Increment the count. Repeat the last two steps 10 times Pseudocode - While_Example Numeric Variables: counter = 1, sum = 0 2. While counter <= 10: sum = sum + counter counter = counter + 1 EndWhile Display “ The sum is”, sum

Averaging Algorithm Come up with an algorithm to average a list of numbers For each number in the list, add that number to a counter (initially zero). Once this is done, divide the counter by the number of items in the list. Notice that we had to create another number to 'store' information in. In a programming language something like this is called a variable. Variables are a name that is attached to a number or other piece of information. Think of it as a bucket with a number inside of it.

Averaging Algorithm A more programmatic version of the algorithm: Average(listOfNumbers, length) Create a variable called counter starting at zero For each number in listOfNumbers: Add that number to counter Set counter to (counter / length) Return counter

Averaging Algorithm What the algorithm looks like in python: def average(listOfNumbers, length): counter = 0 for currentNumber in listOfNumbers: counter = counter + currentNumber counter = counter / length return counter