Computer Science - Unit 1 Mod 2
Computer Science - Unit 1 Mod 2
4 Internal
Problem Context
Problem Description
What is a Problem?
Methods
Bottom-up starts with the most basic components and builds up to
the overall system. Begin by identifying and implementing the
fundamental components or modules. Once the basic components
are developed, start combining them to form higher-level systems or
modules.
20 marks Graded
14 IA: Definition Of Problem
The Cover Image should be a real place that has a real problem.
The Background should have the name of the organization, name of owner
and address of business.
History and specifics of the organization: Who owns the place/who works there?
What do they do? When did it start? Why was it needed in the community? Where
is it located?
Description of Context should come from an interview that describes how
exactly the business is operated on a day-to-day basis, focus on the
papers(forms, receipts, invoice, records, attendance, etc.) in the system.
Describe how the current system works without critique, NO NEGATIVE
STATEMENTS.
Description of the Problem should outline all the issues in the system and
what is wrong that can be solved in programming. Critique in Point format,
minimum of 6 points.
Problem Definition
Problem Analysis
Solution Analysis - identify and evaluate possible
solutions
15 5 Steps to Selection of Solution - select and justify the optimal
Problem solutions
This focuses on what a system must do—the core features and tasks it
performs.
Ensures the system performs its intended functions correctly.
Answers the who, what, where
Eg:
The system must be able to store username, date of birth and email addresses.
The system should require users to log in with a username and password to access
the system.
Users should be able to input, edit, or delete data within specific fields or forms. (be
specific)
The system should enable users to search and retrieve information from a database
or repository.
18 Non-Functional Analysis
Solution Think of two or three possible ways of helping the shopkeeper keep track of
analysis the orders and sales.
Select Weigh the pros and cons of each solution and decide on which is best.
solution Choose a solution.
Create a plan to get the solution into action.
Implemen
tation and Implement the plan.
review Review the results and correct any errors where necessary.
38 Problem Solving Example 2
39 Answer
Problem Talk to the treasurer and try to understand the problem. Validate/Verify the
definition problem by making a problem statement to the treasurer.
Problem Collect some data about the membership and required dues.
Analysis Analyze the data collected.
Solution Think of two or three possible ways of helping to track membership and
analysis subscriptions.
Weigh the pros and cons of each solution and decide which is best to solve the
Select problem.
solution Choose a solution.
Create a plan to get the solution into action.
Implemen
tation and Implement the plan.
review Review the results and Correct any errors where necessary.
Program
Design
Develop a detailed logical plan to
solve the problem. 40
41 Program Design
Pseudocode
46 Ways to
represent
an
algorithm
Flowcharts
47 Narrative
Before we can develop the Algorithm, we must understand the data the
algorithm will be manipulating.
Variables – The name given to a collection of memory cells, designed
to store a particular data item.
Constants – a data item with a name and value that remains the same
throughout the execution of the program
Literals – a constant whose name is the written representation of its
value. Eg 2, True, “Jack”
51 Naming Variables
Num 2
Num “Jack”
2 = num
“Jack” = num
57 Control Structures
IF (condition) THEN
Statement
Selection/ ENDIF
59
Decision/
Condition IF (condition) THEN
Statement1
ELSE
Statement 2
ENDIF
60 Nested Ifs
There are times when multiple checks need to be made before a decision is
arrived at. This is where we place an IF inside another IF…….a nested IF.
IF (condition) THEN
Statement
ELSE
IF (Condition) THEN
Statement
ELSE
IF (Condition) THEN
Statement
ELSE
….
ENDIF
ENDIF Write a pseudocode algorithm to calculate the discount on an Item. If
the price is between 1 and 100 dollars the discount is 5% if the price
ENDIF is between 100 and 1000 the discount is 7% and all prices over 1000
receives a discount of 10%.
61 Selection conditions
Equality operators
= = x = y X is equal to y
AND
OR
NOT
The condition must be true BEFORE the program enters the loop and does
the statements inside the loop.
Initializations must happen before the loop
When do we initialize?
When the variable is on both sides of the equation ( sum sum + num)
When a variable is used in a comparison in an IF statement. (IF num > max THEN)
The statements to be repeated must be placed in the TRUE path of the loop
test.
The loop exits on the condition being False and it SKIPS all the statements
inside the loop.
Write a pseudocode algorithm to accept 6 numbers from the user and print
the sum.
70 Post Test LoOP
REPEAT
Statements
UNTIL (condition)
DO
Statements
WHILE (condition)
In this iteration the loop terminating condition is not made until the body
of the loop is executed at least once.
Write a pseudocode algorithm to accept 6 numbers from the user and print
the sum.
71 Bounded/Indexed LoOP
In this iteration the loop terminating condition is not made until the body
of the loop is executed at least once.
83 E.g. Add 6 numbers
84 Bounded/Indexed Loop
This type of loop does counting and is used when the exact number of
repetitions is known in advance. It is sometimes referred to as definite
repetition.
85 E.g. Add 6 numbers
86
Write a Pseudocode algorithm to read the names and ages of ten (10)
people and print the name of the oldest person. Assume that there are
no persons of the same age. Data is supplied in the following form:
name, age, name, age etc.
88
Write an algorithm to read ELEVEN numbers find their average and print
it. The algorithm should also print the number of times 6 occurs in the
data.
89
Data Structures
A data structure is a particular way of organizing data in a computer so
that it can be used effectively. Often speaks to a logical grouping of sets of
data.
90 Data Structures
0 1 2 3 4
5
96 Array Structure
Arrays are named like variables. The number in brackets determines how
many data items the array can hold. The array score[9] would allow ten data
items to be stored.
Arrays have a fixed predetermined size. An array cannot hold more items than
its capacity.
Declare
Array array_name[max_index] as datatype
Eg Array score[6] as integer NB the index for this array will end at 5, because it starts at 0
To access an element in the array we must use the array name and the index surrounded by square
brackets:
array_name[index]
To place the score of 15 at position 4 in the score array it would read:
score[3] = 15
NB position 4 is index number 3.
numbers
0 1 2 3 4 i
numbers[5]
Indicating the size
For i = 0 To 3 Do of the array
Read numbers[i]
Endfor
99
Using a loop to fill the array - 2
numbers 0
0 1 2 3 i
4
numbers[5] Initialise i to 0 then check
if the value of i equals 3
For i = 0 To 3 Do if not then it will proceed
into the loop or other
Read numbers[i] wise stop.
Endfor
100
Using a loop to fill the array - 3
numbers 9 0
0 1 2 3 i
4
numbers[5] At the start of the loop i is
0 and therefore the value
For i = 0 To 3 Do read will be placed into
location 0. The user
Read numbers[i] entered
. 9
Endfor
101
Using a loop to fill the array - 4
numbers 9 1
0 1 2 3 i
4
numbers[5] When the Endfor
statement is reached i is
For i = 0 To 3 Do incremented by 1.
Read numbers[i]
Endfor
102
Using a loop to fill the array - 5
numbers 9 1
0 1 2 3 i
4
numbers[5] Check if the value of i
equals 3 if not then it will
For i = 0 To 3 Do continue the loop or
other
. wise stop
Read numbers[i]
Endfor
103
Using a loop to fill the array - 6
numbers 9 23 1
0 1 2 3 i
4
numbers[5] i is now 1 and therefore
the value read will be
For i = 0 To 3 Do placed into index 1. The
user
. entered 23
Read numbers[i]
Endfor
104
Using a loop to fill the array - 7
numbers 9 23 2
0 1 2 3 4
i
numbers[5] When the Endfor
statement is reached i is
For i = 0 To 3 Do incremented by 1.
Read numbers[i]
Endfor
105
Using a loop to fill the array - 8
numbers 9 23 2
0 1 2 3 i
4
numbers[5] Check if the value of i
equals 4 if not then it will
For i = 0 To 3 Do continue the loop or
otherwise stop.
Read numbers[i]
Endfor
106
Using a loop to fill the array - 9
numbers 9 23 11 2
0 1 2 3 i
4
numbers[5] i is now 2 and therefore
the value read will be
For i = 0 To 3 Do placed into index 2. The
user entered 11.
Read numbers[i]
Endfor
107 Using a loop to fill the array - 10
numbers 9 23 11 3
0 1 2 3 i
4
numbers[5] When the Endfor
statement is reached i is
For i = 0 To 3 Do incremented by 1.
Read numbers[i]
Endfor
108 Using a loop to fill the array - 11
numbers 9 23 11 3
0 1 2 3 i
4
numbers[5] Check if the value of i
equals 3, if not then it will
For i = 0 To 3 Do continue the loop or
otherwise stop. Since i
Read numbers[i] equals 3 the loop stops.
Endfor
109
Using a loop to display the array -
1
□ Write an algorithm to show the first three numbers in
the array. Assume that the array already have the data
loaded.
numbers 9 23 11 0
0 1 2 3 4 i
9
For i = 0 To 3 Do
Print numbers[i]
Endfor
110
Using a loop to display the array -
2
□ Write an algorithm to show the first three numbers in
the array. Assume that the array already have the data
loaded.
numbers 9 23 11 1
0 1 2 3 4 i
9 23
For i = 0 To 3 Do
Print numbers[i]
Endfor
111
Using a loop to display the array -
3
□ Write an algorithm to show the first three numbers in
the array. Assume that the array already have the data
loaded.
numbers 9 23 11 2
0 1 2 3 4 i
For i = 0 To 3 Do 9 23 11
Print numbers[i]
Endfor
112 Things to remember about
Arrays
□ Never try to access an index outside the array range.
□ The index of the last element in the array is always SIZE -1
□ When the array starts at 0 the index of an element is
always position -1
113 Try this!
114 BEGIN
DECLARE c AS Integer
RECORD STUDENT
DECLARE
name as STRING
Age as INTEGER
END STUDENT
DECLARE ARRAY stud[20] AS STUDENT
FOR c0 TO 20 DO
PRINT “PLEASE Enter your name and age”
READ stud[c].name, stud[c].age
ENDFOR
END
115 Classwork
Efficiency of Maintenance and Testing – each module should be self contained and
have little or no effect on other modules within the algorithm. This modular approach
simplifies debugging and allows developers to focus on specific areas of the code. Each
module can be validated for correctness through unit testing.
Imposter Crew
FUNCTION moduleName ()
Statements
END moduleName
126
127 FUNCTION Main()
DECLARE sq, val AS Integer
END Main
128
Variables in Modules
Local and Global Variables
129 Scope of a Variable
The scope of a variable is the portion of a program in which that variable
has been defined and to which it can be referred.
If a list is created of all the modules in which a variables can be referenced
that list defines the scope of the variable.
Variables can be global or local.
Global Data is data that can be used by all the modules in a program. The
scope of a global variable is the whole program, because every module in
the program can access the data. The lifetime of the global variable spans
the execution of the whole program.
Local Data – variables that are defined within subordinate modules are
called local variables. These local variables are not known to the calling
module, or to any other module. The scope of a local variable is simply the
module in which it was created or defined. The lifetime of a local variable is
limited to the execution of the single subroutine in which it is defined.
Using local variables can reduce what is known as program side effects.
130 Side Effects
When variables are global, any part of the program can change them, which
makes it harder to figure out where things go wrong if a bug occurs.
Global variables stay in memory for the entire duration of the program. This
can lead to inefficient memory use, especially when large data structures are
used globally but are only needed in specific parts of the code.
In contrast, local variables are removed from memory once the function they
are declared in finishes executing.
In programs with multiple threads (like parallel processing or multithreading),
global variables can cause race conditions where different threads modify the
same variable simultaneously, causing inconsistent results.
Local variables prevent such conflicts because they are specific to each
thread or function.
If multiple functions or parts of the code use the same global variable, they
might unintentionally overwrite each other's changes, leading to hard-to-find
bugs.
131 Passing Parameters
An efficient way of inter-module communication is the passing of
parameters or arguments between modules.
Parameters are simply data items that are transferred from a calling
module to its subordinate module at the time of calling.
When the calling module calls a subordinate module in pseudocode, it
must consist of the name of the called module with a list of the
parameters to be passed to the called module enclosed in parentheses
- for example:
CalculateSalesTax (salesAmount, taxRate)
Called
Parameters
Module
The names that the respective modules give to their parameters need not be
the same but their number, type and order must be identical.
Parameter names that appear when a submodule
is defined are known as formal parameters.
Variables and expressions that are passed to a
submodule in a particular call are called actual
parameters. A call to a submodule will include an
Formal and actual parameter list, one variable for each formal
parameter name.
13
2 Actual There is a one-to-one correspondence between
Parameters formal and actual parameters, which is determined
by the relative position in each parameter list.
Also, the actual parameter corresponding to a
formal parameter must have the same data type
as that specified in the declaration of the formal
parameter.
133 Formal and Actual Parameters
For example, a mainline may call a module with an actual parameter list, as
follows:
Print_page_headings (page_count, line_count)
while the module may have been declared with the following formal parameter
list:
Print_page_headings (page_counter, line_counter)
Although the parameter names are different, the actual and formal parameters
will correspond.
134 Value and Reference Parameters
Parameters may have one of three functions:
To pass information from a calling module to a subordinate
module. The subordinate module would then use that information in
its processing, but would not need to communicate any information
back to the calling module.
To pass information from a subordinate module to its calling
module. The calling module would then use that parameter in
subsequent processing.
To fulfil a two-way communication role. The calling module may
pass information to a subordinate module, where it is amended in
some fashion, then passed back to the calling module.
135 Module Structure
ReturnDatatype FUNCTION moduleName(datatype parameter(s))
Statements
END moduleName
FUNCTION moduleName ()
Statements
END moduleName
136 Value parameters
Value parameters only pass data one way - that is, the value of the
parameter is passed to the called module. When a submodule is called,
each actual parameter is assigned into the corresponding formal
parameter, and from then on, the two parameters are independent. The
called module may not modify the value of the parameter in any way,
and, when the submodule has finished processing, the value of the
parameter returns to its original value.
Called/Submodule Formal
MAIN MODULE/Calling Parameters
module
calcAvg(grade1,grade2,grade3){
g1=45
g2=66
Actual average<(grade1+grade2+grade3)/
g3=89
Parameters 3
Print “the average is”, average
Call calcAvg(g1,g2,g3)
}
137 Modularization Classwork - Pairs
You will write a pseudocode algorithm that does the following:
Take 3 numbers from the user.
Pass these 3 numbers to three different functions (listed below).
Print results using the values returned from each function.
Functions:
EvenOdd: This function checks if a number is odd or even. It accepts 1
number and returns "Even" or "Odd".
SqSum: This function adds up the squares of the 3 numbers. It accepts 3
numbers, squares them, adds them up, and returns the sum.
Highest: This function finds the highest number of the 3. It accepts 3 numbers
and returns the largest number.
Each function should accept parameters and return a value.
The main program should call these functions and use the returned
values in print statements.
138
Pause
IA Alert!