An algorithm may be expressed in a number of ways:

Similar documents
Learning the Language - V

Assessment of Programming Skills of First Year CS Students: Problem Set

Unit 3. Operators. School of Science and Technology INTRODUCTION

Our Strategy for Learning Fortran 90

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

CS Introduction to Data Structures How to Parse Arithmetic Expressions

9. Elementary Algebraic and Transcendental Scalar Functions

Chapter 4: Basic C Operators

YOLOP Language Reference Manual

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

ffl assign The assign has the form: identifier = expression; For example, this is a valid assign : var1 = 20-3*2; In the assign the identifier gets th

Operators. Java operators are classified into three categories:

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Simple Java Programming Constructs 4

Chapter 3: Operators, Expressions and Type Conversion

ENGR 105: Introduction to Scientific Computing. Dr. Graham. E. Wabiszewski

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

CSI Lab 02. Tuesday, January 21st

Department of Computer Science

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

, has the form T i1i 2 i m. = κ i1i 2 i m. x i1. 1 xi2 2 xim m (2)

Department of Computer Science COMP The Programming Competency Test

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

6.001 Notes: Section 1.1

Lesson 1: Arithmetic Review

Lesson 3: Basic Programming Concepts

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

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

TABLE OF CONTENTS LIST OF ILLUSTRATIONS

Basic stuff -- assignments, arithmetic and functions

Implementing an Algorithm for Boomerang Fraction Sequences in Python

Flow Control. So Far: Writing simple statements that get executed one after another.

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

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

2.2 Syntax Definition

Chapter 17. Fundamental Concepts Expressed in JavaScript

A Simple Syntax-Directed Translator

Lesson 1: Arithmetic Review

OBJECT ORIENTED SIMULATION LANGUAGE. OOSimL Reference Manual - Part 1

Exponents. Reteach. Write each expression in exponential form (0.4)

Course Outline. Introduction to java

Fortran 90 Two Commonly Used Statements

I. Variables and Data Type week 3

Chapter 1: Foundations for Algebra

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Arithmetic expressions can be typed into Maple using the regular operators:

Information Science 1

MITOCW watch?v=kz7jjltq9r4

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Algebra 1 Review. Properties of Real Numbers. Algebraic Expressions

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

2.8. Decision Making: Equality and Relational Operators

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

CSC 467 Lecture 3: Regular Expressions

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Full file at C How to Program, 6/e Multiple Choice Test Bank

Introduction to Programming

easel LANGUAGE REFERENCE MANUAL

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Project 2: How Parentheses and the Order of Operations Impose Structure on Expressions

Language Reference Manual simplicity

2. Numbers In, Numbers Out

Logical statements and. Lecture 1 ICOM 4075

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Introduction to Python Programming

Draw a diagram of an empty circular queue and describe it to the reader.

Output: For each size provided as input, a figure of that size is to appear, followed by a blank line.

LOON. Language Reference Manual THE LANGUAGE OF OBJECT NOTATION. Kyle Hughes, Jack Ricci, Chelci Houston-Borroughs, Niles Christensen, Habin Lee

Theory of control structures

Section 1.8. Simplifying Expressions

Information Science 1

Add Subtract Multiply Divide

Fundamentals of Programming

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

Program Design Phase. Algorithm Design - Mathematical. Algorithm Design - Sequence. Verify Algorithm Y = MX + B

CSC402 Midterm Due in Sakai Monday 11/6/17 Version 1.1

Math 3 Coordinate Geometry Part 2 Graphing Solutions

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators

Variables and Values

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

COMS 469: Interactive Media II

Maciej Sobieraj. Lecture 1

CMAT Language - Language Reference Manual COMS 4115

Introduction to C# Applications

People = End Users & Programmers. The Web Browser Application. Program Design Phase. Algorithm Design -Mathematical Y = MX + B

ffl assign : The assign has the form: identifier = expression ; For example, this is a valid assign : var1 = 20-3*2 ; In the assign the identifier get

Transcription:

Expressing Algorithms pseudo-language 1 An algorithm may be expressed in a number of ways: natural language: flow charts: pseudo-code: programming language: usually verbose and ambiguous avoid most (if not all) issues of ambiguity; difficult to modify w/o specialized tools; largely standardized also avoids most issues of ambiguity; vaguely resembles common elements of programming languages; no particular agreement on syntax tend to require expressing low-level details that are not necessary for a high-level understanding

Common Elements of Algorithms pseudo-language 2 acquire data (input) computation selection iteration some means of reading values from an external source; most algorithms require data values to define the specific problem (e.g., coefficients of a polynomial) some means of performing arithmetic computations, comparisons, testing logical conditions, and so forth... some means of choosing among two or more possible courses of action, based upon initial data, user input and/or computed results some means of repeatedly executing a collection of instructions, for a fixed number of times or until some logical condition holds report results (output) some means of reporting computed results to the user, or requesting additional data from the user

Simple Variables pseudo-language 3 simple variables Some means hold a simple value, like the number of elements in a list. Generally, we do not need to be too careful about the notion of types, but it is useful to distinguish between variables that hold numbers, character strings, and logical values. Variables must be given names and those names should be descriptive. For readability, we will not allow whitespace in names. Variables must be declared and given a type before they are used: number WidthInFeet string NameOfBook logical valuewaseven Logical variables can have the values true and false, which are considered part of the pseudo-language vocabulary and should not be used as variable names.

List Variables pseudo-language 4 list variables Some means to refer to a collection of simple values as a unit, and to also refer to individual values within the collection. Mathematically we can think of this as a subscripted list. For convenience we often adopt a slightly different notation; for a list named Scores, Scores[i] refers to the i-th element in the list; we number elements starting at 1. List declarations look like: list number Scores list string booktitles (We don't specify a size for the list, as we would do for an array in most programming languages that's not necessary for stating an algorithm.)

pseudo-language: the Basics pseudo-language 5 acquiring data get <variable> Obtain a value for the specified variable; no source is specified; this could stand for reading from a file or other device, or even for prompting a user for interactive input. reporting results display <variable> Report the value of the variable in some manner; no destination is specified; this could stand for returning a value from the algorithm, for writing to a file or other device, or for displaying to a console window.

pseudo-language: the Basics pseudo-language 6 computation Use common mathematical notation, slightly adjusted to reflect keyboard limitations. Any common mathematical and logical expressions can be formed using: +, - addition, subtraction *, / multiplication, division ^ exponentiation, e.g, x^y means x to the power y NOT,! logical negation AND, && logical and OR, logical or (, ) grouping The default precedence is defined by the ordering above (low to high precedence). When in doubt, add parenthesis for clarity.

pseudo-language: the Basics pseudo-language 7 computation (continued) In addition, any standard mathematical functions can be used, with suitable notation: x y sin( theta ) Since the basic precedence rules cannot cover all such cases, if there's any doubt about clarity, use parentheses to disambiguate. For setting the value of a variable from an expression, we will use <variable> := <expression>

pseudo-language: Precedence Rules pseudo-language 8 We must have some definition of the order of operations, unless we want to write lots of parentheses. The ordering used here is slightly adapted from the precedence rules for the C language: (, ) grouping NOT,! ^ logical negation exponentiation *, / multiplication, division (parenthesize if both are chained) +, - addition, subtraction (parenthesize if both are chained) AND, && OR, logical and logical or Remember, when in doubt, add parenthesis for clarity. highest lowest

pseudo-language: Other Issues pseudo-language 9 halting the algorithm halt Means "stop now". May be used at any point within the algorithm. comments In many cases, it is useful to add comments to the (human) reader; there must be some way to distinguish comments from the algorithm itself; we'll use the convention that anything following the symbol '#' on a line is a comment

Algorithm Interface pseudo-language 10 To refer to algorithms, and especially to refer to one algorithm from another, we need to incorporate the notion of an interface to an algorithm: algorithm <name> takes <list of inputs> For example: algorithm XtoN takes number X, number N # Computes the value of X^N. # Pre: X is a number, N is an integer, N >= 0. # number XtoN # result... display XtoN halt # report result

Algorithm Invocation pseudo-language 11 We must also be able to express the invocation of an algorithm: <name> ( <list of input values to algorithm> ) For example: algorithm FutureValue takes number PV, number Rate, number Years # present value # annual pct rate # term # Computes the future value of an asset, assuming # annual compounding of a given rate of return. # Pre: PV and Rate are non-negative numbers. # Years is a non-negative integer. # number FV # future value number ScalingFactor # growth factor ScalingFactor := XtoN(1 + Rate, Years) # invoke algorithm XtoN FV := PV * ScalingFactor display FV halt

pseudo-language: Selection pseudo-language 12 selection if <condition> # one or more statements endif if <condition> # one or more statements else # one or more statements endif A condition may be an algebraic comparison of two variables, such as numberofzeros < 100 or a logical concept, such as havenotseenazero

pseudo-language: Selection Examples pseudo-language 13 if x <= y diff = y - x endif if x <= y diff = y - x else diff = x - y endif

pseudo-language: Iteration pseudo-language 14 iteration while <condition> # one or more statements endwhile

pseudo-language: Iteration Examples pseudo-language 15 while x <= y x := 2 * x endwhile while y >= 0 if ( x > y ) display iter halt endif iter := iter + 1 y := y - x endwhile # y >= 0