From Pseudocode to Real Code



Similar documents
Pseudo code Tutorial and Exercises Teacher s Version

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

ALGORITHMS AND FLOWCHARTS

Everyday Mathematics CCSS EDITION CCSS EDITION. Content Strand: Number and Numeration

Everyday Mathematics GOALS

Section IV.1: Recursive Algorithms and Recursion Trees

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

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

ALGORITHMS AND FLOWCHARTS. By Miss Reham Tufail

Section 1.1 Real Numbers

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

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

Unit 6 Number and Operations in Base Ten: Decimals

Descriptive statistics Statistical inference statistical inference, statistical induction and inferential statistics

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.

Fourth Grade Math Standards and "I Can Statements"

1st Grade Math Standard I Rubric. Number Sense. Score 4 Students show proficiency with numbers beyond 100.

Notes on Algorithms, Pseudocode, and Flowcharts

Session 7 Fractions and Decimals

Moving from CS 61A Scheme to CS 61B Java

7. Solving Linear Inequalities and Compound Inequalities

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

5.1 Radical Notation and Rational Exponents

PAYCHEX, INC. BASIC BUSINESS MATH TRAINING MODULE

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

Paramedic Program Pre-Admission Mathematics Test Study Guide

Excel: Introduction to Formulas

Solutions of Linear Equations in One Variable

Sect Least Common Multiple

FRACTIONS OPERATIONS

Math Review. for the Quantitative Reasoning Measure of the GRE revised General Test

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

CHAPTER 5. Number Theory. 1. Integers and Division. Discussion

Algorithm and Flowchart Structured Programming 1

Math Workshop October 2010 Fractions and Repeating Decimals

COMPUTER SCIENCE. Paper 1 (THEORY)

Math Refresher. Book #2. Workers Opportunities Resources Knowledge

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Chapter 3. if 2 a i then location: = i. Page 40

Informatica e Sistemi in Tempo Reale

Programming Languages

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Instructor Özgür ZEYDAN (PhD) CIV 112 Computer Programming

Linear Programming Notes V Problem Transformations

Handout #1: Mathematical Reasoning

JavaScript: Control Statements I

13 Classes & Objects with Constructors/Destructors

MATH-0910 Review Concepts (Haugen)

BPS Math Year at a Glance (Adapted from A Story Of Units Curriculum Maps in Mathematics K-5) 1

Maple Mini-Course, Summer 2001

Mathematics Scope and Sequence, K-8

The Euclidean Algorithm

8 Primes and Modular Arithmetic

Access Queries (Office 2003)

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Objective. Materials. TI-73 Calculator

BANK B-I-N-G-O. service charge. credit card. pen. line nickel interest cash bank. ATM dollar check signature. debit card.

CSI 333 Lecture 1 Number Systems

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

The finite field with 2 elements The simplest finite field is

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

ISyE 2030 Test 2 Solutions

Progressing toward the standard

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Lesson 3.1 Factors and Multiples of Whole Numbers Exercises (pages )

Using Casio Graphics Calculators

Microsoft Access 3: Understanding and Creating Queries

Getting Started with R and RStudio 1

Zabin Visram Room CS115 CS126 Searching. Binary Search

Continued Fractions and the Euclidean Algorithm

VB.NET Programming Fundamentals

Arithmetic Coding: Introduction

CALCULATIONS & STATISTICS

KS3 Computing Group 1 Programme of Study hours per week

NUMBER SYSTEMS. William Stallings

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Solve addition and subtraction word problems, and add and subtract within 10, e.g., by using objects or drawings to represent the problem.

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Analysis of Binary Search algorithm and Selection Sort algorithm

Welcome to Basic Math Skills!

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Addition Methods. Methods Jottings Expanded Compact Examples = 15

Python Lists and Loops

Database Applications Microsoft Access

Chapter 7. Using Formulas and Functions

Microsoft Excel 2010 Part 3: Advanced Excel

15 Prime and Composite Numbers

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

Algorithms are the threads that tie together most of the subfields of computer science.

MATHEMATICAL INDUCTION. Mathematical Induction. This is a powerful method to prove properties of positive integers.

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Solution Guide Chapter 14 Mixing Fractions, Decimals, and Percents Together

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

The Crescent Primary School Calculation Policy

COMMON CORE STATE STANDARDS FOR MATHEMATICS 3-5 DOMAIN PROGRESSIONS

Fraction Vocabulary. It is important that vocabulary terms are taught to students.

Transcription:

From to Real Code Once we have expressed an algorithm in pseudocode, we need one more step to turn it into something that machines can do for us: conversion into an actual programming language, or real code For this course, that programming language is chosen because it is built-in to most Web browsers, which means you already have it on whatever computer you may be using This handout hopes to serve as a guide for converting pseudocode into vs. Programming Languages Unlike pseudocode, programming language code is meant to be understood and run by the computer this is where the rubber meets the road: Programming language code is much more precise (and thus less flexible and less forgiving ) than pseudocode Programming languages may have their own distinct symbols and look, which might vary significantly from the original pseudocode Programming languages may have multiple variations for the same concept (e.g., repetitions, conditionals)

Naming and Comments in The table below shows how our previous pseudocode notation translates into They are similar, with needing some additional symbols at times, such as semi-colons and braces name! value var name = value; procedure name(input1, input2, ) algorithm body // Comment. var name = function(input1, input2, ) { algorithm body ; // One-line comment, or /* Comment consisting of multiple lines. */ Repetitions and Conditionals while (condition) (code to repeat) list! [first, second, ] for each (member in list) (code to repeat) if (condition) then (code if condition is true) if (condition) then (code if condition is true) else (code if condition is false) while (condition) { code to repeat var list = [first, second, ]; for (var index = 0; index < list.length; index += 1) { var member = list[index]; code to repeat if (condition) { code if condition is true if (condition) { code if condition is true else { code if condition is false

[Some] Built-In Operations! (assign an expression to a name) = + (addition), (subtraction) +,! (multiplication), (division) *, / = (equal to), <> (not equal to) ===,!== <, <= (less than [or equal to]) <, <= >, >= (greater than [or equal to]) >, >= integer division (no remainder) parseint(dividend / divisor) remainder after division (modulo) % (e.g., ((x % 2) === 1) tests whether x is odd) random number from min max Math.round((max min) * Math.random()) + min Returning Answers and Invoking Other Algorithms return result return result; procedure algorithm(input) code for algorithm algorithm(actualinput) procedure partialanswer(input) code for partialanswer return output value! partialanswer(someinput) var algorithm = function(input) { code for algorithm ; algorithm(actualinput); var partialanswer = function(input) { code for partialanswer return output; ; var value = partialanswer(someinput);

Lists (a.k.a. Arrays) // Creating an empty list. emptylist! [ ] /* 2 choices: */ var emptylist = [ ]; /* or: */ var emptylist = new Array(); // Accessing or assigning an item. item! list[index] list[index]! value var item = list[index]; list[index] = value; add value to list list.push(value); sort list lexically, ascending list.sort(); // Caution: a comes after Z! sort list numerically, ascending list.sort(function(a, b) { return a b; ); number! smallest number in list var number = Math.min.apply(Math, list); Interacting with the User input! information from user (prompted by a message) display message var input = prompt(message); alert(message); The examples below work only for the course s Scratch Page: retrieve text entered into the Input 1 field on the scratch page var form = document.getelementbyid("scratch"); var input1field = form.input1; var input1text = input1field.value; display message at the bottom of the scratch page var displaybox = document.getelementbyid("display"); displaybox.innerhtml = message;

Example Conversions from to There s much more to (especially with regard to what s built-in ) than shown here, but the preceding tables should be enough to translate the pseudocode that you ve seen so far into real programs that you can run within a browser The overall approach would be: Write out your pseudocode, and test it by hand to make sure that it does produce the expected results Refer to the preceding tables to convert each pseudocode segment into its equivalent procedure countcoins(amount, denomination) currentamount! amount coincount! 0 while (currentamount! denomination) (coincount! coincount + 1 currentamount! currentamount denomination) return coincount procedure makechange(amount) currentamount! amount quarters! countcoins(currentamount, 25) currentamount! currentamount (25! quarters) dimes! countcoins(currentamount, 10) currentamount! currentamount (10! dimes) nickels! countcoins(currentamount, 5) currentamount! currentamount (5! nickels) pennies! countcoins(currentamount, 1) return [quarters, dimes, nickels, pennies] var countcoins = function(amount, denomination) { var currentamount = amount; var coincount = 0; while (currentamount >= denomination) { coincount = coincount + 1; currentamount = currentamount - denomination; return coincount; ; var makechange = function(amount) { var currentamount = amount; var quarters = countcoins(currentamount, 25); currentamount = currentamount - (25 * quarters); var dimes = countcoins(currentamount, 10); currentamount = currentamount - (10 * dimes); var nickels = countcoins(currentamount, 5); currentamount = currentamount - (5 * nickels); var pennies = countcoins(currentamount, 1); return [quarters, dimes, nickels, pennies]; ; procedure listrpm(factor1, factor2) if (factor1 > factor2) then (term1! factor2 term2! factor1) else (term1! factor1 term2! factor2) addendlist! [ ] while (term1 > 0) (if (term1 is odd) then (add term2 to addendlist) term1! halvewithoutremainder(term1) term2! double(term2)) product! 0 for each (number in addendlist) (product! product + number) return product var listrpm = function(factor1, factor2) { var term1 = factor1; var term2 = factor2; if (factor1 > factor2) { term1 = factor2; term2 = factor1; var addendlist = [ ]; while (term1 > 0) { if ((term1 % 2) == 1) { addendlist.push(term2); term1 = parseint(term1 / 2); term2 = term2 * 2; var product = 0; for (var index = 0; index < addendlist.length; index += 1) { product = product + addendlist[index]; return product; ;