Pseudocode and Algorithms Flashcards
queue
FIFO data structure: ordered collection of items added at the rear and removed from the front. sequence depends on order inserted, size depends on number inserted
array
a finite, ordered set of elements of the same type. grouped under one identifier, each element addressed using index
index
used to reference EACH element of an array
elementary data type
data type built into a programming language e.g. VB, pascal, python
tuple
dynamic data structure: ordered set of elements not necessarily of the same type able to inc/dec in size
structural data type
collection of data (made up of a number of elements) of a specified type e.g. array, string, record
primitive data type
data type PROVIDED by a programming language
n-dimensional array
a finite, ordered set of elements of a specified type indexed by n integers
abstract data type
a data type created by the programmer rather than defined by the programming language.
logical description of how data is viewed and operations that can be performed on it
programmer decides how to build and implement it and uses data types from programming language, user just needs to know state and behaviour
uses data abstraction: encapsulation, hiding details and implementation from user
encapsulation
ensuring a subroutine is completely self contained and independent of any global variables
dynamic data structure
data structure (a COLLECTION of data of a specified type in memory) with the ABILITY to grow and shrink in size in run time, with the aid of a heap
heap
portion of memory which is AUTOMATICALLY allocated and deallocated space as required (dynamic data structures)
adv of dynamic data structures and built in dynamic data structure
- good for implementing data structures when you don’t know max size in advance (can have arbitrary max to prevent overflow but not necessary to allocate memory beforehand)
- if built in dynamic data structure- many methods and functions e.g. pop already written and can be used in other data structures
- good use of memory: only takes up storage required
disadv of dynamic data structures
- can cause OVERFLOW if exceeds max memory limit
- needs memory to store pointers
- harder to program
- often time consuming to search
static data structure
data structure (collection of data of a specified type in memory) which is fixed in size at compile time so can’t inc in size or free up memory whilst the PROGRAM IS RUNNING
adv static data structure
- suitable for storing a fixed number of items e.g. months of year
- size is known so easier to program
- no need for memory wasted on pointers
disadv static data structure
- size decided in advance and no more items can be added if full, irrespective if free space in memory
- wastes storage if items stored is small relative to size of data structure
algorithm
a sequence of steps / set of rules SPECIFYING how to solve a problem, including inputs outputs and processing, written in a way it can easily be translated into high level code and then machine code for execution by the computer if in context of programming.
execute in finite time and produce an expected output for given inputs
a good algorithm
- DESIGNED in such a way that others can UNDERSTAND and MODIFY if necessary
- CLEAR, PRECISELY stated steps with correct OUTPUT for any valid input
- should allow for invalid inputs
- should execute efficiently with as few steps as possible
- should TERMINATE at some point
pseudocode
- halfway between a program, with its structural functions and english, with a solution expressed in a way that can be easily translated, and easily readable WITH NO CONCRETE SYNTAX
- helps develop and outline algorithms and understand the functioning
input and output pseudocode
print(“what is your name”)
myname= input()
OR
myname= input(“what is your name”)
integer
whole number
float/real
number with a fractional part
boolean
only takes values of true or false
character
a letter, number or special character typically represented in ASCII
string
a number of characters, enclosed in single or double quote marks
how to round a number to 3 dp
round(mynum,3)
exponentials
y=x**5 or y= x^5
finding remainder
20 mod 3 = 2
dividing whole
20 div 2 = 10
find length of string
len(string) or stringx.length, returns index of first character, and -1 otherwise. assume 1 is first element of string
find ‘me’ in a string called firststring
firststring.find(me)
finds ASCII value of a character
ord(“a”)
chr(97) ?
finds the CHARACTER value represented by an INTEGER
“Johnny” + “Bates” - what does this do
concatentate strings
convert char to integer
int(“1”)
convert integer to string
str(123)
convert string to float
float(“123.4”)
date format
date(year, month, date)
printed: year-month-day
variables
identifiers (names) for memory locations whose contents will change throughout the course of the program
constants
values that never change WHILST THE PROGRAM is being run
why have constants
in a LONG complex program: no chance of accidentally changing a value by using the identifier for something else
how to define constants
CONST companyPhone = “0124638393”
why have meaningful var names
easier to follow and update
why have standards e.g. camelCaps for var names
less errors and inconsistencies
standards for var names
camelCaps, short meaningful names, caps for constants
program constructs
selection, sequence, iteration
sequence
two or more statements following one after the other. all statements are executed once in the order they appear
assignment statement
a statement where a value is assigned to a variable
selection
used to select which statement will be executed next, depending on some condition, formulated using a relational operator. some statements may not be executed as a result.
not equal to pseudocode
!=
selection eg
if..then..else, switch/case statement
what is a nested loop
a loop is placed within another allowing you to have several alternatives
why have a switch/case
useful when have several alternatives
switch case pseudocode
switch choice: case 1 : print.... case 2: .... case 3 : ... else print ("...") endswitch
which boolean operators take precedence
brackets then AND then OR
iteration
repetition, involves involves a loop to repeat a number of statements. It is either controlled by a condition or repeats a set number of times
iteration eg
while. ..endwhile
repeat. ..until
for. ..next
while…endwhile properties
expression controlling loop = boolean
expression tested at start of every loop :
so input to be tested should be at end of loop
repeat…until properties
boolean expression tested at end of program
done at least once
for…next properties
useful when know how many iterations to be performed
for…next pseudocode
for count = 2 to 12
product = 2 * count
print (“….”)
next count
how to generate a random number between 1 and 6
random(1, 6)
subroutines
a named block of code/SECTION OF THE PROGRAM performing a specific task within the program, used in a long complex program to break it into subroutines: helpful for designing a satisfactory solution. Used in modular programming
function aka inline
a subroutine which assigns a return value to a variable and can be used within expressions
procedure
called by just writing its name but does not assign a result to a variable
existing system function
len(x), int(“12”), input
procedure call vs procedure declaration
displayMenu = procedure call in main program
procedure displayMenu = procedure declaration
how to call a function in main program
option = getChoice //getChoice is function
print (option)
function getChoice ... choice = input() return choice endfunction
parameters
used to pass values or variables into a subroutine
byval
a copy of the actual value is passed and treated as a local variable so value outside the subroutine is not affected
byref
the address of the parameter is passed so if the value is change, it changes in the main program also
pseudocode byval vs byref
procedure abc(x, y: integer; var z: integer) if var then byref, so z is byref x, y is byval
global variable
declared at the top. its scope is the whole program, so it can be accessed throughout the program. it allows data to be shared between subroutines.