This is the topic: 2.2 Problem Solving and Programming for the OCR Computer Science (H446) course. I got 4 A*s in my A-Levels (Computer Science, Physics, Maths, Further Maths) , so they are very detailed and cover all of the specification for this topic.
Casting is when you convert one data type to another data type. To convert to a real, we use float().
String concatenation is when strings are joined together.
MOD gives remainder, DIV gives whole division with no remainder.
Programming Constructs: These determine the order in which lines of code are executed.
Sequencing = Lines of code are run one line at a time, in the order they are written.
Selection/branching = The outcome of a condition determines which lines of code run next.
Iteration = Repeating lines of code using a loop. It can be count controlled (repeated a fixed
number of times), or condition controller (repeated until a condition is met).
Recursion:
Recursion = When a function calls upon itself. A recursive algorithm has three features:
The function must call itself
A base case: A condition which stops the recursion, so it can return a final value without
further recursive calls. This prevents stack overflow errors, resulting in program crashes.
A stopping case: this must be reachable after a finite number of times. If it doesn’t, it will call
itself indefinitely, using up excessive memory and causing the program to malfunction.
Recursion Iteration
Benefits Concise - Can often be expressed in a Performance – More efficient that
more concise way, especially for recursion, less memory usage
structures like trees. Debugging – Easier to understand and
Simple – Simply states what needs to be debug
done makes it more reusable and Wider application – More suitable to a
maintainable wider range of problems
Drawback Performance – Repeated function calls Complexity – Can get very complex and
s can be CPU and memory intensive, use more lines of code than recursive
leading to slower execution alternatives
Debugging – Recursive code can be much Less concise – Compared to recursive
more difficult to track the state of the alternatives, making them harder to
program understand
Limited application – Not all problems
are suited to recursive solutions
1
, Tracing a Recursive Function - Example:
We use a table:
1. When the function is called make
note of it (function call)
2. Write in what is passed to it (value)
3. When it returns a value, write what
it returns + an arrow downwards
4. Once the final call has been made,
work your way back u to get to the
final return (in this case, 29).
Global and Local Variables:
Global variable = A variable declared at the outermost level of a program. They have a global scope,
so they can be accessed or modified from any part of the
program. Uses:
Data sharing - They allow data sharing between different
parts of the program, allowing data to be passed
between different modules easily without parameter
passing.
Persistent storage - They retain their values throughout
the program’s execution, so they can store data that
needs to persist across function calls.
Global configuration - They can store configuration
settings or constants relevant across the whole program.
Local variable = A variable declared within the specific scope
they’re defined in (e.g. a subroutine). They’re only accessible
within the subroutine which they are defined. Once the
execution of the subroutine ends, the local variable is
destroyed, and its memory is freed. They can’t be accessed
externally unless passed as a parameter or returned from a
function.
Two variables can share the same name if they have different
scopes.
2
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller maddysunter1. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for £2.99. You're not tied to anything after your purchase.