100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.6 TrustPilot
logo-home
Other

2.2 Problem Solving and Programming

Rating
-
Sold
-
Pages
8
Uploaded on
11-09-2024
Written in
2023/2024

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.

Show more Read less
Institution
Course









Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Study Level
Examinator
Subject
Unit

Document information

Uploaded on
September 11, 2024
Number of pages
8
Written in
2023/2024
Type
Other
Person
Unknown

Subjects

Content preview

2.2 Problem Solving and Programming


2.2.1 Programming Techniques

Programming Constructs:

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
$4.92
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
maddysunter1
5.0
(1)

Also available in package deal

Get to know the seller

Seller avatar
maddysunter1
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
1 year
Number of followers
0
Documents
16
Last sold
5 months ago

5.0

1 reviews

5
1
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions