Big Idea 3
Algorithms and Programming
What Is an Abstraction?
An abstraction is a way to represent essential features without including background details or explanations in computer science.
Abstractions help to reduce complexity, enabling efficient design and implementation of complex software systems.
Example: When using Instagram, users interact with abstracted processes allowing them to focus on content instead of the technical workings behind the application.
Programmers leverage abstractions to hide coding details, focusing on solving current problems rather than worrying about lower-level code.
Machine Code and Abstraction
Computers understand binary machine code, which consists of numerical expressions, but is difficult for humans to use.
Example of machine code:
-10111000 10100011 00100001…High-level programming reduces the necessity of understanding machine code, enabling programmers to communicate in semi-human languages.
Historical Context
The first high-level language to use abstractions was COBOL, designed by Grace Hopper.
Programmers rarely work directly with machine code due to its complexity; abstractions allow for a more user-friendly experience.
Driving Analogy for Abstraction
Driving represents how abstraction works: a driver navigates a car without needing to understand engine mechanics, focusing instead on the road ahead.
Variables
Variables can change values and are modified using an assignment operator.
- Example:
-score = 7// sets value to 7.
-score = score + 3// new value becomes 10.Meaningful variable names improve code readability and reduce complexity.
- Example of a good variable name:score.
- Poor variable name:xdoes not convey context of value.
Abstraction Examples on the AP Exam
Different programming languages provide different levels of abstraction.
- High-level languages offer more abstractions than low-level languages.Example abstractions for the exam include:
- DISPLAY (expression)
- Description: Displays the value of expression, followed by a space.
- RANDOM (a, b)
- Description: Evaluates to a random integer from a to b (inclusive).
- Example:RANDOM(1, 3)yields possible values of 1, 2, or 3.These abstractions can be reused throughout the code, saving time and reducing errors.
Searching Algorithms
Linear Search
A linear search, or sequential search, checks each element in a list until the target element is found or the list ends.
Best-case scenario: Target is found on the first comparison.
Worst-case scenario: All elements must be checked.
- If the list has n elements, the maximum number of comparisons required is n.
- Example:
- For list:numList = [11, 35, 2, 1, 56, 76, 3, 33, 90, 180]
- Finding 11 takes 1 comparison.
- Finding 180 takes 10 comparisons (worst case).
Binary Search
A more efficient search method for sorted lists, reducing the number of needed comparisons by half each time.
A binary search compares the middle element of the list to the target value, eliminating half of the remaining elements.
Steps to perform binary search:
- Compare the target with the middle element of the list.
- If not equal, eliminate the half where the target cannot lie.
- Repeat until the target is found.
Programming Development
Flexibility of Programs
Programs can be created for various reasons, such as creative expression, personal curiosity, or problem-solving.
Example: Mark Zuckerberg developed Facebook as a localized platform for Harvard students, which adapted to a larger audience over time.
Growth in user numbers and adaptation illustrates programming flexibility.
Programming Design Steps
The first step is to plan and consult with stakeholders to address their concerns.
An iterative process for error-checking and coding aids in efficiently isolating problems.
Steps in programming include design, implementation, testing, debugging, and maintenance.
Flowcharts
Flowcharts visually represent algorithms using standardized symbols:
- Oval: Start/End
- Rectangle: Processing steps
- Diamond: Decision points
- Parallelogram: Input/Output messagesFlowcharts facilitate understanding of complex programming logic.
List Operations
Lists store and retrieve data, with elements accessed by index (starting at 1 in the AP exam).
- Example:
-animals = [Cow, Pig, Dog, Golden Bandicoot, Frog]Operations that result in errors should be noted:
- Accessing an index that doesn’t exist results in an index out of bounds error.
Common List Abstractions on the AP Exam
list[i]: Refers to the element at index i.
INSERT (list, i, value): Inserts value at index i, shifting subsequent elements.
APPEND (list, value): Adds value to the end of the list.
REMOVE (list, i): Removes the element at index i and shifts subsequent elements.
LENGTH (list): Calculates the number of elements in the list.
Procedures
Procedures are blocks of code that can be invoked by their name anytime in a program, similar to methods or subroutines in other programming languages.
Traversing a List
Traversing involves accessing each element of the list sequentially and is a key concept for implementing algorithms effectively.
Example code demonstrating traversal:
-For EACH item IN scores: DISPLAY itemthat iterates through thescoreslist and displays each element sequentially.