PCSG Comp Sci Unit 1 Tutorial 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

PCSG Computer Science Unit 1 -

Tutorial 2
ALGORITHMS – PSEUDOCODE

NOVEMBER 4, 2022

STUDENT-HOST: JOSHUA LAYNE


Objectives
• Work through the solutions to Tutorial 1
• Answer any questions related to Pseudocode and Algorithms
Solutions to Tutorial 1
PCSG COMPUTER SCIENCE
Question 1:

Using Pseudocode, devise an algorithm for each question


prompt that chooses the largest number among three
integers that are inputted by the user
Using Pseudocode, devise an algorithm for each question prompt that
chooses the largest number among three integers that are inputted
by the user
START
DEFINE a, b, c, lg AS integer
PRINT “Enter 3 integers: ”
READ a, b, c
IF (a > b AND a > c) THEN
lg = a
ELSEIF (c > b AND c > a) STUDENT’S SOLUTION
lg = c
ELSE
lg = b
ENDIF
PRINT “Largest number = lg”
STOP
S-H’S SOLUTION START
DEFINE a, b, c AS integer
PRINT “Enter 3 numbers”
Q1: USING READ a, b, c
PSEUDOCODE, FOR 1 TO 3
DEVISE AN IF a > b AND a > c THEN
ALGORITHM FOR PRINT a
EACH QUESTION ELSE IF b > a AND b > c THEN
PROMPT THAT PRINT b
CHOOSES THE ELSE
LARGEST NUMBER PRINT c
AMONG THREE ENDIF
INTEGERS THAT ENDFOR
ARE INPUTTED BY STOP
THE USER
Question 2:

Using Pseudocode, devise an algorithm for each question


prompt that prints out numbers that are inputted if they are
less than the number that come before and after it
Using Pseudocode, devise an algorithm for each question prompt that
prints out numbers that are inputted if they are less than the number
that come before and after it
START
DECLARE numArray : ARRAY [1:5] of
integer
// Worked out in class
STOP STUDENT’S SOLUTION
START
Q2: USING DEFINE n, i, j AS integer
PSEUDOCODE, DECLARE array : ARRAY [1:n] OF INTEGER
PRINT “How many numbers do you want to enter: ”
DEVISE AN READ n
ALGORITHM FOR PRINT “Enter ” n “numbers: ”
EACH QUESTION i←0
PROMPT THAT FOR i TO n
READ array[i]
PRINTS OUT ENDFOR
NUMBERS THAT j←1
ARE INPUTTED IF FOR j TO n
THEY ARE LESS IF (array[j] < array[j - 1]) AND (array[j] < array[j + 1]) THEN
THAN THE PRINT array[j]
ENDIF
NUMBER THAT ENDFOR
COME BEFORE STOP
AND AFTER IT S-H’S SOLUTION
Question 3:

Using Pseudocode, devise an algorithm for each question


prompt that takes a number as input and prints it if it is a
prime number
Using Pseudocode, devise an algorithm for each question prompt that
takes a number as input and prints it if it is a prime number
START
DEFINE a, b AS integer
PRINT “Enter a number: ”
READ a
PRINT “Enter another number except 1: ”
READ b
IF (a / a = 1) AND (a / 1 = a) STUDENT’S SOLUTION
IF (a % b != 0)
PRINT “a is a prime number”
ELSE
PRINT “a is not a prime number”
ENDIF
ENDIF
STOP
START
DEFINE i, num, isPrime, limit AS integer
S-H’S SOLUTION PRINT “Enter a number: ”
READ num
limit ← squareroot(num)
i←2
FOR i TO limit
Q3: USING IF (num % i == 0)
isPrime ← 0
PSEUDOCODE, ELSE
DEVISE AN isPrime ← 1
ALGORITHM FOR ENDIF
EACH QUESTION ENDFOR
IF (isPrime == 0)
PROMPT THAT PRINT num “is not a Prime Number”
TAKES A NUMBER ELSE
AS INPUT AND PRINT num “is a Prime Number”
PRINTS IT IF IT IS ENDIF
A PRIME NUMBER STOP
Question 4:

Using Pseudocode, devise an algorithm for each question


prompt that takes 5 numbers as input and finds their total
and average
Using Pseudocode, devise an algorithm for each question prompt that
takes 5 numbers as input and finds their total and average
START
DECLARE num, total, i AS integer
DECLARE average AS float
i0
total  0
WHILE (I < 5)
PRINT “Enter an integer: ”
INPUT num
STUDENT’S SOLUTION
total = total + num
i=i+1
ENDWHILE
average = total / 5
PRINT “Average is ”, average
PRINT “Total is ”, total
STOP
START
S-H’S SOLUTION DEFINE total, num, i AS integer
DEFINE avg AS float
PRINT “Enter 5 numbers: ”
Q4: USING i←0
PSEUDOCODE, FOR 1 TO 5
DEVISE AN READ num
ALGORITHM FOR total ← total + num
EACH QUESTION i←i+1
PROMPT THAT ENDFOR
TAKES 5 avg ← total / 5
NUMBERS AS PRINT “The total is” total
INPUT AND FINDS PRINT “The average is” avg
THEIR TOTAL AND STOP
AVERAGE
Question 5:

Using Pseudocode, devise an algorithm for each question


prompt that calculates a running sum of numbers that the
user will enter and that will be added to the sum; but when a
negative number is encountered, stop adding the numbers
and print the final result.
Using Pseudocode, devise an algorithm for each question prompt that
calculates a running sum of numbers that the user will enter and that
will be added to the sum; but when a negative number is
encountered, stop adding the numbers and print the final result.
START
DECLARE a, sum AS integer
sum  0
a1
WHILE (a > 0) STUDENT’S SOLUTION
PRINT “Enter number: ”
READ a
sum = sum + a
ENDWHILE
PRINT “Sum = ”, sum
STOP
Q5: USING S-H’S SOLUTION
PSEUDOCODE, START
DEVISE AN
ALGORITHM FOR DEFINE num, total AS integer
EACH QUESTION total ← 0
PROMPT THAT PRINT “Enter a number: ”
CALCULATES A
RUNNING SUM OF REPEAT
NUMBERS THAT THE READ num
USER WILL ENTER
AND THAT WILL BE total ← total + num
ADDED TO THE SUM; UNTIL (num < 0)
BUT WHEN A
NEGATIVE NUMBER
PRINT total
IS ENCOUNTERED, STOP
STOP ADDING THE
NUMBERS AND
PRINT THE RESULT.
Question 6: A certain cellular company provides the
following rates for voice calls.
First 10 minutes $1.50 per minute
Any additional minutes $0.50 per minute
Write an algorithm that reads an integer value
representing the number of minutes used for voice
calls, calculates and prints the cost of calls.
Solution:
START
DECLARE mins, add_mins AS integer
DECLARE cost AS float
PRINT “Enter duration of call: ”
READ mins
IF (mins <= 10)
cost = mins * 1.50
ELSE
add_mins = mins - 10
cost = (10 * 1.50) + (add_mins * 0.50)
ENDIF
PRINT “Cost of call = ”, cost
STOP
Question 7: A soft-drink manufacturer sells five soft-drink
flavours: bananas, cherry, mango, orange and pineapple.
The company knows that banana and pineapple are the
two best-selling flavours and is carrying out a poll to
determine which of these two flavours is the more popular
among its customers

Write an algorithm to find the MORE popular flavour and


the NUMBER of votes it obtained. Assume that 100
customers participate in the poll and that there is NO tie.
Solution:
START
DECLARE ban, pin AS integer
PRINT “Enter votes for banana: ”
READ ban
PRINT “Enter votes for pineapple: ”
READ pin
IF (ban > 50)
PRINT “Banana is the more popular flavour with”, ban, “votes.”
ELSE
PRINT “Pineapple is the more popular flavour with”, pin, “votes.”
ENDIF
STOP
Question 8: A primary school is conducting a survey
on the popularity of certain colours. Students are
asked to vote for any of four choices: red, blue,
green, none. If red, blue or green is not the
favourite, students vote for 'none’.
Write an algorithm to find and print the
(i) number of students that voted for EACH
of the colours: red. blue, green.
(ii) TOTAL number of students that voted for
red, blue or green.
Assume that on the day of the survey, 150 students
are present. Also assume that all votes are valid.
START ELSEIF (colour == 3)
b = b+ 1
DECLARE r, b, g, n, colour, total AS
integer ELSE
n= n + 1
PRINT “Menu:”
ENDIF
PRINT “1. Red”
ENDFOR
PRINT “2. Green”
total = r + g + b
PRINT “3. Blue”
PRINT “Number of students that
PRINT “4. None” voted for red = ”, r
PRINT “Enter favourite colour: ” PRINT “Number of students that
FOR 1 TO 150 DO voted for green = ”, g
READ colour PRINT “Number of students that
IF (colour == 1) voted for blue = ”, b
r=r+1 PRINT “Total votes for red, blue and
ELSEIF (colour == 2) green = ”, total
g=g+1
STOP
Flowcharts
PCSG COMPUTER SCIENCE – MODULE 2
Intro to Flowcharts
• What is a flowchart?
• A flowchart can be defined as a visual representation of an algorithm.
Flowcharts make use of various shapes and symbols to visually represent
structures that exist within an algorithm.
• Why is a flowchart useful?
• There are times when a visual representation might be more appropriate
and easier to display and understand than a worded one.

Source: Programming by Isaiah Carrington


Intro to Flowcharts
• Using a flowchart
• As mentioned, flowcharts use various symbols to convey different meanings.
Here in this introduction, we will look at the most common ones and their
meanings.

Source: Programming by Isaiah Carrington


The flowline
• This is a flow line (an arrow).
• These show how the program
is supposed to flow from one
shape to another.

Source: Programming by Isaiah Carrington


The Oval

• This shape is used for START


and STOP.
• These show where the
flowchart begins and ends.

Source: Programming by Isaiah Carrington


The Parallelogram
• This shape is used for INPUT
and OUTPUT.
• That is, whenever you want
the user to enter a value, or
you want to display a value to
the user

Source: Programming by Isaiah Carrington


The Rectangle
• This shape is used for
processes.
• These processes include
mathematical calculations and
variable assignment (giving a
value to variables).

Source: Programming by Isaiah Carrington


The Rhombus
• The Rhombus is the shape
used to evaluate a given
condition, and then do 1 of 2
results depending on the
condition’s result.

Source: Programming by Isaiah Carrington


Example
• For this example, we will make BEGIN
use of an algorithm problem… DECLARE number as type integer
that is, displaying only the FOR number = 1 TO 15 DO
even numbers within the range IF number % 2 == 0 THEN
of 1 and 15 inclusive which you
DISPLAY number
can see right here.
ENDIF
ENDFOR
END

Source: Programming by Isaiah Carrington


Source: Programming by
Isaiah Carrington
Any Questions?
PCSG COMPUTER SCIENCE – MODULE 2
Practice - Flowcharts
PCSG COMPUTER SCIENCE – MODULE 2
Practice
• Now we’ll be practising flowcharts by converting the pseudocode
questions we have done before, into flowcharts.
• I’ll be using an interactive whiteboard called a Jamboard so that you
all can write them, and I’ll present on the screen for those watching
later. I’ll also save this Jamboard and post it in the Discord later.
• https://jamboard.google.com/d/1nUgSuUwTSH05XURv06DKPjCNM
eBBrCxsQN0sXlQ3zJ0/edit?usp=sharing
Attached Jamboard Slides
PCSG COMPUTER SCIENCE – MODULE 2
FLOWCHARTS

You might also like