CMSC 104, Version 8/061L05Algorithms2.ppt Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode Control Structures Reading Section 3.1.

Slides:



Advertisements
Similar presentations
Algorithms 10 IST – Topic 6.
Advertisements

CMSC 104, Version 9/011 The while Looping Structure Topics The while Loop Program Versatility o Sentinel Values and Priming Reads Checking User Input Using.
ITEC113 Algorithms and Programming Techniques
Chapter 9 Describing Process Specifications and Structured Decisions
Adapted from slides by Marie desJardins
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
Describe the Program Development Cycle. Program Development Cycle The program development cycle is a series of steps programmers use to build computer.
CMSC 1041 Algorithms III Problem Solving and Pseudocode.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
Algorithms, Part 2 of 3 Topics Problem Solving Examples 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.
Quiz1 Question  Add Binary Numbers a) b) c) d) e) none
CHAPTER 2 GC101 Program’s algorithm 1. COMMUNICATING WITH A COMPUTER  Programming languages bridge the gap between human thought processes and computer.
ALGORITHMS AND FLOWCHARTS
CS1010 Programming Methodology
REPETITION CONTROL STRUCTURE
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Chapter 2: Input, Processing, and Output
GC211Data Structure Lecture2 Sara Alhajjam.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms and Flowcharts
CHAPTER 5A Loop Structure
Pseudocode Upsorn Praphamontripong CS 1110 Introduction to Programming
Introduction To Flowcharting
Algorithm Algorithm is a step-by-step procedure or formula or set of instruction for solving a problem Its written in English language or natural language.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
UMBC CMSC 104 – Section 01, Fall 2016
Algorithm and Ambiguity
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
CS 240 – Lecture 11 Pseudocode.
ALGORITHMS AND FLOWCHARTS
The while Looping Structure
Programming & languages
Pseudocode algorithms using sequence, selection and repetition
2008/09/22: Lecture 5 CMSC 104, Section 0101 John Y. Park
Understanding the Three Basic Structures
1) C program development 2) Selection structure
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
ALGORITHMS AND FLOWCHARTS
3 Control Statements:.
The while Looping Structure
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithm and Ambiguity
Problem Solving.
Computing Fundamentals
Software Development Process
Chapter 11 Describing Process Specifications and Structured Decisions
Click to add Text Computers & Instructions. Computers are given instructions in the form of computer programs that are created through the development.
Boolean Expressions to Make Comparisons
Flowcharts and Pseudo Code
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Topics Introduction to Repetition Structures
Chapter 2: Input, Processing, and Output
Loops.
Basic Concepts of Algorithm
More Loops Topics Relational Operators Logical Operators for Loops.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
UMBC CMSC 104 – Section 01, Fall 2016
The while Looping Structure
WJEC GCSE Computer Science
The while Looping Structure
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

CMSC 104, Version 8/061L05Algorithms2.ppt Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode Control Structures Reading Section 3.1

CMSC 104, Version 8/062L05Algorithms2.ppt Problem Solving Decode this sentence: Pdeo eo pda yknnayp wjosan. We have just come up with a specific solution to a problem. Can this solution be generalized?

CMSC 104, Version 8/063L05Algorithms2.ppt Problem Solving (con’t) Now that we know what algorithms are, we are going to try some problem solving and write algorithms for the problems. The first step in problem solving is to make sure you know exactly what the problem is. Otherwise, you might solve the wrong problem. The next step is to find out what information is available to help solve the problem. When we have solved the problem, what information will we have to provide to the our user?

CMSC 104, Version 8/064L05Algorithms2.ppt Problem Solving (con’t) What calculations will we have to do to solve the problem? Once we have answered those questions, we’ll start with step-by-step instructions that solve a particular problem and then write a generic algorithm that will solve any problem of that type. Once we have our instructions, then we will try to use them to ensure we have the correct answer.

CMSC 104, Version 8/065L05Algorithms2.ppt Someone Stole a Cookie from the Cookie Jar Problem: Momma had just filled the cookie jar when the 3 children went to bed. That night one child woke up, ate half of the cookies and went back to bed. Later, the second child woke up, ate half of the remaining cookies, and went back to bed. Still later, the third child woke up, ate half of the remaining cookies, leaving 3 cookies in the jar. How many cookies were in the jar to begin with?

CMSC 104, Version 8/066L05Algorithms2.ppt Someone Stole a Cookie from the Cookie Jar (cont’d) Information available: o Three children o Each one ate half of the cookies o Three cookies remaining Information needed: o Original number of cookies Calculations: o For each child, multiply the number of remaining cookies by two.

CMSC 104, Version 8/067L05Algorithms2.ppt Specific Solution to the Problem First, we solve the specific problem to help us identify the steps. o 3 cookies left X 2 = 6 cookies left after 2nd child o 6 X 2 = 12 cookies left after 1st child o 12 X 2 = 24 = original number of cookies

CMSC 104, Version 8/068L05Algorithms2.ppt A Generic Algorithm What is a generic algorithm for this problem? An algorithm that will work with any number of remaining cookies AND that will work with any number of children.

CMSC 104, Version 8/069L05Algorithms2.ppt Generic Algorithm for Cookie Problem Get number of children. Get number of cookies remaining. While there are still children that have not raided the cookie jar, multiply the number of cookies by 2 and reduce the number of children by 1. Display the original number of cookies.

CMSC 104, Version 8/0610L05Algorithms2.ppt Test The Generic Algorithm Try the algorithm on paper with: o Four children and six cookies remaining. o Two children with two cookies remaining. If you did not get the correct answer, modify the algorithm so that you get the correct answer.

CMSC 104, Version 8/0611L05Algorithms2.ppt Pseudocode When we broke down the previous problem into steps, we expressed each step as an English phrase. We can think of this as writing pseudocode for the problem. Typically, pseudocode is a combination of English phrases and formulas.

CMSC 104, Version 8/0612L05Algorithms2.ppt Pseudocode (con’t) Pseudocode is used in odesigning algorithms ocommunicating an algorithm to the customer oconverting an algorithm to code (used by the programmer) odebugging logic (semantic) errors in a solution before coding (hand tracing) Let’s write the Cookie Problem algorithm using a more formal pseudocode and being more precise.

CMSC 104, Version 8/0613L05Algorithms2.ppt Improved Pseudocode Display “Enter the number of children: “ Read Display “Enter the number of cookies remaining: “ Read = While ( > 0) = X 2 = - 1 End_While Display “Original number of cookies = “,

CMSC 104, Version 8/0614L05Algorithms2.ppt Observations Any user prompts should appear exactly as you wish the programmer to code them. The destination of any output data should be stated, such as in “Display”, which implies the screen. Make the data items clear (e.g., surround them by ) and give them descriptive names. Use formulas wherever possible for clarity and brevity. Use keywords (such as Read and While) and use them consistently. Accent them in some manner.

CMSC 104, Version 8/0615L05Algorithms2.ppt Observations (con’t) Use indentation for clarity of logic. Avoid using code. Pseudocode should not be programming language-specific. Always keep in mind that you may not be the person translating your pseudocode into programming language code. It must, therefore, be unambiguous. You may make up your own pseudocoding guidelines, but you MUST be consistent.

CMSC 104, Version 8/0616L05Algorithms2.ppt Brian’s Shopping Trip Problem: Brian bought a belt for $9 and a shirt that cost 4 times as much as the belt. He then had $10. How much money did Brian have before he bought the belt and shirt?

CMSC 104, Version 8/0617L05Algorithms2.ppt Brian’s Shopping Trip (cont’d) Information available: o Shirt cost $9. o Belt cost four times as much as the shirt. o Ten dollars left over. Information needed: o Starting amount Calculations o Cost of the shirt plus the cost of the shirt plus ten dollars is the original amount.

CMSC 104, Version 8/0618L05Algorithms2.ppt Specific Solution Start$ = Belt$ + Shirt$ + $10 Start$ = Belt$ + (4 X Belt$) + $10 Start$ = 9 + (4 X 9) + 10 = $55

CMSC 104, Version 8/0619L05Algorithms2.ppt Generic Algorithm Now, let’s write a generic algorithm to solve any problem of this type. What are the inputs to the algorithm? o the cost of the first item (doesn’t matter that it’s a belt): o the number to multiply the cost of the first item by to get the cost of the second item: o the amount of money left at the end of shopping:

CMSC 104, Version 8/0620L05Algorithms2.ppt Generic Algorithm (con’t) What are the outputs from the algorithm? o the amount of money available at the start of the shopping trip: Note that we may end up needing some intermediate variables.

CMSC 104, Version 8/0621L05Algorithms2.ppt Testing the Generic Algorithm Try the algorithm with: o Belt cost ten dollars, shirt cost five times as much, and and there was twenty-five dollars left. o Belt cost thirty dollars, shirt cost two times as much, and and there was forty-five dollars left.

CMSC 104, Version 8/0622L05Algorithms2.ppt Pseudocode Display “Enter the price of the first item: “ Read Display “Enter the multiplier: “ Read Display “Enter the amount left after shopping: “ Read = X = + + Display “The starting amount was “,

CMSC 104, Version 8/0623L05Algorithms2.ppt Control Structures Every problem can be solved using only three logical control structures: o Sequence o Selection o Repetition

CMSC 104, Version 8/0624L05Algorithms2.ppt Sequence A series of steps or statements that are executed in the order they are written. Example: Display “Enter two numbers: “ Read = + Display “sum = “,

CMSC 104, Version 8/0625L05Algorithms2.ppt Selection Defines one or more courses of action depending on the evaluation of a condition. Synonyms: conditional, branching, decision Examples: If (condition is true) If (condition is true) do this do this End_if Else do that End_if

CMSC 104, Version 8/0626L05Algorithms2.ppt Repetition Allows one or more statements to be repeated as long as a given condition is true. Synonyms: looping, iteration Example: While (condition is true) do this End_while Notice the repetition structure in the Cookie Problem pseudocode.

CMSC 104, Version 8/0627L05Algorithms2.ppt Use Of Control Structures In this course, you can only use these control structures. It has been proven that using only these structures: o Reduces the number of mistakes o Enables us to verify the algorithm is correct o Provides us with a way to test our programs.