An Introductory Computer Science
Activity Book
2
Grace McClurg
What is Computer Science?
• Computer science is the study of computational systems with a specific
focus on software.
• Code consists of instructions that a computer can interpret and follow to
achieve designated goals.
• Software engineers collaborate to develop programming solutions that
help solve the problems facing our world!
I
Variables
• A variable labels and stores information used within a program.
• Imagine a variable as a labeled box, the contents of which may change
over time.
• In the example above, the label “My Favorite Thing” can be applied to
a variety of different objects. This kind of variable is referred to as an
untyped variable.
• On the other hand, a typed variable can only label data of a specific
type.
My Favorite
Butterfly
This typed variable accepts only objects that are of type butterfly.
Data Types
Integer Whole Number. -1, 4
Double Decimal Number. -1.5, 4.2, 6.0
Character Single Text Symbol. ‘a’, ‘A’, ‘1’, ‘!’
Typically distinguished using single quotation marks.
String Zero or More Text Symbols. “ink”, “1234”,
Typically distinguished using double quotation marks. “!”, “Our cat”
Boolean Either True or False. True, False
Fill in the color by number below according to data type:
Integer Double Character String Boolean
Red Orange Yellow Blue Green
42
‘!’
12.9
“Learning”
-4.3 “40”
‘m’
-4 ‘6’ 32
1.2
3.1
‘L’
True
“Let’s False
Explore!”
Conditions
• A condition encodes how a computer is instructed to make
decisions.
• A computer executes different instructions depending on whether
the given condition is true or false.
• Color the flowers below based on the condition tree.
TRUE Color Pink
Color Blue
TRUE
IF the flower is the
largest of all the
IF the flower petals
flowers shown FALSE have squiggly edges FALSE Color Purple
rather than smooth
Compound Conditions
• A compound condition can be used to evaluate more complex
scenarios when multiple factors influence the decision that must be
made.
• Multiple individual conditions can be connected using and and or.
AND OR
Both conditions must be true for the At least one of the conditions must
compound condition to be true. be true for the compound condition
to be true.
AND
To enter Panda’s Castle, we
need the door to be unlocked
and the bridge to be down.
OR
To enter Sea Otter’s Castle,
we need at least one door to
be open.
For Loops
• A loop is a sequence of instructions set to repeat.
• For loops are a convenient way to make a loop repeat a fixed
number of times.
• For example, let’s say you need to bake multiple cakes that each
require the same number of cups of flour.
How we use for loops in code:
1 int totalCakes = 3;
2 int flourCupsPerCake = 2;
3 for(totalCakes number of times){
4 for(flourCupsPerCake number of times){
5 **Add a Cup of Flour**
6 }
7 **Add Other Ingredients**
8 }
Explanation:
Lines 1-2: We create the integer variables totalCakes and flourCupsPerCake to establish the
required number of cakes and the amount of flour cups needed per cake.
Lines 3-8: The inner portion of the outer loop (lines 4-7) runs totalCakes number of times. The
opening and closing brackets define the scope of the loop.
Lines 4-6: The contents of the inner loop (line 5) run flourCupsPerCake number of times for each
iteration of the outer loop.
Bonus Question:
How many total times does
the contents of the inner for
loop run in the above code?
Answer: 6. For each cake, the inner loop runs 2 times. There are 3 cakes. Thus: 3×2=6
While Loops
• A while loop is used when a condition indicates when the loop
should stop repeating.
• For example, let’s say we have a turtle who is walking over to a
delicious strawberry. He doesn't know the exact number of steps
required to reach the strawberry, but he knows to stop once he
reaches it.
• In this case, whether he has reached the strawberry is the condition
we evaluate.
While (Turtle has NOT
reached the Strawberry) {
Step
}
Step
Reached Strawberry? FALSE
Step
Reached Strawberry? FALSE
Step
Reached Strawberry? TRUE
EXIT LOOP
Recursion
• Recursion is a way to solve a problem by breaking it down into
smaller instances of the same problem.
• We keep breaking the problem down until we reach a base case
where the problem is simple enough that we already know what the
answer is.
• From this point, we can trace our solutions all the way back up to
the original question.
How many wings are there for 4 birds? Answer: 8 wings.
= 2 + Number of Wings there are for 3 Birds
= 2 + Number of Wings there are for 2 Birds
= 2 + Number of Wings there are for 1 Bird
=2
We trace our way down until we get to the base case for 1 bird. Then, we
can trace back up since each answer resolves the above question.
Data Structures: Arrays
• A data structure is a container that can store and organize groups
of data.
• An array is a specific type of data structure. Think of it like a carton
of eggs, where each egg represents information needed to be stored.
Each piece of information in an array is called an element.
• Similar to an egg carton, an array has a fixed number of spaces. You
can easily access any element in an array regardless of its position.
Inserting a new egg to spot 9 is quick!
However, adding or removing an egg from any other spot
would require shifting the other eggs to maintain the same
ordering.
4
2 3
1
0
7 8 9
6
5
Linear vs Binary Search
• When searching for a specific element in an array, you can check
each element sequentially until you find your target. This is called
linear search.
• However, if the array is sorted, a faster approach called binary
search can be used.
• Binary search repeatedly divides the search range in half by
comparing the value you are seeking with the middle element of the
available positions.
Find the dog named Frank who has 5 spots.
Because 5 > 4, focus on dogs to the right.
1 Spot 3 Spots 4 Spots 5 Spots 6 Spots 8 Spots
Eliminated Possibilities Because 5 < 6, focus on dogs to the left.
1 Spot 3 Spots 4 Spots 5 Spots 6 Spots 8 Spots
Eliminated Possibilities Eliminated Possibilities
1 Spot 3 Spots 4 Spots 5 Spots 6 Spots 8 Spots
Frank is
Found!
Quick Sort Algorithm
• Quick Sort is one of the most efficient algorithms for sorting an
array of values!
• Let’s say we wanted to sort flowers in order from those with the
least to the most petals.
• We pick the last flower in the list to be the pivot. We find its spot in
the sorted array by finding all the flowers that should go after it and
those that should go before it. We then repeat this process on the
subarrays.
Pivot
1 7 5 2 3
Spot 1 Spot 2 Spot 3 Spot 4 Spot 5
Less than 3 Petals Greater than 3 Petals
Pivot
Pivot
7 5
1 2
Less than 2 Less than 5 Greater than 5
Greater than 2
Petals Petals Petals
Petals
1 7
Data Structures: Linked Lists
• A linked list is like an array; however, there is no preset size.
Instead, the data is interconnected through links!
• A linked list allows for faster insertion and deletion of elements
compared to an array. However, accessing items in a linked list is
slower because you need to trace through the links rather than
directly access an item's position.
A linked list is similar to linking hands! Visualize the linking and
unlinking that must happen when someone exits or enters the chain.
Data Structures: Queues and Stacks
Queue
Storage system where the order in which data is accessed is based
on which data was added first. This data structure operates like a
line. FIFO—First In, First Out.
Stack
In a stack, the order in which data is accessed is based on which
data was added last. This data structure operates like a stack of
books. FILO—First In, Last Out
Below, there are four examples of stack and queue data structures. Color the
bunnies using a stack brown and the ones using a queue gray.
Queue: Line, Escalator. Stack: Cart Rack, Carrot Stand
Data Structures: Graphs
• A graph describes data stored as points, which we refer to as nodes,
that have connections called edges.
• Graphs can represent various complex relationships between data.
For instance, a graph can depict roads connecting locations.
• Weights can be assigned to each edge to represent the cost of
traversing it. These weights can indicate the distance between two
places, or the time taken to travel along the edge, considering the
speed limit.
Find the path between the museum and the ice cream shop that has the lowest
total cost of traveling. The cost is calculated as the sum of the weights of all
the edges in the path.
School
4 Pool
2
Museum
6
5
30
10
Post Office
Library 2
Ice Cream Shop
Park
Museum, Post Office, Ice Cream Shop (Total Cost: 7)
BFS and DFS
• Breadth First Search (BFS) and Depth First Search (DFS) describe
ways to travel through a graph.
• BFS explores all the nodes at the present depth before moving to
nodes at the next depth level.
• DFS explores as far down a branch as possible before backtracking.
BFS DFS
A 1
A
A
B 2 C 5
D 3 E 4 F 6 G 7
Mr. Mole loves to adventure by looking for
jewels while digging!
In the graph of tunnels above, the letter
associated with each node indicates the BFS
ordering, while the number indicates the
DFS ordering.
Computer Science Application:
Space Exploration
• To contribute to the exploration of space, computer scientists have
aided in the development of technology related to spacecraft design,
astronomy models, celestial mechanics, and data processing tasks.
• Margaret Hamilton, a computer scientist who led the Software
Engineering Division of the MIT Instrumentation Laboratory, was a
leader in developing flight software for NASA’s Apollo program.
Computer Science Application:
Computer Vision
• Computer vision involves a computer’s ability to understand
information from visual inputs including images and videos.
• This technology can be used for self-driving cars, crop monitoring,
cancer screening, and many more applications!
• For example, this technology can be used for monitoring
populations of endangered animals to ensure their safety.
Computer Science Application:
Medicine and Healthcare
• Computer science can enable the development of powerful
healthcare technology!
• Bioinformatics can help us understand structures as complicated as a
genome, which is the complete set of DNA in an organism.
• Software engineering is also used in the development of surgical
technology, medical imaging, and electronic health records.
Computer Science Application:
Computer Graphics
• Computer graphics describes generating images using a computer.
• This field can be used for creating animations and 3D models for
entertainment, engineering, medical, architectural, and physics
purposes!
• Computer scientists can also create algorithms to support computer
graphics. For example, the boids framework can be used to generate
realistic flocks of animals in animated films, such as a school of
fish!
Computer Science Application:
Assistive Technology
• Computer science can enable the development of powerful
technologies used for healthcare applications!
• Data analytics can help us understand constructs as complicated as
genomes. Software engineering is also used in the deveqq4lopment
of surgical technologies and medical imaging.
Computer Science Application:
Assistive Technology
• Assistive technology includes assistive, adaptive, and rehabilitative
devices for the elderly and people with disabilities.
• Software engineers can help develop assistive technology such as
electronic pill dispensers, speech-to-text programs for those that are
deaf and hard of hearing, text-to-speech programs for those that are
blind or have low vision, and eye-tracking software for alternative
communication devices.
Coloring Computing
Coloring Computing is an educational coloring book that
introduces computer science through beautiful and interactive
illustrations.
This work creatively teaches readers different applications of CS,
as well as concepts including variables; loops; conditions; data
structures including linked-lists, arrays, graphs, queues, and stacks;
searching and sorting algorithms; and recursion.