CP NOTE 1
Programming Logic and Control Structures
Displaying Numbers Based on Conditions
Pseudocode Exercises
For each pseudocode segment, determine the numbers displayed based on the conditions given.
1)
n ← 2 REPEAT UNTIL n ≥ 15 IF n MOD 4 = 1 DISPLAY n n ← n + 3Displayed Values: 5
2)
x ← 10 REPEAT UNTIL x < 1 IF x MOD 2 = 0 DISPLAY x x ← x - 3Displayed Values: 10, 4
3)
count ← 5 REPEAT UNTIL count > 25 IF count MOD 5 = 0 DISPLAY count count ← count + 4Displayed Values: 5, 25
4)
value ← 3 REPEAT UNTIL value > 20 IF value MOD 6 = 3 DISPLAY value value ← value + 5Displayed Values: 3
5)
num ← 12 REPEAT UNTIL num < 0 IF num MOD 3 = 2 DISPLAY num num ← num - 4Displayed Values: 8
Robot Navigation Logic
Robot Navigation Framework
A robot drone performs a scanning operation and stores distances in a list called
scanData.Safety verification requires checking two conditions:
At least one object is farther than 30 meters (indicating open space).
No object is closer than 5 meters (indicating danger).
Available Procedures:
CountAbove(numList, n): Returns count of numbers in
numListgreater thann.CountBelow(numList, n): Returns count of numbers in
numListless thann.
Safety Verification Code:
Correct segment for safety check:
isSafe ← (CountAbove(scanData, 30) > 0) AND (CountBelow(scanData, 5) = 0)Correct Answer: A
Student Eligibility for Honor Society
Eligibility Check Logic
Two rules determine if a student qualifies for an invitation:
Rule 1:
rule1 ← (NOT (gradeLevel < 11)) AND (gpa ≥ 3.5)Rule 2:
rule2 ← (gradeLevel < 11) OR (gpa < 2.0)
Example Calculation:
Given: gradeLevel = 10, gpa = 3.8:
Evaluate each rule for correctness:
Rule 1 Evaluation:
(NOT (10 < 11))=NOT true= false(3.8 ≥ 3.5)= trueCombined:
false AND true= false
Rule 2 Evaluation:
(10 < 11)= true(3.8 < 2.0)= falseCombined:
true OR false= true
Final Values:
rule1 = falserule2 = true
Correct Answer: C
Computational Thinking and Collaboration
Collaborative Development
Groups design mobile apps by dividing tasks among members to combine skills (graphics design, coding, testing) demonstrating teamwork advantages.
Benefits of collaboration include:
Diverse perspectives leading to innovative solutions.
Reduces biases and enhances creativity.
Algorithms and Program Structure
Algorithms: Step-by-step instructions for solving problems:
Sequencing: Order in which instructions are executed.
Selection: Making decisions with IF statements.
Iteration: Repeating sequences using loops.
Programming Constructs:
Variables: Store data values (e.g.,
x ← 5).Control Structures: IF-statements and loops guide program flow based on conditions.
Procedures: Group related code for readability and reusability (e.g., defining a procedure like
PROCEDURE triple(x)to return3 * x).
Data Structures:
Lists provide organized ways to store collections of items, indexed from 1 for AP exam purposes.
Operations include APPEND, INSERT, and REMOVE for list manipulation.
Error Handling in Programming
Error Types:
Syntax Error: Violations of programming language grammar.
Logic Error: Code runs without failure but gives an incorrect output.
Runtime Error: Program crashes during execution (e.g., dividing by zero).
Debugging Practices: Common techniques to find and correct errors include:
Using print statements to track variable states.
Applying test cases to verify expected outcomes.
Watching variable states during code execution.
Understanding Data and Algorithms
Data Representation:
Binary Numbers: Information stored as bits (0s and 1s).
Data Compression: Two types, lossless and lossy, adapted for efficient storage.
Procedural Abstraction: Hiding implementation details while maintaining the overarching functionality.
Sorting Algorithms:
Bubble Sort demonstrates basic sorting by repeatedly stepping through a list, comparing adjacent elements and swapping them if they are in the wrong order.
Search Algorithms:
Linear Search checks each element sequentially, while Binary Search requires sorted lists and halving search space upon each decision.
Final Thoughts
Programming is a complex but highly structured domain where collaboration, algorithms, and control structures play fundamental roles. Understanding key concepts such as logic structures, data representation, and error handling can enhance problem-solving skills in computational contexts. Effective collaboration fosters innovative solutions and diverse perspectives, ultimately improving the development process.