HONP112 - Week 11 Professor Jim Williams

Similar documents
Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

=

PAYCHEX, INC. BASIC BUSINESS MATH TRAINING MODULE

QM0113 BASIC MATHEMATICS I (ADDITION, SUBTRACTION, MULTIPLICATION, AND DIVISION)

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

That s Not Fair! ASSESSMENT #HSMA20. Benchmark Grades: 9-12

Counting Money and Making Change Grade Two

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

VHDL Test Bench Tutorial

Independent samples t-test. Dr. Tom Pierce Radford University

Chapter 12 Programming Concepts and Languages

What Is Singapore Math?

Decimals and other fractions

Section 1.5 Exponents, Square Roots, and the Order of Operations

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

OA3-10 Patterns in Addition Tables

Decimal Notations for Fractions Number and Operations Fractions /4.NF

Click on the links below to jump directly to the relevant section

NF5-12 Flexibility with Equivalent Fractions and Pages

1. The Fly In The Ointment

6.3 Conditional Probability and Independence

Algorithm & Flowchart & Pseudo code. Staff Incharge: S.Sasirekha

2 SYSTEM DESCRIPTION TECHNIQUES

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

Sample Fraction Addition and Subtraction Concepts Activities 1 3

Database Programming with PL/SQL: Learning Objectives

YOU MUST BE ABLE TO DO THE FOLLOWING PROBLEMS WITHOUT A CALCULATOR!

1 ST GRADE COMMON CORE STANDARDS FOR SAXON MATH

Session 7 Fractions and Decimals

Commutative Property Grade One

**Unedited Draft** Arithmetic Revisited Lesson 5: Decimal Fractions or Place Value Extended Part 3: Multiplying Decimals

Math Journal HMH Mega Math. itools Number

Computation Strategies for Basic Number Facts +, -, x,

Introduction to formal semantics -

Math Games For Skills and Concepts

Quosal Form Designer Training Documentation

3 Some Integer Functions

Fractions to decimals

MULTIPLICATION AND DIVISION OF REAL NUMBERS In this section we will complete the study of the four basic operations with real numbers.

DECIMAL COMPETENCY PACKET

Minnesota Academic Standards

Cobol. By: Steven Conner. COBOL, COmmon Business Oriented Language, one of the. oldest programming languages, was designed in the last six

Social Return on Investment

3. Mathematical Induction

Managing Variability in Software Architectures 1 Felix Bachmann*

Pocantico Hills School District Grade 1 Math Curriculum Draft

EKT150 Introduction to Computer Programming. Wk1-Introduction to Computer and Computer Program

Parsing Technology and its role in Legacy Modernization. A Metaware White Paper

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Division of whole numbers is defined in terms of multiplication using the idea of a missing factor.

Chapter 3 Review Math 1030

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Using Proportions to Solve Percent Problems I

The programming language C. sws1 1

Introduction to Fractions, Equivalent and Simplifying (1-2 days)

Working with whole numbers

Computer Science 281 Binary and Hexadecimal Review

Grade 4 Unit 3: Multiplication and Division; Number Sentences and Algebra

Code Kingdoms Learning a Language

Binary Adders: Half Adders and Full Adders

A Numeracy Refresher

1. The RSA algorithm In this chapter, we ll learn how the RSA algorithm works.

Clock Arithmetic and Modular Systems Clock Arithmetic The introduction to Chapter 4 described a mathematical system

JobTestPrep's Numeracy Review Decimals & Percentages

Math Content by Strand 1

Welcome to Harcourt Mega Math: The Number Games

Basic numerical skills: FRACTIONS, DECIMALS, PROPORTIONS, RATIOS AND PERCENTAGES

Sources: On the Web: Slides will be available on:

Relative and Absolute Change Percentages

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

Chapter 13: Program Development and Programming Languages

Java Basics: Data Types, Variables, and Loops

Q&As: Microsoft Excel 2013: Chapter 2

Chapter 14: Boolean Expressions Bradley Kjell (Revised 10/08/08)

Welcome to Basic Math Skills!

The VB development environment

RSA Encryption. Tom Davis October 10, 2003

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

Base Conversion written by Cathy Saxton

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Verbal Phrases to Algebraic Expressions

A positive exponent means repeated multiplication. A negative exponent means the opposite of repeated multiplication, which is repeated

C# and Other Languages

IV. ALGEBRAIC CONCEPTS

Unit 6 Number and Operations in Base Ten: Decimals

Unit 7 The Number System: Multiplying and Dividing Integers

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

ZIMBABWE SCHOOL EXAMINATIONS COUNCIL. COMPUTER STUDIES 7014/01 PAPER 1 Multiple Choice SPECIMEN PAPER

Activity 1: Using base ten blocks to model operations on decimals

Programming Languages

A Short Guide to Significant Figures

The Euclidean Algorithm

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

6th Grade Lesson Plan: Probably Probability

Minimax Strategies. Minimax Strategies. Zero Sum Games. Why Zero Sum Games? An Example. An Example

Order of Operations More Essential Practice

hp calculators HP 17bII+ Net Present Value and Internal Rate of Return Cash Flow Zero A Series of Cash Flows What Net Present Value Is

Transcription:

HONP112 - Week 11 Professor Jim Williams An algorithm is a clearly-defined series of steps and decisions that are used to solve a specific problem. Algorithms are used in many different areas, not just computers. We use algorithms every day in real life, but are not usually aware of them. Some examples: Adding 2 numbers together Following driving directions Think of some others. 1

Allow us to clearly define and understand a problem. Provide us with a repeatable, precise way to solve the problem. Must have a clear beginning and a clear ending. To get from the beginning to the end, there are a precise number of steps and decisions to follow. Operates exactly the same way any time it is used. Any problem we expect a computer to solve must first be broken down into an algorithm. This is because the computer can only make yes/no decisions and follow exact instructions. A problem that cannot be represented as an algorithm cannot be solved by a computer. 2

A lot of human thought involves intuition. Computers are not intuitive. Therefore, a computer cannot solve problems like Is a person trustworthy? Does a person dislike me? Is this web site REALLY useful to me? Did he mean $1.00 or $100? Humans may make decisions based on prior knowledge and experience. For example, if you are driving to school, and there is road construction, you may know another way to get here. Unless the computer is also aware of the construction, it cannot give you an alternate way of getting to school. Because a computer lacks intuition and certain knowledge, it has no way of knowing if the results it produces are right. It only knows how to follow exact instructions. Keep this in mind when we discuss the three types of programming errors later on. 3

Before we can design an algorithm, we must have a complete understanding of a problem and how it is solved. Then the problem must be broken down into a series of steps and decisions. The resulting algorithm must be precise and account for any possibility. When we solve problems, we usually do not think in terms of small steps. Rather, we usually use intuition and experience in the course of solving problems. Because of this, it is not easy to design algorithms, even for the simplest of problems. Let s take a closer look at an algorithm we all learned in grade school: Adding two whole numbers together. How do we do it? 4

We start at the rightmost column (column 0) We add the bottom digit to the top digit. If the result <10, we put in in the corresponding result column. If result not <10, we put [result 10] into the corresponding result column, and carry a 1 to the next column. Move to next column and repeat until all the columns are accounted for. The steps are performed in a certain order. A decision is made and different action taken based on the decision (i.e. when do we carry?) Certain steps are repeated over and over again until we run out of columns. What if we wanted to add 100 + 50? What if we wanted to add 34789 + 0? What if we wanted to add 5286 + 1? Would we go through all of the steps of the algorithm to get the solution? A computer would have to do this. 5

Notice that the algorithm as stated only works for base-10 addition. How can we improve the algorithm to account for different bases? Can you think of any other limitations of the algorithm? What about fractional numbers? As you can see an algorithm is only effective within a very specific SCOPE. When designing algorithms we need to clearly specify the scope of the problem. What types of problems the algorithm can solve More importantly, what types of problems the algorithm will NOT solve! Our algorithm for adding two whole base-10 numbers together illustrates the 3 ways in which the flow of the algorithm is controlled. These are called control structures. 6

Sequence - Do things in order Selection (AKA Choice) Answer a yes/no question and perform some action based on the answer. Iteration (AKA Repetition) - Keep doing the same thing over and over again until a certain condition is met (or no longer met). In programming this is commonly called a loop. To make algorithms easier to understand, we can use diagrams called flow charts to illustrate the flow of events. Start Do Step A Do Step B Yes Is some condition met? No Do Step C End 7

Start Do Step A Do Step B These steps are done in order Yes Is some condition met? No Do Step C End Start Do Step A Do Step B Yes Is some condition met? No Do Step C End Start Do Step A Do Step B Step B repeats until the condition is no longer met. Yes Is some condition met? No Do Step C End 8

Know what types of problems can be solved algorithmically. Understand the limitations and scope of an algorithm given its application. Understand the three control structures. Know how to read and understand a flow chart. A Program is a series of instructions that tell a computer what to do (or how to solve a problem). A program is therefore a software implementation of an algorithm. To put it another way, a program is an algorithm in a form that the computer can understand (as you already know, programs are either compiled or interpreted, based on the language they are written in). A programming language is a tool that allows a person to tell a computer what to do. All programming languages must allow a person to explain an algorithm it in a way that the computer can later understand. Why do we need programming languages? (hint: do people speak in ones and zeros?) 9

Java JavaScript C++ Visual Basic VB Script BASIC COBOL SQL Syntax (the grammar of the language) Variables (place to store values) Operators (do something with the stored values) Commands (direct instructions to the computer) Algorithmic Control (sequence, selection, iteration) There are many different programming languages, and each has its own syntax. The Syntax of a program is a set of rules about how the instructions can be written. 10

The Syntax of a programming language is analogous to grammar in normal spoken/written languages. Example: In English, a predicate (verb) generally follows an object (noun). You wouldn't say "Drove I to the store," you would say "I drove to the store." Different languages may use different grammar rules. This also applies to programming languages. Example: In English, an adjective comes before a noun, as in "red car." In Spanish, it is just the opposite. The above phrase would be translated as "car red" Any language also has a set of words, or vocabulary, that are legal in the language. Example: In English, there is no such word as yplughrt. Computer languages also have a list of legal words (usually much smaller than in human languages). 11

Some are case-sensitive, others are not. Some require specific symbols to mean end of the statement, others do not. There may be different legal commands in one language versus another. There may be different ways to express the selection and iteration control structures. There may be different ways to represent variables and operators (we will discuss these later) A variable is place (or container ) to store a piece of information whose value may change. A variable is really a pointer to a specific memory address that reserve enough space for a certain amount of bits (depending on the type of variable we need). In modern programming languages, we do not specify what the address is specifically; the operating system will handle that behind the scenes. Let s say we wanted to tell the computer to add any 2 numbers together. The computer needs a place to hold the first addend. The computer needs a place to hold the second addend. The computer needs need a place to store the sum after it has done the arithmetic. 12

The NAME of a variable tells us what we will call the container. Every variable must have a name. The TYPE of a variable tells us what is allowed to be stored in it. The CONTENTS of the variable changes depending on what is actually stored in the container. Suppose you have a jar at home you call "My Spare Change" Every day, you empty your pocket change into the jar. Also, you may take change out of the jar as you need to use it. Thus, the total amount of the money in the jar changes. But the jar itself does not. Also, you do not put anything into the jar that is not pocket change (like buttons or toothpicks). If you can imagine your jar as a variable, and coins as what is allowed to be put into the variable, you will discover 2 things: The NAME of the variable is "My Spare Change" The TYPE of the variable is "coins". The CONTENTS of the variable depend on what you have inside the jar (how many nickels, pennies, dimes, etc.). 13

Variable NAMES point to a location in memory where the variable is stored. Think of memory as a large collection of bins where you can store things. The computer understands the variable name as the location of the bin where the variable is kept. Variable TYPES tell the computer about what the information in the variable means, and what the computer is allowed to do with the information. Consider 2 types of variables - numeric (numbers) and character (words or letters). Would a computer attempt to multiply a number by a character? NOTE: You should recognize that this concept is very similar to field types when we discussed databases. The same principles apply as far as storage space in memory, etc. An operator represents an action that can be performed against one or more variables. Operators help the computer calculate values and make decisions. You already know all about operators from everyday life (but may not realize it). 14

Mathematical Operators Comparison Operators Logical Operators These are very familiar to most people. Some examples include: Add (-) Subtract (-) Multiply (*) Divide (/) Modulus (remainder from division) Like the name implies, these operators compare the values of two variables or expressions. Some examples: GREATER THAN (>) Example 5 > 3 LESS THAN (<) Example 3 < 5 EQUAL TO (=) Example 1 = 1 15

When the computer compares two things using a comparison operator, it evaluates the comparison as either TRUE or FALSE (or YES/NO or 1/0). Therefore the result of a comparison operation is actually a boolean value. Expression What We Think What the Computer Thinks 4 < 2 Is 4 less than 2? False 5 = 6 Is 5 equal to 6? False 5 = 5 Is 5 equal to 5? True 100 > 4 Is 100 greater than 4? True 4 < 10 Is 4 less than 10? True 6 > 200 Is 6 greater than 200? False Logical (boolean) operators are the key to helping the computer to make decisions and answer questions. You should remember these from early in the term: NOT (condition must not be true) AND (all of the conditions must be true) OR (either of the conditions must be true) 16

These are in reality combinations of comparison and logical operators, but commonly written in this shorthand form: GREATER THAN OR EQUAL TO (>=) LESS THAN OR EQUAL TO (<=) NOT EQUAL TO (!=, <>) Commands are special words or phrases that directly tell the computer do do something. Different programming languages can have different command words and phrases. What types of commands to you think we may want to give to the computer? Begin the program End the program Take input from the user Send output to the screen or printer Make a new variable Put a value into variable Do a procedure or run another program Open a file etc. Each programming language would have a special way of telling the computer to do these things. 17

Every programming language must have a way to understand the three control structures: Sequence Selection iteration. Different languages have different ways of doing this. BEGIN PROGRAM MAKE VARIABLE ADDEND_1, TYPE IS NUMERIC MAKE VARIABLE ADDEND_2, TYPE IS NUMERIC MAKE VARIABLE SUM, TYPE IS NUMERIC SEND OUTPUT PLEASE ENTER 2 NUMBERS TO SCREEN TAKE INPUT FROM KEYBOARD INTO ADDEND_1 TAKE INPUT FROM KEYBOARD INTO ADDEND_2 PUT (ADDEND_1 + ADDEND_2) INTO SUM SEND OUTPUT HERE IS YOUR ANSWER TO SCREEN SEND OUTPUT SUM TO SCREEN END PROGRAM Every programming language must have a way to express: Syntax Variables Operators Commands Algorithmic Control 18

As you are well aware, programs can contain errors (colloquially called bugs ). There are three main types of errors. It is important that you know what these are and how to identify them. Syntax Errors Run-Time Errors Logic Errors If the way the program is written does not make sense to the computer, the program cannot be used. This is called a syntax error. If the syntax is correct, the computer is allowed to translate the written program into its own language (1s and 0s) and run the program. If not, will either (a) NOT compile, or (b) not be interpreted. Either way, the complete program can never run. Therefore, syntax errors are usually fairly easy to identify and correct. IF (a > b) THEN LET c = 0; IF (a < b THEN LET c = 1; In the second line, notice that there is an unclosed parenthesis. 19

This is an error that causes a computer program that is already running successfully to crash. It may work fine in some cases, but crash in other cases. Usually this is because some particular cases were not though out well enough in design (i.e. a number gets too big to hold in memory the right way), or the program does not validate user input well enough (i.e. lets a user try to divide by zero). Run time errors are harder to identify and correct than syntax errors. It is very important to make sure that a program is rigorously tested and to deliberately try to break the program during testing. This can help identify run-time errors. ENTER a; ENTER b; LET c = (a / b); DISPLAY c; What if the value of b is zero? This will result in a run-time error, unless care is taken to prevent this. For instance, we can keep asking to enter a value for b until the value entered is NOT zero (using iteration and selection). The program executes properly in all cases, and never stops running because of a syntax or runtime error. HOWEVER, it does not solve the problem correctly! Logic errors are the most dangerous type, because the computer does not know that anything is wrong. Logic errors are extremely difficult to identify and correct. Imagine a mission critical system (medical, financial, navigation, defense, etc.) that has a logic error. VERY BAD THINGS CAN HAPPEN!!! 20

LET X=0; WHILE (X < 10) DISPLAY Hello ; END-WHILE This is called an endless loop. Since the condition to exit the loop will never be met, it will keep repeating indefinitely. LET carry = 0; LET result = addend1 + addend2; IF (result > base) THEN LET result = result base; LET carry = 1 END-IF Based on the context of the problem, you should be able to identify the logic error in this piece of a larger program. Can you?. Know how to identify the three types of errors. Syntax Run-Time Logic 21

The examples I gave were only to illustrate the concepts. These were not examples of any real programming language. The important thing to understand is HOW programming languages are constructed and what they are capable of doing. In general, if you do become proficient in a particular programming language, you can adapt that knowledge to any other language. 22