2.2 Problem Solving and Programming Flashcards
What is iteration?
Repeating code a set number of times or until a condition is met
What is selection/branching?
Taking different paths through the code based on a condition
What is sequence?
The order the instructions are in
What is a variable?
A space in memory allocated to store a value that can change and be recalled in the program
What is a constant?
Fixed values that do not change during run time
What are naming conventions?
Important when programming that you agree within a project team on suitable naming conventions
Variable and constant can’t start with a number or symbol and cannot contain spaces
What is recursion?
Where a subroutine calls itself
Method of solving a problem where the solution depends on solving increasingly smaller instances of the same problem
What are the characteristics of recursive algorithms?
Base case (stopping condition)
For any input values other than the one that meets the base case, subroutine should call itself
General case
Must have finite number of iterations or a memory error will occur (stack overflow)
What is a base case?
Condition to determine when the algorithm should stop calling itself
Point when you know the answer to that part of the problem
Where the problem has got as small as it can get and therefore starts to unwind
What is the stopping condition used to halt a recursive algorithm called?
Sentinel or rogue value
What is a general case?
Way of defining the problem which references a decreasing in size version of the problem
Statement that expresses the solution to a problem in terms of a reference to a smaller version of that same problem
What are the benefits of a recursive approach opposed to an iterative approach?
Can be CLOSER TO NATURAL LANGUAGE AND EASY TO UNDERSTAND
Can be SHORTER AND EFFICIENT
Can be good to iterate through a tree structure (good when algorithm needs to branch off e.g. a flood fill or tree)
Can provide a solution when iterative code is very long and complex
What are the drawbacks of a recursive approach opposed to an iterative approach?
NOT MEMORY EFFICIENT - lot of local variables
Uses stacks to store all the data and therefore if a recursive algorithm calls itself too many times then it may run out of memory on the stack
Some data will need to be stored for a long time until algorithm unravels
Can be slower to execute because of maintenance of stack that needs to go on during the execution
Can be DIFFICULT TO CODE AND DIFFICULT TO TRACE
How can a recursive algorithm be used in binary search trees?
On each comparison, sent left or right so values in other subtree don’t need to be searched
Tree must be balanced to be as efficient as binary search
What is a balanced tree?
Every leaf is around the same distance away from the root as any other leaf
Doesn’t need same number of nodes on each side but should be same length
What is an unbalanced tree?
One or more leaves are much further away from the root than other leaf
How can recursion be used for a binary search?
Algorithm returns True if value found and False if not
Takes two arguments: node (current node being examined) and search_item (item being searched for)
As data stored in tree, refer to each element as node rather than item
Node is structure containing data (simple value attached to node); right (reference to node to right); left (reference to node to left)
If no node to right, right value = Null
If no node to left, left value = Null
Where local variables declared?
Inside the subroutine
What is the scope of a local variable?
Cannot be accessed outside the subroutine
What happens to a local variable when the subroutine has finished executing?
Value in local variable deleted and cannot be recalled - meaning memory used only during subroutine then deleted
How do subroutines access local variables?
Variables have to be passed/returned between subroutines
Can local variables be named the same?
Yes, if they are in different subroutines
Where are global variables declared?
Outside of the subroutine
What is the scope of global variables?
Can be access from inside any subroutine
What happens to a local variable when the subroutine has finished executing?
Value remains in the variable - stored in memory throughout the entire program
When do global variables require memory?
Memory allocated at the beginning of the program and throughout the program
What is a problem with using global variables?
Makes integrating modules tricking as the other module may have a global variable of the same name
If there are two variables with the same name (one local and one global), which one will data be written to unless specified?
Local
What are the different ways that data can be passed into a subroutine?
Passing by value
Passing by reference
What is passing by value?
A copy of the value is sent as a parameter and when changes are made inside the subroutine, they don’t change the original value
Creates increased demand on RAM
Parameter has local scope
What is passing by reference?
A pointer to the memory location holding the value is passed as a parameter and when changes are made inside the subroutine, they also change the original value
What are subroutines?
Named section of code that can be called by writing the subroutines name in a program statement
Makes the code more readable and usable as the code is broken into smaller sections