Exercise 2
Exercise 2
Possible Routes:
There are 4 x 2 = 8 possible routes
someone can take to reach City C from
City A passing through City B.
Explanation:
o There are 4 different roads from City A
to City B (represented by the four
lines).
o From each point where City B is
reached there are 2 different roads
leading to City C.
o Since we can choose any of the 4 roads from A to B and then any of the 2 roads from B to C
independently, we multiply the possibilities (4 x 2) to get the total number of routes (8).
Unit # 2
COMPUTATIONAL THINKING AND ALGORITHMS
LONG QUESTIONS
Give Long answers to the following extended response questions (ERQs)
Q1 Identify whether the given problems are Decision Problem, Counting Problem or Search
Problem. Write your answer in front of each problem given below:
(a) Does a given binary string have an even number of zeros?
(b) Flipping a coin result in Head or tails. I flip a coin 20 times, how many different
sequences of heads and tails are possible
(c) Does a certain Java program say “yes” to an empty input?
(d) How many ways can the letters of the word TRIANGLE be arranged?
(e) N-queens problem: where the goal is to place eight queens on a chessboard such that
no queen attacks any other
Ans:
(a) Does a given binary string have an even number of zeros?
Decision Problem: This problem asks for a yes/no answer (decision) based on the properties of
the input (binary string). It doesn't ask for counting the number of zeros or searching for a
specific string with even zeros.
(b) Flipping a coin result in Head or tails. I flip a coin 20 times, how many different sequences of
heads and tails are possible
Counting Problem: This problem asks for the total number of possible outcomes (sequences)
which is the stamp of a counting problem. It's not concerned with just deciding if a specific
sequence is possible (decision problem) or finding a specific sequence with some property
(search problem)
(c) Does a certain Java program say “yes” to an empty input?
Decision Problem: This problem asks for a yes/no answer based on the behavior of the Java
program with a specific input (empty input). It doesn't care about how many times it says "yes"
for different inputs (counting problem) or finding a specific input that makes it say "yes" (search
problem).
(d) How many ways can the letters of the word TRIANGLE be arranged?
Counting Problem: This problem asks for the total number of possible arrangements of the
letters in the word "TRIANGLE". It's concerned with the quantity of arrangements, not deciding
if a specific arrangement exists (decision problem) or finding a specific arrangement with some
property (search problem).
(e) N-queens problem: where the goal is to place eight queens on a chessboard such that no
queen attacks any other
Search Problem: The N-queens problem doesn't ask for a yes/no answer (decision) or the
number of solutions (counting). It asks you to find a valid arrangement (search) of queens on the
chessboard that meets the given criteria (no attacks). In this case, the search space is all possible
placements of queens, and the search aims to find a configuration that satisfies the no-attacking
condition.
Q2 A student has to take one course of physics, one of science and one of mathematics. He may
choose one of 3 physics courses (P1, P2, P3), one of 2 science courses (S1, S2) and one of 2
mathematics courses (M1, M2). In how many ways can this student select the three courses he
has to take?
Ans: The student can choose one physics course in 3 ways (P1, P2, or P3).
The student can choose one science course in 2 ways (S1 or S2).
The student can choose one mathematics course in 2 ways (M1 or M2).
Since these choices are independent of each other, we can multiply the number of ways to make each
choice to find the total number of possible selections.
Total ways = 3 (physics) x 2 (science) x 2 (mathematics) = 12 ways.
Therefore, the student can select the three courses in 12 ways.
Q3 Create an IPO chart which will accept the ages of four boys and calculate their total age and
average age. The program must display both the total age and the average age.
Ans: Here is an IPO chart for calculating the total age and average age of four boys:
Input (I)
Age of boy 1 (integer)
Age of boy 2 (integer)
Age of boy 3 (integer)
Age of boy 4 (integer)
Process (P)
1. Calculate the total age by adding the ages of all four boys.
2. Calculate the average age by dividing the total age by 4.
Output (O)
Display the total age of the four boys.
Display the average age of the four boys.
This IPO chart outlines the steps involved in the program. You can use this chart to write the code in any
programming language.
Q4 Create an IPO chart of a scenario that allows a user to enter in two numbers. The operation to
be performed is either addition, subtraction, multiplication or division and accordingly the
output should be given to the user.
Ans: IPO chart for Two-Number Calculator
Input (I)
First number (numeric)
Second number (numeric)
Operation (+, -, *, /) (character)
Process (P)
1. Validate the input:
o Check if both entered values are numbers.
o Check if the operation entered is one of +, -, *, /.
2. If input is valid, perform the chosen operation on the two numbers.
3. If input is invalid, display an error message.
Output (O)
Display the result of the operation on the two numbers.
If the input was invalid, display an error message indicating the issue