UMBC CMSC 104 – Section 01, Fall 2016

Slides:



Advertisements
Similar presentations
ITEC113 Algorithms and Programming Techniques
Advertisements

Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Pseudocode.
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
CMSC 1041 Algorithms III Problem Solving and Pseudocode.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
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.
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
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Quiz1 Question  Add Binary Numbers a) b) c) d) e) none
Algorithms and Pseudocode CS Principles Lesson Developed for CS4 Alabama Project Jim Morse.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
All materials copyright UMBC unless otherwise noted CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking.
CMSC 104, Version 8/061L05Algorithms2.ppt Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode Control Structures Reading Section 3.1.
Program design Program Design Process has 2 phases:
CS1010 Programming Methodology
CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking Prof. Katherine Gibson Based on slides by Shawn Lupoli and Max Morawski at UMBC.
Chapter 4 The If…Then Statement
REPETITION CONTROL STRUCTURE
Chapter 4 C Program Control Part I
Week of 12/12/16 Test Review.
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Control Structures II Chapter 3
GC211Data Structure Lecture2 Sara Alhajjam.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Algorithms and Flowcharts
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
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
Algorithm and Ambiguity
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
CS 240 – Lecture 11 Pseudocode.
Relational & Logical Operators
ALGORITHMS AND FLOWCHARTS
Unit# 9: Computer Program Development
2008/09/22: Lecture 5 CMSC 104, Section 0101 John Y. Park
Programming Funamental slides
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
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
UMBC CMSC 104 – Section 01, Fall 2016
Programming We have seen various examples of programming languages
Algorithm and Ambiguity
Problem Solving.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 11 Describing Process Specifications and Structured Decisions
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Flowcharts and Pseudo Code
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Chapter 3: Selection Structures: Making Decisions
Chapter 2: Input, Processing, and Output
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
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

UMBC CMSC 104 – Section 01, Fall 2016 Algorithms II

Notes & Announcements For Project 2… C Code Sample Files New Due Date! 5:30PM, Thurs Oct 6th Write pseudocode for problems 1-3 We’ll cover this today Write C code for problem 4 Read your book (sections 2.2-2.4 for help on this!) There’s a rumor that the book is available online… C Code Sample Files I placed a link to the samples folder on the main page of the class website under “Helpful Links” Added a new sample (cookie.c) for one of today’s problems

Scanning & Printing Characters Last class we covered printing and scanning integer values in C int number = 5; printf("%d", number); /* Prints "5" */ Printing and scanning characters works basically the same! char letter = 'A'; printf("%c", letter); /* Prints "A" */

Problem Solving Decode this sentence: Ocdn dn ocz xjmmzxo vinrzm. We have just come up with a specific solution to a problem. Can this solution be generalized?

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. 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.

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?

Specific Solution to the Problem First, we solve the specific problem to help us identify the steps. 3 cookies left X 2 = 6 cookies left after 2nd child 6 X 2 = 12 cookies left after 1st child 12 X 2 = 24 = original number of cookies

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.

Generic Cookie Problem Algorithm 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.

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.

Pseudocode (con’t) Pseudocode is used in designing algorithms communicating an algorithm to the customer converting an algorithm to code (used by the programmer) debugging 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.

Improved Pseudocode Display “Enter the number of children: “ Read <number of children> Display “Enter the number of cookies remaining: “ Read <cookies remaining> <original cookies> = <cookies remaining> While (<number of children> > 0) <original cookies> = <original cookies> X 2 <number of children> = <number of children> - 1 End_While Display “Original number of cookies = “, <original cookies>

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 > ) 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.

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.

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?

Specific Solution Start$ = Belt$ + Shirt$ + $10 Start$ = Belt$ + (4 X Belt$) + $10 Start$ = 9 + (4 X 9) + 10 = $55

Generic Algorithm Now, let’s write a generic algorithm to solve any problem of this type. What are the inputs to the algorithm? the cost of the first item (doesn’t matter that it’s a belt): <item1 price> the number to multiply the cost of the first item by to get the cost of the second item: <multiplier> the amount of money left at the end of shopping: <amount left>

Generic Algorithm (con’t) What are the outputs from the algorithm? the amount of money available at the start of the shopping trip: <start amount> Note that we may end up needing some intermediate variables.

Pseudocode Display “Enter the price of the first item: “ Read <item 1 price> Display “Enter the multiplier: “ Read <multiplier> Display “Enter the amount left after shopping: “ Read <amount left> <item2 price> = <multiplier> X <item1 price> <start amount> = <item1 price> + <item2 price> + <amount left> Display “The starting amount was “, <start amount>

Control Structures Any problem can be solved using only three logical control structures: Sequence Selection Repetition

Sequence A series of steps or statements that are executed in the order they are written. Example: Display “Enter two numbers: “ Read <number1> Read <number2> <sum> = <number1> + <number2> Display “sum = “, <sum>

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

Selection Example Display “Enter your age: “ Read <age> If (<age> is greater than or equal to 21) Display “Have a beer!” Else Display “Have a soda!”

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.

Repetition Example Display “Enter your age: “ Read <age> While (<age> is less than 21) Display “Too young! Try again: ” End_while Display “Have a beer!”

Let’s Make A Game! Let’s try writing pseudocode for a simple number guessing game. Tell the user you’re thinking of a number between 1 and 10 Ask the user to guess the number. If they are too high or too low, tell them and ask them to guess again. If they guess correctly, tell them and quit Assume your program already has logic built in for generating a random number <random number> = Generate Random Number Between 1 and 10

Questions?