0% found this document useful (0 votes)
12 views19 pages

Pseudocodes

The document provides an overview of pseudocode and programming concepts, including definitions of variables, constants, and data types, as well as structured statements for sequence, selection, and loops. It explains how to declare variables and constants, perform arithmetic operations, and manage input/output operations. Additionally, it covers the use of arrays and offers guidelines for using pre-release material in programming tasks.

Uploaded by

tinasherufudza18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views19 pages

Pseudocodes

The document provides an overview of pseudocode and programming concepts, including definitions of variables, constants, and data types, as well as structured statements for sequence, selection, and loops. It explains how to declare variables and constants, perform arithmetic operations, and manage input/output operations. Additionally, it covers the use of arrays and offers guidelines for using pre-release material in programming tasks.

Uploaded by

tinasherufudza18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Computer Science 2210

Sec 2.1.2 Pseudocodes & Programming Concepts


with Majid Tahir

PSEUDOCODE:

Pseudo code is an outline of a program, written as a series of instruction using simple English
sentences.
Pseudo code uses keywords commonly found in high-level languages and mathematical notation. It
describes an algorithm‟s steps like program statements, without being bound by the strict rules of
vocabulary and syntax of any particular language, together with ordinary English.

Variable:
Variable is memory location where a value can be stored.

Constants:
Just like variables, constants are "dataholders". They can be used to store data that is needed
at runtime.
In contrast to variable, the content of a constant can't change at runtime, it has a constant value.
Before the program can be executed (or compiled) the value for a constant must be known.

Arithmetic
Use the arithmetic operators.

Assignment
Assignment is the process of writing a value into a variable (a named memory location). For
example, Count ← 1 can be read as „Count is assigned the value 1‟, „Count is made equal to 1‟
or „Count becomes 1‟.

Initialization:
If an algorithm needs to read the value of a variable before it assigns input data or a calculated
value to the variable, the algorithm should assign an appropriate initial value to the variable,
known as Initialization.

Input
We indicate input by words such as INPUT, READ or ENTER, followed by the name of a
variable to which we wish to assign the input value.

Output:
We indicate output by words such as OUTPUT, WRITE or PRINT, followed by a comma-
separated list of expressions.

Totaling
To keep a running total, we can use a variable such as Total or Sum to hold the running total
and assignment statements such as:
Total ← Total + Number
ADD Number to Total

Counting
It is sometimes necessary to count how many times something happens.
To count up or increment by 1, we can use statements such as:
Count ← Count + 1
INCREMENT Count by 1

1
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Structured statements
In the sequence structure the processing steps are carried out one after the other. The
instructions are carried out in sequence, unless a selection or loop is encountered.

Operator Comparison
> Greater than
< Less than
>= Greater than equal to
<= Less than equal to
= Equals to
<> Not equal
() Group
AND And
OR Or
NOT not

Data types

The following table shows the Visual Basic data types, their supporting common language runtime types,
their nominal storage allocation, and their value ranges.

Basic Data Types


A variable can store one type of data. The most used data types are:

2
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Declaration of Variables and Constant:

The process of creating a variable is called declaring a variable. Each declaration needs 4 things:

VB code example:
• DECLARE keyword
• Variable name
• AS keyword
• Variable data type

DECLARE variable As Datatype

Declaring Multiple Variables

DECLARE index As Integer


DECLARE grade As Integer
DECLARE counter As Integer

The three declarations above can be rewritten as one declaration:

DECLARE index, grade, counter As Integer

Constants

Creating Constants in Pseudocode is just writing costant name and value with it. In contrast to variable,
the content of a constant can't change at runtime, it has a constant value.

CONSTANT <identifier> = <Value>


CONSTANT Pi 3.1415 or CONSTANT Pi = 3 .14

3
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Type of Programs:

 Sequence
 Selection
 Repetitions/Loops

Sequence
Statements are followed in sequence so the order of the statements in a program is important.

Assignment statements rely on the variables used in the expression on the right-hand side of
the statement all having been given values. Input statements often provide values for
assignment statements. Output statements often use the results from assignment statements.

BEGIN VB code example

DECLARE number1 As Integer


DECLARE number2 As Integer
DECLARE sum As Integer
DECLARE product As Integer

PRINT (“Enter number 1”)


INPUT number1

PRINT (“Enter number 2”)


INPUT number2

Sum number1 + number2


product number1 * number2

PRINT (“the sum is”)


PRINT (sum)

PRINT (“the product is”)


PRINT (product)

END

4
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Structured statements for selection (conditional statements)


These statements are used to select alternative routes through an algorithm; selection‟s logical
expressions often involve comparisons, which can operate on text strings as well as numbers.
• IF…THEN…ELSE…ENDIF
• CASE…OF…OTHERWISE…ENDCASE

IF…THEN…ELSE…ENDIF
For an IF condition the THEN path is followed if the condition is true and the ELSE path is
followed if the condition is false.
There may or may not be an ELSE path. The end of the statement is shown by ENDIF.
A condition can be set up in different ways:

IF ((Height > 1) OR (Weight > 20) OR (Age > 5)) AND (Age < 70)
THEN PRINT "You can ride"
ELSE PRINT "Too small, too young or too old"
ENDIF

CASE … OF … OTHERWISE … ENDCASE


For a CASE condition the value of the variable decides the path to be taken. Several values are
usually specified. OTHERWISE is the path taken for all other values. The end of the statement
is shown by ENDCASE.
The algorithm below specifies what happens if the value of Choice is 1, 2, 3 or 4.

CASE Choice OF
1: Answer ← Num1 + Num2
2: Answer ← Num1 - Num2
3: Answer ← Num1 * Num2
4: Answer ← Num1 / Num2
OTHERWISE PRINT "Please enter a valid choice"
ENDCASE

5
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

The IF THEN statement

BEGIN

DECLARE grade As Integer

PRINT ("Enter your grade")

INPUT grade

IF grade > 50
THEN PRINT ("You have passed")
ELSE PRINT (“You have failed”)
END IF

END

IF THEN, ELSE-IF statements VB code example

BEGIN

DECLARE grade As Integer

PRINT ("Enter a grade")

INPUT grade
IF grade > 80
THEN PRINT ("Grade A")
ELSE IF grade > 60
THEN PRINT ("Grade B")
ELSE IF grade > 50
THEN PRINT ("Grade C")
ELSE PRINT ("Grade U")
END IF
END IF
END IF

END

The IF statement is useful, but can get clumsy if you want to consider “multi-way selections

6
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

CASE OF OTHERWISE…

Pseudo code VB code example

BEGIN
DECLARE grade As Integer
PRINT ("Enter your grade")
INPUT grade

CASE grade OF
grade > 80
PRINT ("Grade A")
grade > 69
PRINT ("Grade B")
grade > 59
PRINT ("grade C")
grade > 39
PRINT ("grade E")
grade < 39
PRINT ("grade U")
OTHERWISE
PRINT ("Error, enter again")
END CASE
END

LOOPS (Structured statements for iteration (repetition)


Many problems involve repeating one or more statements, so it is useful to have structured
statements for controlling these iterations or repetitions. Exit conditions consist of logical
expressions whose truth can be tested, such as Count = 10 or Score < 0. At a particular time, a
logical expression is either True or False.

• FOR…TO…NEXT
• WHILE…DO…ENDWHILE
• REPEAT…UNTIL

FOR … NEXT LOOP


This is to be used when loop is to be repeated a known fixed number of times.
The counter is automatically increased each time the loop is performed.

For COUNT = 1 to 10
Input NUMBER
TOTAL = TOTAL + NUMBER
Next COUNT

7
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

WHILE … Do LOOP
This loop is used when we don not know how many times the loop is to be performed. The Loop
is ended when a certain condition is true.
This condition is checked before starting the loop.

While COUNT < 10 DO


Input NUMBER
TOTAL = TOTAL + NUMBER
COUNT = COUNT + 1
Endwhile
Output TOTAL

REPEAT … UNTIL LOOP


REPEAT UNTIL Loop is used when we do not know how many times loop will be performed.
The Loop is ended when a certain conation is true.
The Condition is checked at the end of the Loop and so a REPEAT Loop always has to be
performed at least once.

REPEAT
Input NUMBER
TOTAL = TOTAL + NUMBER
COUNT = COUNT + 1
Until COUNT = 10
Output Total
FOR loop

The fore loop repeats statements a set number of time. It uses a variable to count how many time it goes
round the loop and stops when it reaches its limit.

BEGIN
DECLARE index As Integer

FOR index = 1 To 20
PRINT (index & “times 5 is" & index * 5”)
NEXT VB code example:

8
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Other examples of FOR loop


BEGIN
DECLARE BiggestSoFar, NextNumber, Counter As Integer

INPUT BiggestSoFar

FOR Counter 1 TO 5

INPUT NextNumber
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar NextNumber
ENDIF

END FOR

OUTPUT (“The biggest number so far is” & BiggestSoFar)


END

Sample VB Code of above Pseudocode:

9
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

WHILE DO ENDWHILE loop

The wile look is known as a test before loop. The condition is tested before entering the loop, but tested
each time it goes round the loop. The number of times the statements within the loop are executed
varies. The test before loop goes round 0 or more times.
This method is useful when processing files and using “read ahead” data

VB Code example
BEGIN
DECLARE name As String

INPUT name

WHILE name <> "x"


PRINT (“Your name is: “name)
INPUT name
END WHILE
END

REPEAT UNTIL loop

The repeat loop is similar to the while loop, but it tests the condition after the statements have been
executed once. This means that this test after loop goes round 1 or more times.

VB code example
BEGIN
DECLARE name As String

REPEAT
INPUT name
PRINT (“Your name is:” name)
UNTIL name = "x"

END

Keeps inputting name and keeps printing name until user enters “X”

10
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Array Data Type


An array is a special variable that has one name, but can store multiple values. Each value is stored in an
element pointed to by an index.
The first element in the array has index value 0, the second has index 1, etc

One Dimensional Arrays


A one dimensional array can be thought as a list. An array with 10 elements, called names, can store 10
names and could be visualized as this:

Arrays (One-dimensional arrays)


In order to use a one-dimensional array in a computer program, you need to consider:

• What the array is going to be used for, so it can be given a meaningful name
• How many items are going to be stored, so the size of the array can be determined.
• What sort of data is to be stored, so that the array can be the appropriate data type.

This array would be created by:


VB code example:

DECLARE names (9) As String

PRINT (names (1))

will display James

PRINT (names (7))

Will display Mathew

11
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Entering Values in One-Dimension Array

BEGIN

DECLARE index As Integer


DECLARE names (10) As String // for declaring 10 elements in ARRAY
DECLARE grades (10) As Integer

FOR index = 1 to 10 // for inputting 10 names and grades


PRINT (“Enter Name “& index)
INPUT names (index)
PRINT (“Enter grade for “& names (index))
INPUT grades (index)
NEXT index
// for displaying 10 names and grades
FOR index 1 to 10
PRINT (names (index) & “has grade " & grades (index))
NEXT index
END

12
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Using Pre-Release Material in Programming


In order to answer practical questions based on pre-release material, you will need to practise the skills
you have learnt so far. The pre-release material will arrive a few months before your examination; you
can discuss it with your teacher and your fellow students.

You need to practise applying your skills to the tasks mentioned in the scenario, which is different for
each examination series.

Here is a checklist of useful things to do:


.
1. Read through the pre-release material several times. Check with your teacher if there is
anything at all that you do not understand.

2. For each task, write an algorithm using both pseudocode and a flowchart to show what
is required.

3. Choose sets of test data that you will need to use, and work out the expected results.
Remember to use normal, boundary and erroneous data. Be able to give reasons for
your choice of test data.

4. Complete trace tables to test your pseudocode and flowcharts. This will enable you to
ensure that both the pseudocode and the flowcharts work properly. It is a good idea to
get another student to trace your algorithms as well.

5. Decide which works best for each task, pseudocode or a flowchart, and why.

6. Before starting to write your program for each task:


a. decide the variables, including any arrays, and constants you will need
b. decide the data types required for these
c. decide the meaningful names you will use
d. be able to explain your decisions.

7. If you are asked to repeat the same thing many times, for example adding up totals,
complete the task for one and check it works before repeating it many times.

8. Write and test each task. You can use the same test data as you used for your
pseudocode and flowcharts.

13
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Pre Release Material


Summer 2017 | O Levels | Computer Science
In Preparation for the examination candidates should attempt the following practical tasks by
Writing and testing a program or programs.

 The Organizer of a senior citizens‟ club has arranges outings for the members. For each
of these outings a coach is hired, meals at a restaurant are reserved and tickets for the
theater has booked. A program is required to work out the costs and provide a printed
list showing everyone on the outing.
 Write and test a program for the club organizer.
 Your program must include appropriate prompts for the entry of data.
 Error message and other output need to be set out clearly.
 All variables, constants and other identifiers must have meaningful names.
You will need to complete these three tasks. Each task must be fully tested.

Task 1:
The organizer finds out how many senior citizens are interested in the outing. The program for
Task 1 works out the cost for the information.

The minimum number of senior citizens needed for the outing to go ahead is 10; there cannot
be more than 36 senior citizens on the outing. A minimum of two carers go on the outing. With
an additional carer needed if more than 24 citizens go on the outing. Carers do not have to pay
anything for the outing. Workout the total cost per person for the senior citizens.

Task 2:
Record who is going on the outing and how much has been paid.
Using your results from Task 1, record the names of the people on the outing and the amount
they have paid; include the carers on the outing. If there are spare places on the coach then
extra people can be added; they are charged the same price as the other citizens. Calculate the
total amount of money collected. Print out the list of the people on the outing.

Task 3:
Identify the break-even point or profit that will be made on the outing.
Show whether the outing has made a profit or has broken even using the estimated cost from
the Task 1 and money collected from Task 2.

14
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Solution (Pseudo Code)

BEGIN
DECLARE citizen, people As integer //declaration of variables
DECLARE total, cost_per_person As real

CONSTANT coach1 = "150" // declaration of constants


CONSTANT coach2 = "190"
CONSTANT coach3 = "225"

CONSTANT meal1 = "14"


CONSTANT meal2 = "13.50"
CONSTANT meal3 = "13"

CONSTANT ticket1 = "21"


CONSTANT ticket2 = "20"
CONSTANT ticket3 = "19"

People 0, total 0, cost_per_person 0 //initialization

OUTPUT ("ENTER HOW MANY senior citizens want to go for outing”)


INPUT citizen
WHILE (citizen < 10 OR citizen > 36)
OUTPUT (“The minimum number of senior citizens for outing are 10;
there cannot be more than 36 senior citizens on the outing.”)
OUTPUT (“Enter in numbers how many senior citizens are interested
in the outing”)
INPUT citizen
ENDWHILE
OUTPUT ("Total numbers of citizens are:" & citizen)

IF citizen > 24 THEN people = citizen + 3 ELSE people = citizen + 2


CASE people OF
people <= 16
total = coach1 + (meal1 * people) + (ticket1 * people)
costperson = total / citizen
people <= 26
total = coach2 + (meal2 * people) + (ticket2 * people)
costperson = total / citizen
people <= 39
total = coach3 + (meal3 * people) + (ticket3 * people)
costperson = total / citizen
END CASE
PRINT("Total number of people:", & people)
PRINT("Total cost accumulated:", & total)
PRINT("Cost per peson:", & cost_per_person

15
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

DECLARE name(39) As string //deaclaration of arrays


DECLARE amount(39) As real
DECLARE seats, updated_people, count As integer
DECLARE extra_amount, total_amount, amount_paid As real

Seats 0, updated_people 0, extra_amount 0,


total_amount 0, amount_paid 0

FOR count = 1 to people


OUTPUT (“Enter the name of person on the outing”)
INPUT name(count)
OUTPUT (“Enter the amount paid by the person”)
INPUT amount(count)
NEXT
OUTPUT (“If there are spare places on the coach then extra people can be added; they are charged
the same price as the other citizens.”)
IF people >= 12 AND people <= 16 THEN
Seats 16 – people
extra_amount cost_per_person * seats
ENDIF

IF people >= 17 AND people <= 26 THEN


Seats 26 - people
extra_amount cost_per_person * seats
ENDIF
IF people >= 26 AND people <= 39 THEN
Seats 39 – people
extra_amount cost_per_person * seats
ENDIF
total_amount total + extra_amount
Updated_people people // if there will be no extra people
IF seats >= 1 THEN
updated_people people + seat // if there are extra people
OUTPUT(“seats available for : ”, seats, person(s), Enter Name and Amount for people
will to go for outing”)

FOR d count to updated_people //appending the same arrays


OUTPUT (“Enter the name of person on the outing”)
INPUT name(d)
OUTPUT (“Enter the amount paid by the person”)
INPUT amount(d)
NEXT
ENDIF
FOR count 1 to updated_people
OUTPUT (“The Citizens’ Name :”, name(c) ,” & the amount submitted :”, amount(c))
NEXT

16
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

DECLARE profit As real

profit amount_paid – total_amount

IF profit > 0 THEN OUTPUT (“ The Profit Gained : “, profit)

ELSE OUTPUT (“The Break Even Point Value : “,total)

END IF

END

Refrences:

Computer Science by David Watson & Helen Williams


Visual Basic Console Cook Book
Computer Science AS and A level by Sylvia Langfield and Dave Duddell

17
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Solution (Visual Basic)

Dim citizen, people As Integer


Dim total, costperson As Single

Const coach1 = "150"


Const coach2 = "190"
Const coach3 = "225"

Const meal1 = "14"


Const meal2 = "13.50"
Const meal3 = "13"

Const ticket1 = "21"


Const ticket2 = "20"
Const ticket3 = "19"

total = 0
costperson = 0
citizen = 0
people = 0

Console.Write("Please ENTER HOW MANY senior citizens want to go")


citizen = Console.ReadLine()

While citizen < 10 Or citizen > 36


Console.Write("The minimum number of senior citizens needed for the outing
to go ahead is 10; there cannot be more than 36 senior citizens are
interested in the outing : ")
citizen = Console.ReadLine()
End While
Console.WriteLine("Total number of citizens:")
Console.WriteLine(citizen)
If citizen > 24 Then people = citizen + 3 Else people = citizen + 2

Select Case people


Case Is <= 16
total = coach1 + (meal1 * people) + (ticket1 * people)
costperson = total / citizen
Case Is <= 26
total = coach2 + (meal2 * people) + (ticket2 * people)
costperson = total / citizen
Case Is <= 39
total = coach3 + (meal3 * people) + (ticket3 * people)
costperson = total / citizen

End Select
Console.WriteLine("Total number of people:")
Console.WriteLine(people)
Console.WriteLine("Total cost accumulated:")
Console.WriteLine(total)
Console.WriteLine("Cost per peson:")
Console.WriteLine(costperson)

18
Computer Science 2210
Sec 2.1.2 Pseudocodes & Programming Concepts
with Majid Tahir

Dim name(39) As String


Dim amount(39) As Single
Dim seats, updated_people, c As Integer
Dim extra_amount, total_amount, amount_paid As Single
seats = 0
updated_people = 0
extra_amount = 0
total_amount = 0
amount_paid = 0

For c = 1 To people
Console.Write("Enter the name of person on the outing : ")
name(c) = Console.ReadLine()
Console.Write("Enter the amount paid by the person : ")
amount(c) = Console.ReadLine()
amount_paid = amount_paid + amount(c)
Next
Console.WriteLine("If there are spare places on the coach then extra people can be added;
they are charged the same price as the other citizens.")

If people >= 12 And people <= 16 Then


seats = 16 - people
extra_amount = costperson * seats
End If
If people >= 17 And people <= 26 Then
seats = 26 - people
extra_amount = costperson * seats
End If
If people >= 26 And people <= 39 Then
seats = 39 - people
extra_amount = costperson * seats
End If
total_amount = total + extra_amount
updated_people = people 'Comment * : if there will be no extra people
If seats >= 1 Then
updated_people = people + seats ' Comment * : if there are extra people()
Console.WriteLine("seats available for : " & seats & " person(s), EnterName and Amount
for people will to go for outing")
For d = c To updated_people 'Comment *: appending the same arrays
Console.Write("Enter the name of person on the outing : ")
name(d) = Console.ReadLine()
Console.Write(" The amount paid by the person : ")
amount(d) = Console.ReadLine()
amount_paid = amount_paid + amount(d)
Next
End If
For c = 1 To updated_people
Console.WriteLine("The Citizens’ Name :" & name(c) & " and amountpaid : " & amount(c))
Next
Dim profit As Single
profit = amount_paid - total_amount
If profit > 0 Then Console.WriteLine(" The Profit Gained : " & profit) Else
Console.WriteLine("The Break Even Point Value : " & total)
Console.ReadKey()

19

You might also like