Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo

Similar documents
Introduction to Programming, Aug-Dec 2008

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Lecture 6: Arithmetic and Threshold Circuits

Bits, Words, and Integers

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

EC121 Mathematical Techniques A Revision Notes

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Outline. Program development cycle. Algorithms development and representation. Examples.

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Basic Arithmetic Operations

2.) = 7.) Find the unit rate of 6 miles in 20 minutes. 4.) 6 8 = 8.) Put in simplified exponential form (8 3 )(8 6 )

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

CS110D: PROGRAMMING LANGUAGE I

Fundamentals of Programming (Python) Getting Started with Programming

Algebra 2 Common Core Summer Skills Packet

Unit 3. Operators. School of Science and Technology INTRODUCTION

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

SECTION 5.1. Sequences

Dr. Relja Vulanovic Professor of Mathematics Kent State University at Stark c 2008

i) Natural numbers: Counting numbers, i.e, 1, 2, 3, 4,. are called natural numbers.

CS112 Lecture: Repetition Statements

MAT 003 Brian Killough s Instructor Notes Saint Leo University

MITOCW watch?v=kvtlwgctwn4

Lecture_Module3 Prof Saroj Kaushik, IIT Delhi

CGF Lecture 2 Numbers

Chapter 8 Algorithms 1

Full file at

One of the most important areas where quantifier logic is used is formal specification of computer programs.

CS102 Unit 2. Sets and Mathematical Formalism Programming Languages and Simple Program Execution

MATH STUDENT BOOK. 11th Grade Unit 1

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

1. Let n be a positive number. a. When we divide a decimal number, n, by 10, how are the numeral and the quotient related?

Learning Log Title: CHAPTER 3: PORTIONS AND INTEGERS. Date: Lesson: Chapter 3: Portions and Integers

Department of Computer Science COMP The Programming Competency Test

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Introduction Objectives: 1) differentiate between high-level, low-level, and machine language; 2) explain why it is necessary to translate a program

Formal Methods. CITS5501 Software Testing and Quality Assurance

A.1 Numbers, Sets and Arithmetic

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Computer Programming C++ (wg) CCOs

Engineering program development. Edited by Péter Vass

Lesson 3: Basic Programming Concepts

Algorithm Discovery and Design

Simple Java Programming Constructs 4

9. Elementary Algebraic and Transcendental Scalar Functions

INTRODUCTION TO ALGORITHMS

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details.

COMP Primitive and Class Types. Yi Hong May 14, 2015

Visual Basic for Applications

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Introduction to Sets and Logic (MATH 1190)

COMP108 Algorithmic Foundations

SME1013 PROGRAMMING FOR ENGINEERS

Skill 1: Multiplying Polynomials

Outline. Introduction to Programming (in C++) Introduction. First program in C++ Programming examples

x= suppose we want to calculate these large values 1) x= ) x= ) x=3 100 * ) x= ) 7) x=100!

Introduction to Modular Arithmetic

Pseudocode. ARITHMETIC OPERATORS: In pseudocode arithmetic operators are used to perform arithmetic operations. These operators are listed below:

PROBLEM SOLVING TECHNIQUES

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Gateway Regional School District VERTICAL ALIGNMENT OF MATHEMATICS STANDARDS Grades 3-6

Algorithms and Flowcharts

Chapter 1: Foundations for Algebra

CSc 10200! Introduction to Computing. Lecture 1 Edgardo Molina Fall 2013 City College of New York

Congruence Arithmetic

A Different Content and Scope for School Arithmetic

Pre-Algebra Notes Unit One: Variables, Expressions, and Integers

Programming in C++ PART 2

CMPT 120 Introduction To Computing Science And Programming I. Pseudocode. Summer 2012 Instructor: Hassan Khosravi

Objective- Students will be able to use the Order of Operations to evaluate algebraic expressions. Evaluating Algebraic Expressions

Lecture 3 Operators MIT AITI

Excel Shortcuts Increasing YOUR Productivity

The RAM Computation Model

Solve the matrix equation AX B for X by using A.(1-3) Use the Inverse Matrix Calculator Link to check your work

Section A Arithmetic ( 5) Exercise A

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Real Numbers finite subset real numbers floating point numbers Scientific Notation fixed point numbers

Rational and Irrational Numbers

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems

CEN 414 Java Programming

Integers and Mathematical Induction

Getting Started with Python

Programming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs

3. Introduction to Algorithm and Flowchart

Arithmetic Algorithms Taught in Schools

DOWNLOAD PDF BIG IDEAS MATH VERTICAL SHRINK OF A PARABOLA

Assertions & Verification & Example Loop Invariants Example Exam Questions

Converting between Percents, Decimals, and Fractions

CS112 Lecture: Loops

COMPUTING Syllabus Version 1.0

Introduction to MATLAB for Engineers, Third Edition

Outline. Data and Operations. Data Types. Integral Types

CSE 215: Foundations of Computer Science Recitation Exercises Set #4 Stony Brook University. Name: ID#: Section #: Score: / 4

Programming II (CS300)

The PCAT Programming Language Reference Manual

MAT 243 Test 2 SOLUTIONS, FORM A

Pre-Calculus Mr. Davis

PROBLEM SOLVING TECHNIQUES SECTION - A. 1. Answer any ten of the following

Transcription:

Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo Purpose: # Describe the method for constructing algorithms. # Describe an informal language for writing PSEUDOCODE. # Provide you with sample algorithms. Constructing Algorithms 1) Understand the problem A common mistake is to start writing an algorithm before the problem to be solved is fully understood. Among other things, understanding the problem involves knowing the answers to the following questions. a) What is the input list, i.e. what information will be required by the algorithm? b) What is the desired output or result? c) What are the initial conditions? 2) Devise a Plan The first step in devising a plan is to solve the problem yourself. Arm yourself with paper and pencil and try to determine precisely what steps are used when you solve the problem "by hand". Force yourself to perform the task slowly and methodically -- think about <<what>> you are doing. Do not use your own memory -- introduce variables when you need to remember information used by the algorithm. Using the information gleaned from solving the problem by hand, devise an explicit step-by-step method of solving the problem. These steps can be written down in an informal outline form -- it is not necessary at this point to use pseudo-code. 3) Refine the Plan and Translate into Pseudo-Code The plan devised in step 2) needs to be translated into pseudo-code. This refinement may be trivial or it may take several "passes". 4) Test the Design Using appropriate test data, test the algorithm by going through it step by step. Think about ordinary sets of input data as well as those which are "degenerate". If you find errors correct them. If serious errors are found, it may be necessary to scrap your pseudo-code and go back to step 2). 1

An Informal Language for Writing Algorithms (Pseudocoding) # An algorithm is a finite series of unambiguous instructions that when given a set of input values produces a desired result. # A desirable property of an algorithm is that it be easy to translate into a programming language. # Algorithms are written during the programming process, which involves the steps below: Problem analysis Algorithm construction Translation of algorithm into programming language Testing and debugging of the program # The ability to write an algorithm is one of the most important skills to be learned by students taking this course! # It is a well known fact of programming life that if the algorithm construction is not done well, a programmer will probably spend excessive amounts of time debugging, i.e. finding and fixing errors in the program. # There are many ways of expressing algorithms. In this class we will adopt an informal algorithmic language which is powerful, concise, and easy to translate into the Turbo Pascal programming language. We will refer to this language as pseudocode. The textbook also discusses pseudocode, as well as flowcharts, which are another way of describing algorithms. # Before specifying our language formally, we'll look at some examples and discuss a few concepts and definitions that will be used below. Example 1: An algorithm that asks for a number, then prints its square. print "Enter a number" read N print "The square of " N " is " N * N Example 2: An algorithm that asks for a number, then tells whether it is odd or even. print "Enter a number" read Number if Number mod 2 = 0 then print N "is even" print N "is odd" 2

Variables It is not possible to write useful algorithms without using variables. A variable can be thought of a named memory cell. Variables can be assigned values and later these values can be inspected or referenced. Note that a variable has both a name and a value. Often we will use a diagram such as the one below to convey this idea. N 12 In this case, the name of the variable is "N" and the value of the variable is 12. Arithmetic Operations We'll assume that the following arithmetic operations are available: + - * / div mod inc dec "div" is integer division. For example, 7 div 3 = 2 4 div 5 = 0 "mod" is used to find the remainder when one number is divided by another, i.e. a mod b is the remainder when a is divided by b. For example 7 mod 3 = 1 and 4 mod 5 = 4. "inc" and "dec" increment and decrement a variable. After N = 1 inc(n) the value of N will be 2. Standard mathematical functions such as square root, log, and abs (absolute value) can be used. 3

Relations and Logical Operations The following relations can be used = equal <> not equal < less than <= less than or equal > greater than >= greater than or equal More sophisticated checks can be made by using the logical operations, AND, OR and NOT. For example, expressions such as A > 0 AND A < 10 are allowed. Pseudo code Statements At first, our language will have 6 types of statements. These are described below. 1) Assignment statements are used to give a values to a variables. Various notation are used for assigning a value to a variable X, including: assign X the value 0 set X to 0 X := 0 X = 0 In examples, the last notation is used, but any of the above are acceptable. Examples of assignment statements: N = 0 N = N + 1 2) Input statements are used to get information from some external source, such as the users keyboard. Algorithms need a way of getting information to be used. "read X" or "input X are both commonly used to indicate the "reading" or "input" of the next item in the input list into the variable X. We'll use the latter notation. Examples of input statements. input X input Number read Number 4

3) Output statements are used to display information determined by an algorithm. There are two sorts of items which we will need to display. a) The value of a variable or expression. b) A string of characters. Several kinds of notation can be used to display the value of a variable X. These include output X display X write X print X Examples 1: print "Hello" Example 2: print "The value of X is " X 4) The if-then statement allows allow other statements to be executed conditionally, i.e. if a certain condition is met. The if-then statement is of the form if <condition> then <statements to be executed if condition is true> The "" serves a delimiter, to mark the end of the statements to be executed if the condition is true. Example 1: if Sum > 0 then print "The sum is positive" Example 2: if N < 0 then print "Please enter a positive number" input N 5

5) The if-then- statement is a more powerful version of the if-then statement. It is of the form: if <condition> then <statements to be executed if condition is true> <statements to be executed if condition is false> Example: if Y < 0 then print "Division by 0 is not allowed" Quotient = X/Y print "The quotient is " Quotient 6) The while statement allows a group of statements to be repeated while a condition is true. It is of the form: while <condition is true> <statements to be executed while condition is true> end while Example: Algorithm to add numbers entered by user. print "Enter a series of number" print "Enter a non-positive number to end." Sum = 0 read N while N > 0 print "Adding " N "to " Sum Sum = Sum + N print "Enter next number " read N end while print "The sum of the numbers you entered is " Sum 6

7) The repeat-until statement allows a group of statements to be repeated until a condition is true. It is of the form repeat <statements to be executed until condition is true> until <condition is true> It is important to note that the statements in the repeat loop body are always executed at least once. Example: Algorithm to read integers entered by user and find the sum. Input ends when a non-positive number is entered. print "This algorithm adds numbers that you enter." print "Enter 0 or negative number to end." Sum = 0 repeat print "Enter next number" input N Sum = Sum + N until N <= 0 print "The sum of the numbers you entered is " Sum 8) The for statement allows a group of statements to be repeated an exact number of times. It is of the form for i = 1 to 10 <statements to be executed> end for The loop variable "i" is automatically incremented by 1, every time the loop is executed. Example: Algorithm to read 10 integers entered by user and find their sum. Input ends after the 10th. number is entered. print "This algorithm adds 10 numbers that you enter." Sum = 0 for i = 1 to 10 print "Enter a number" input N Sum = Sum + N end for print "The sum of the numbers you entered is " Sum 7

Tips On Loop Selection The REPEAT LOOP is usually best if you are constructing a loop that will execute indefinite number of times, but at least once. (Repeat 1 or more times) The WHILE LOOP is usually best if you are constructing a loop that will execute indefinite number of times, and possibly not at all. (Repeats 0 or more times) The FOR LOOP is usually best if you are constructing a loop, and you know exactly how many times the loop has to execute. More Algorithm Examples 1) An algorithm which finds the sum of a list of 10 numbers. List_Size = 10 Sum = 0 while List_Size > 0 input N Sum = Sum + N List_Size = List_Size - 1 end while print "The sum is " Sum 2) An algorithm which asks the user to guess a secret character. If the first guess is incorrect, a clue is revealed and one more chance is given. algorithm Guess_The_Secret_Character print "I'm thinking of a secret character -- what is it? " read Guess if Guess = "H" or Guess = "h" then print "What a guesser! You are correct!" print "The last name of one of your instructors begins with this character." print "What is your second guess? " read Guess if Guess = "H" or Guess = "h" then print "This time you are correct!" print "Sorry, you were wrong again. The secret character is H" end Guess_The_Secret_Character 8