Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240 – Lecture 11 Pseudocode.

Similar presentations


Presentation on theme: "CS 240 – Lecture 11 Pseudocode."— Presentation transcript:

1 CS 240 – Lecture 11 Pseudocode

2 Pseudocode – Ideas on Paper before Coding
This is going to be review of what should hopefully have been discussed in previous course work. Pseudocode is a structured in-English description of an algorithm. The lines of pseudocode act as instructions that should be good enough for a person familiar with the problem domain to follow and solve the problem. These instructions should be high-level but not ambiguous. You don't need to go into the mechanics of how a simple task should be accomplished. Example: Read a line from input. vs. Call fgets on buffer, 100, and stdin.

3 Pseudocode – Basic Instruction Sequences
Pseudocode is intended to be a code, but it's not intended to be compiled for a computer. This means that it otherwise is intended to share the same structure as actual program code. At the very least, pseudocode consists of a sequence of instructions. Here's an example of pseudocode for a general morning: Wake up Shower Get dressed Eat breakfast

4 Pseudocode – Referring to Variables
When writing out instructions, it's very likely that you will encounter variables, things you'll need to reference that were involved with other instructions. There are two ways to refer to them: indirectly and directly. Directly: Pick up a ball, call it ball1 Throw ball1 into the basket Indirectly: Pick up a ball Throw that ball into the basket You should only really use direct reference if something has already been named or not naming it would lead to ambiguity.

5 Pseudocode – Control Flow
Like any algorithm, even one written in English needs control flow. There are states that need to be accounted for when solving a problem, so there must be things like If- and While-statements. For when these are needed, they naturally flow into the way you describe solving the problem. while my room is still dirty look for something to clean clean it You can also use if in the same natural way: if I find dirty clothes put them in the hamper then go back to cleaning

6 Pseudocode – Control Flow with For
By now, you should be realizing that a lot of the keywords that we use in our programming languages come directly from their meaning in English. The for-loop is no different and we can use a similar statement in Pseudocode for every pair of pants in the hamper check if they have a big hole in them if they do throw them out

7 Pseudocode – Formal Treatment
For academia and industry, when pseudocode is expected, there are some general expectations about clarity and consistency. There is no "standard" syntax for pseudocode but there's a general "I know it when I see it" for good and bad pseudocode. It's often good to establish conventions that you and your group are going to use so that there is no confusion about anything you've written. Common conventions include Using indentation to indicate block structure (as seen on previous slides) Starting control-flow lines with familiar terms like while, for, if, etc. Explicitly naming and assigning direct variables with words like "set X to ___"

8 Pseudocode – From Code to Code
The secondary purpose for Pseudocode is to be turned into program code. Generally, Pseudocode is written for a programmer or by a programmer for structuring the idea behind an algorithm. When it's time to actually automate solving that problem, pseudocode should be translatable, line-by-line, to actual source code. Read a line from input fgets(str,10,stdin); Print that line to output printf("%s",str); Some lines of pseudocode may turn into more than one line of actual source code.

9 Pseudocode Examples – Homework 2 Echo
Read one character from input If reading from input failed Print an error message Otherwise or else Print that character to output End program optional but sometimes it looks good

10 Pseudocode Examples – Homework 2 Count
Read a character from input Set counter to 0 Set current to that character While that character wasn't End of File If that character is the same as current increment the counter Else print the counter set current to that character set counter to 1 Read a new character from input End Program

11 Pseudocode Examples – Homework 3 Mid
For pseudocode for functions, it's fine to include a generic function header. function mid(first, second, third) if first is greater than second and third and second is greater than third return second otherwise return third if second is greater than first and third and first is greater than third return first if third is greater than first and second and first is greater than second return first otherwise return second End function It's obvious from the way the function is described that it has a return type and that it should be some numeric.

12 Pseudocode Examples – Homework 3 Snag
function contains(line, ch) for every character in line if that character equals ch Return true Return false End function Main Read in a character as ch Clear the newline after that character For every line of input if line contains ch it's clear how to check! print "FOUND: " then print the line after that else print "NOT FOUND: " End program

13 Pseudocode Examples – Homework 4 Direct
Direct1 Read a line from input Print that line to output Direct2 For every line in input Print that line to output

14 Pseudocode Examples – Homework 4 Sum
Open the file "sum.txt" for reading Set sum to 0 Read a number from sum.txt this can be done with fscanf Add that number to sum Repeat until sum.txt runs out of numbers Open the file "result.txt" for writing Print sum to result.txt End program

15 Pseudocode Exercises – Groceries Shopping
You're given a list of things that you need to buy at the grocery store. The objective of the algorithm is get everything on your list and go to the cashier to pay for it. Common concepts available to you: For any one item on the list, it's clear where it is in the store Between any two points in the store, you know how to get from one to the other. You have access to a shopping cart which can hold more than you need. You cannot carry everything in just your hands, but it may be tempting to try.

16 Topics for Next Week's Exam
Program Execution Questions You'll be expected to look at C code and determine the state of the machine. Variable values Input read Output written Amount of memory allocated You'll be tested on your understanding of Numeric Representations Converting from Hex to Binary One's and Two's complements Comparing Numeric values of two different bases You'll be expected to know how lexical scoping works with functions.

17 Topics for Next Week's Exam
You should be familiar with writing Pseudocode. You should have a base understanding of gdb. Any of the following may be tested: Compiling a program for debugging. Attaching the debugger. Setting breakpoints. Running the program. Stepping during the program. Finding Errors in C programs. Logical and Syntactic. Most importantly, you should be very familiar with basic C syntax, definitions, and semantics.

18 Topics for Next Week's Exam
The last part of the program will be a Program on Paper. You will be given a program to write from scratch. It should have proper C syntax and should be as compiler-ready as if you had typed it in an editor. You will be expected to use your understanding of Pseudocode design the algorithm before you start writing the function. Partial credit will be awarded based on the quality of the Pseudocode and correctness of the program.


Download ppt "CS 240 – Lecture 11 Pseudocode."

Similar presentations


Ads by Google