PHYS 2020 Pseudocode. Real Programmers Program in Pencil!  You can save a lot of time if you approach programming in a methodical way.  1) Write a clear.

Slides:



Advertisements
Similar presentations
 Computer Science 1MD3 Introduction to Programming Winter 2014.
Advertisements

Chapter 2: Problem Solving
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Session Objectives# 24 COULD code the solution for an algorithm
Programming Logic and Design Fourth Edition, Introductory
ITEC113 Algorithms and Programming Techniques
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Program Design and Development
Computer Science 1620 Programming & Problem Solving.
Understanding the Mainline Logical Flow Through a Program (continued)
COBOL for the 21 st Century Stern, Stern, Ley Chapter 1 INTRODUCTION TO STRUCTURED PROGRAM DESIGN IN COBOL.
Chapter 1 Program Design
CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.
Chapter 2: Algorithm Discovery and Design
Chapter 3 Planning Your Solution
Fundamentals of C programming
ALGORITHMS AND FLOWCHARTS
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
CSE 1340 Introduction to Computing Concepts Class 2.
The University of Texas – Pan American
Structured COBOL Programming, Stern & Stern, 9th edition
Introduction to Python
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
CHAPTER 1: INTRODUCTION TO COMPUTER SCIENCE Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
By the end of this session you should be able to...
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
CPS120: Introduction to Computer Science Functions.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
The Software Development Process
1 CS161 Introduction to Computer Science Topic #9.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Problem-solving with Computers. 2Outline  Computer System  5 Steps for producing a computer program  Structured program and programming  3 types of.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2.
Progression in KS3/4 Algorithms MONDAY 30 TH NOVEMBER SUE SENTANCE.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to Problem Solving Programming is a problem solving activity. When you write a program, you are actually writing an instruction for the computer.
Lab 4: Loops and Iteration Graham Northup
Lecture 2 Introduction to Programming
Introduction To Flowcharting
Scripts & Functions Scripts and functions are contained in .m-files
ALGORITHMS AND FLOWCHARTS
Designing and Debugging Batch and Interactive COBOL Programs
Programming & languages
Introduction to pseudocode
ALGORITHMS AND FLOWCHARTS
Algorithm and Ambiguity
Data and Flowcharts Session
Data and Flowcharts Session
Flowcharts and Pseudo Code
Functions continued.
Data and Flowcharts Session
Basic Concepts of Algorithm
Presentation transcript:

PHYS 2020 Pseudocode

Real Programmers Program in Pencil!  You can save a lot of time if you approach programming in a methodical way.  1) Write a clear statement of the problem, with the steps you need to solve it, in plain English.  2) Take your statement and turn it into pseudocode. This should be a program, but not in a specific language.  3) When you are satisfied with the logic of the pseudocode, then turn your program into C (or whatever).

What is Pseudocode  Pseudocode mimics computer code in that it contains the same structures, but it does not have a specific syntax.  I have seen very structured pseudocode, but as long as you detail your structures and logic properly, the specific words are not that important.  I think that you should adopt your own preferred statements and practice using them.

Suggestions for Pseudocode  A set of words you can refer to helps e.g.  PRINT “print this to screen”  WHILE (condition is true) DO (this) ENDWHILE  FOR (a specified number of times) REPEAT (this)  IF (this is true) DO (this) ELSE DO (that) ENDIF  MODULE (values passed) (value returned) {do something in a separate module with the values passed and return the value returned}  COMMENT {remind yourself to put some comments in the program}

More Suggestions  LET variable = value {assign a value to a variable}  Variable = INPUT {take input from the key board and assign it to a variable}  OPEN file {open a file for output}  CLOSE file {close a file when finished with it}  WRITE (values) (file) {write values to a file}

An Example  Problem: Design a program to make a database which contains peoples names and occupations. The program should accept input from the keyboard and print output to the terminal. It should also create a file with the information so that it is stored permanently.  Solution in Class Wed 14 th March 2007.

Plain English Statement  The program needs to  Take input from the keyboard to collect a data set of peoples names and occupations  Store this information in variables  Write this information to a file  Repeat this process until the users says to stop  Take input from the keyboard after each entry to see if the user wants to enter another set of data  Use the input to decide whether to keep asking for data or exit.  Ask the user if they want the data printed to the screen after data collection is finished.

What Next?  Now we will write the program in pseudocode.  This is a good planning tool, and also emphasizes the difference between the logical structures needed to write a program (such as DO WHILE (condition is true), and IF (condition is met) DO) which can be expressed in any computing language), and the syntax of a particular language (sort of like writing instructions in e.g. English, French, German, Turkish, Iranian, Hindi, Japanese, Chinese or Vietnamese etc. The language, and the structures of the language, can be quite different, but the meaning conveyed will be the same.  The importance of pseudocode is that it concentrates the mind on the logic flow rather than the syntax of the language.  i.e. Important things like “how do I set up an IF statement” rather than “are all the semicolons in the right place”.

Detailed Pseudocode (1)  Program Setup  Comments in code to explain what program does  Define variables and their types  Print introduction to screen for the the user, explaining what the program does and how they should use it.

Detailed Pseudocode (2)  Collect input from user and store data:  OPEN storevalues PRINT “Would you like to enter data”  PRINT“enter y or n”  answer = INPUT

Detailed Pseudocode (3)  WHILE (answer = y) DO  PRINT “Please enter first name”  First = INPUT  PRINT “Please enter family name”  Family = INPUT  PRINT “Please enter occupation”  Occupation = INPUT  WRITE (First, Family, Occupation) to (storefile)  PRINT “Do you want to enter data for another person? Y or N?”  Answer = INPUT  END WHILE

Detailed Pseudocode (4)  Once answer = n and the loop exits, tidy up and ask whether the user want to print the data entered to the screen.  PRINT “Do you want a printout of what you have entered today? (y/n)”  Printout = INPUT  Test to see if user wants output printed  IF (Printout = y) THEN  WHILE (NOT EOF)  READLINE storefile (first, family, occupation)  Print “first, family, occupation”  ENDWHILE  ENDIF  CLOSE storefile  END program

How Much Pseudocode Should I/you/one* Use?  When you first start writing programs, using detailed pseudocode is really useful to help you get the logic used in computer programming inside your head, to use a technical description.  As you get more familiar with the logical structures (IF, WHILE, DO etc.) you can write less verbose pseudocode.  To be honest, I now write my pseudocode in my head (really!), and go straight to writing in the language.  BUT, I think the torture I was put through as an undergrad, writing pseudocode and programming in the training language PASCAL, is what has enabled me to work out in my head what logical structures a program will need.  Also, with a long and difficult program I still sketch out a skeleton pseudocode plan.

How Much Pseudocode Should I/you/one* Use?  Unfortunately we won’t have time in this course to cover the scripting language PERL, but I love the PERL maxim “there is more than one way to do it”. This applies to programming in C (or any other language).  The aim is to produce a working, maintainable program, that you will still know how to use a year (or more) after you wrote it (and optional but desirable extra, that someone else can understand the logic of and use and modify if necessary). I can give you skills that I think will help you do this in the most efficient way possible, but how I am most efficient will be different to how you are most efficient. When you are coding, reflect on you successes and failures, and find what works best for you

How Much Pseudocode Should I/you/one* Use?  * Why the apostrophe in the title?  Language is a tricky and subtle thing, and computer languages share this trait, so it is good to understand this.  Should the title be “How much pseudocode should I use” or “How much pseudocode should you use” or “How much pseudocode should one use” ?  In English all three forms are acceptable, with the “How much pseudocode should one use” form sounding a bit formal compared to the other two.  However, after spending some months in France recently, I realize that the “How much pseudocode should one use” form is a vulgarism, and the polite way to say this would be “How much pseudocode should we use?”  Computer languages restrict themselves to one form of a statement as correct and all others as unrecognized  When your program wont compile or work, check the syntax!