Algorithms (continued)
QUIZ
What are the 3 (or 4) fundamental control
structures that we use as building blocks for
algorithms?
Building blocks for algorithms
• Actions (input, output, computations)
• Decisions
• Loops (repetition)
QUIZ
What is pseudocode?
QUIZ
What are the symbols that we use in flowcharts?
Flowchart Symbols
Input/Output
Process step
or instruction
Conditional Test
(decision)
Start/Stop
Connector
Flowchart Rules
– Always have a Start and a
Stop
– Blocks are connected with
lines and arrows indicating
the direction of process
flow
– Flow should mainly be top
to bottom or left to right
What is printed by this pseudocode?
Set of purchase prices: {42, 100, 10}
Total_cost = 0
For each purchase:
tax = purchase_price * 0.08
Total_cost = purchase_price + tax
Print Total_cost
What is printed by this pseudocode?
Set of purchase prices: {42, 100, 10}
Total_cost = 0
For each purchase:
tax = purchase_price * 0.08
Total_cost = purchase_price + tax
Print Total_cost
Watch the indentation carefully!
Modify the pseudocode to get the correct algorithm
Hint: Use else
What does this algorithm print?
What does this algorithm print?
What does this algorithm print?
N = 20
While N > 0
Print N
if N is even
N = N/2
else
N = 3*N + 1
The algorithm does not terminate!
(infinite loop)
N = 20
Make a small
While N > 0
modification so it
Print N
terminates!
if N is even
N = N/2
else
N = 3*N + 1
What does this algorithm print?
N = 20
While N > 1
Print N
if N is even
N = N/2
else
N = 3*N + 1
Counting marbles
There are 20 marbles in a bag, some green,
some blue. The only action available to us is to
draw a marble (at random) and examine its
color.
Develop an algorithm to determine and print
the numbers of green and blue marbles.
There are 20 marbles in a bag, some green, some blue.
The only action available to us is to draw a marble (at
random) and examine its color.
Develop an algorithm to determine and print the
numbers of green and blue marbles.
• Imagine yourself doing the counting. What
would you write down on your scratchpad?
• What variables would a computer program
need to keep track of the same information?
There are 20 marbles in a bag, some green, some blue.
The only action available to us is to draw a marble (at
random) and examine its color.
Develop an algorithm to determine and print the
numbers of green and blue marbles.
What control structures would the computer
need to implement the algorithm (initializations,
loops, decisions)?
There are 20 marbles in a bag, some green, some blue.
The only action available to us is to draw a marble (at
random) and examine its color.
Develop an algorithm to determine and print the
numbers of green and blue marbles.
Write the pseudocode!
Pseudocode Solution
1.
2.
3.
4.
Initialize color counters: green = 1 and blue = 1
Initialize marble count: count = 1
Draw a marble
If the marble is green, increment the green count
green = green + 1
5. If the marble is blue, increment the blue count
blue = blue + 1
6. Are there any marbles left? (is count ≤ 20?)
1. If yes, increment marble count count = count + 1
and go to step 3
2. If no, then output green and blue.
7. The end
Assignment #4:
Draw the flowchart for this pseudocode!
1.
2.
3.
4.
Initialize color counters: green = 0 and blue = 0
Initialize marble count: count = 1
Draw a marble
If the marble is green, increment the green count green =
green + 1
5. If the marble is blue, increment the blue count blue = blue
+1
6. Are there any marbles left? (is count < 20?)
1. If yes, increment marble count count = count + 1 and go to
step 3
2. If no, then output green and blue.
7. The end
EOL 1
Your turn: Can you find the mistakes?
Hint: What would be printed if there were 20 green marbles?
Solution
What would be printed if there were 20 green marbles?
21 1
Conclusion: “off by one”
Corrected pseudocode
1.
2.
3.
4.
Initialize color counters: green = 0 and blue = 0
Initialize marble count: count = 1
Draw a marble
If the marble is green, increment the green count
green = green + 1
5. If the marble is blue, increment the blue count
blue = blue + 1
6. Are there any marbles left? (is count < 20?)
1. If yes, increment marble count count = count + 1
and go to step 3
2. If no, then output green and blue.
7. The end
Partial solution
START
green=0 blue = 0
count = 1
Select a
marble
Y
Is green?
N
Partial solution
START
green=0 blue = 0
count = 1
Select a
marble
Y
green = green + 1
Is green?
N
blue = blue + 1
Partial solution
START
green=0 blue = 0
count = 1
Select a
marble
Y
Is green?
green = green + 1
N
blue = blue + 1
count<20?
Y
N
START
green=0 blue = 0
count = 1
Select a
marble
count = count + 1
Y
Is green?
green = green + 1
N
blue = blue + 1
count<20?
Y
N
OUTPUT
blue, green
STOP
Assignment #4 (final):
There are 42 marbles in a bag, some green, some blue,
and some red. The only action available to us is to draw
a marble (at random) and examine its color.
Develop an algorithm to determine the number of R, G,
and B marbles:
– Write a pseudocode solution.
– Write a flowchart solution.
• Hint: We need two decision in the increment part of the
algorithm
Due Thursday, Oct.13
Is the test
performed at the
beginning or at the
end of the loop?
COSC 1100 Freshman Seminar Fall 2016
Source: AP CS Principles – Course and Exam Descriptions
Is the test
performed at the
beginning or at the
end of the loop?
COSC 1100 Freshman Seminar Fall 2016
Source: AP CS Principles – Course and Exam Descriptions
What is displayed?
COSC 1100 Freshman Seminar Fall 2016
Source: AP CS Principles – Course and Exam Descriptions
What is displayed?
A: Nothing – it is an infinite loop, b/c i never reaches 4!
COSC 1100 Freshman Seminar Fall 2016
Source: AP CS Principles – Course and Exam Descriptions
How can we fix the algorithm to avoid the infinite loop?
What is displayed?
A: Nothing – it is an infinite loop, b/c i never reaches 4!
COSC 1100 Freshman Seminar Fall 2016
Source: AP CS Principles – Course and Exam Descriptions
COSC 1100 Freshman Seminar Fall 2016
Source: AP CS Principles – Course and Exam Descriptions
COSC 1100 Freshman Seminar Fall 2016
Source: AP CS Principles – Course and Exam Descriptions
COSC 1100 Freshman Seminar Fall 2016
Source: AP CS Principles – Course and Exam Descriptions
© Copyright 2026 Paperzz