0% found this document useful (0 votes)
3 views

Standard Methods of Solution

The document outlines standard methods used in algorithms, including totaling, counting, finding maximum/minimum values, searching, and sorting. It provides detailed explanations and examples of each method, such as linear search and bubble sort, as well as validation techniques like range checks and type checks. The content serves as a guide for implementing basic algorithmic solutions and data validation in programming.

Uploaded by

joelmukumba
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Standard Methods of Solution

The document outlines standard methods used in algorithms, including totaling, counting, finding maximum/minimum values, searching, and sorting. It provides detailed explanations and examples of each method, such as linear search and bubble sort, as well as validation techniques like range checks and type checks. The content serves as a guide for implementing basic algorithmic solutions and data validation in programming.

Uploaded by

joelmukumba
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Standard methods of

solution
By M Dzinzi
Standard methods used in
algorithms
• Totalling
• Counting
• Finding maximum, minimum, and average (mean) values
• Searching using linear search
• Sorting using bubble sort
Totalling and counting
• Totaling – Used with repetition, to keep the total updated. E.g.
BillTotal BillTotal + ProductCost
• Counting – Used with repetition to increment the counter by 1, each
time the loop is repeated. E.g.
NumItems NumItems + 1
Counting
PassCount ← 0
FOR Counter ← 1 TO ClassSize
INPUT StudentMark
IF StudentMark > 50
THEN
PassCount ← PassCount + 1
NEXT Counter
Count ← Count + 1
Maximum
MaximumMark ← 0
MinimumMark ← 100
FOR Counter ← 1 TO ClassSize
IF StudentMark[Counter] > MaximumMark
THEN
MaximumMark ← StudentMark[Counter]
ENDIF
IF StudentMark[Counter] < MinimumMark
THEN
MinimumMark ← StudentMark[Counter]
ENDIF
NEXT Counter
• If the largest and smallest values are not known, an alternative method is to set the
maximum and minimum values to the first item in the list.
MaximumMark ← StudentMark[1]
MinimumMark ← StudentMark[1]
FOR Counter ← 2 TO ClassSize
IF StudentMark[Counter] > MaximumMark
THEN
MaximumMark ← StudentMark[Counter]
ENDIF
IF StudentMark[Counter] < MinimumMark
THEN
MinimumMark ← StudentMark[Counter]
ENDIF
NEXT Counter
Average (mean)
Total ← 0
FOR Counter ← 1 TO ClassSize
Total ← Total + StudentMark[Counter]
NEXT Counter
Average ← Total / ClassSize
Linear Search
• A search is used to check if a value is stored in a list, performed by
systematically working through the list
• This is called a linear search, which inspects each item in a list in turn
to see if the item matches the value searched for.
• For example, searching for a name in a class list of student names,
where all the names stored are different items in the list.
9

Linear search
Search
The search
Then it continues
begins with
The criteria in moves until a match
the first
which the item to the is found or
item and
has to be second the end of
checks
searched is set item, the list is
whether a
up. and so reached with
match is
on. no match
found.
found.
OUTPUT "Please enter name to find "
INPUT Name
Found ← FALSE
Counter ← 1
REPEAT
IF Name = StudentName[Counter]
THEN
Found ← TRUE
ELSE
Counter ← Counter + 1
ENDIF
UNTIL Found OR Counter > ClassSize
IF Found
THEN
OUTPUT Name, " found at position ",
Counter, " in the list."
ELSE
OUTPUT Name, " not found."
ENDIF
Bubble sort
• Each element is compared with the next element and swapped if the
elements are in the wrong order, starting from the first element and
finishing with next-to-last element

• Once it reaches the end of the list, we can be sure that the last element is
now in the correct place.

• However, other items in the list may still be out of order.

• Each element in the list is compared again apart from the last one
because we know the final element is in the correct place.

• This continues to repeat until there is only one element left to check or no
swaps are made.
13

Bubble sort algorithm If


If the swappin
in correct order. Steps 2
current
An algorithm used to order a list g was
The
item to 3 are perform
curre
and the repeate ed in
The first nt
next d for any of
item is item
item the next the
taken is
are out items in steps,
and comp
of the list repeat
stored as ared
order, until the the
a current with
the last steps
item. the
items item is from 1.
next
are reached Else,
item.
swappe . the list
First ← 1
Last ← 10
REPEAT
Swap ← FALSE
FOR Index ← First TO Last - 1
IF Temperature[Index] > temperature[Index + 1]
THEN
Temp ← Temperature[Index]
Temperature[Index] ← Temperature[Index + 1]
Temperature[Index + 1] ← Temp
Swap ← TRUE
ENDIF
NEXT Index
Last ← Last - 1
UNTIL (NOT Swap) OR Last = 1
Validation and verification
• Validation is the automated checking by a program that data is reasonable
before it is accepted into a computer system.
• There are many different types of validation checks
including:
• » range checks
• » length checks
• » type checks
• » presence checks
• » format checks
• » check digits.
Range check

• A range check checks that the value of a


number is between an upper value and a lower
value. For example, checking that percentage
marks are between 0 and 100 inclusive:
Range
OUTPUT "Please enter the student's mark "
REPEAT
INPUT StudentMark
IF StudentMark < 0 OR StudentMark > 100
THEN
OUTPUT "The student's mark should be in the
range 0 to 100, please re-enter the mark "
ENDIF
UNTIL StudentMark >= 0 AND StudentMark <= 100
Length check

• A length check checks either:

• » that data contains an exact number of characters, for


example that a password must be exactly eight
characters in length so that passwords with seven or
fewer characters or nine or more characters would be
rejected, for instance:
OUTPUT "Please enter your password of eight
characters "
REPEAT
INPUT Password
IF LENGTH(Password) <> 8
THEN
OUTPUT "Your password must be exactly eight
characters, please re-enter "
ENDIF
UNTIL LENGTH(Password) = 8
Cont…

• » or that the data entered is a reasonable number of characters, for


example, a family name could be between two and thirty characters
inclusive so that names with one character or thirty-one or more
characters would be rejected.
OUTPUT "Please enter your family name "
REPEAT
INPUT FamilyName
IF LENGTH(FamilyName) > 30 OR
LENGTH(FamilyName) < 2
THEN
OUTPUT "Too short or too long,please re-enter "
ENDIF
UNTIL LENGTH(FamilyName) <= 30 AND
LENGTH(FamilyName) >= 2
Type check

• A type check checks that the data entered is of a given data


type, for example, that the number of brothers or sisters
would be an integer (whole number).
OUTPUT "How many brothers do you have? "
REPEAT
INPUT NumberOfBrothers
IF NumberOfBrothers <> DIV(NumberOfBrothers, 1)
THEN
OUTPUT "This must be a whole number, please re- enter"
ENDIF
UNTIL NumberOfBrothers = DIV(NumberOfBrothers, 1)
Presence check

• A presence check checks to ensure that some data


has been entered and the value has not been left blank,
for example, an email address for an online transaction
must be completed.
OUTPUT "Please enter your email address "
REPEAT
INPUT EmailAddress
IF EmailAddress = ""
THEN
OUTPUT "*=Required "
ENDIF
UNTIL EmailAddress <> ""
Format check and check digit
• A format check checks that the characters entered conform
to a pre-defined pattern
A check digit
• is the final digit included in a code; it is calculated from all the other digits in
the code. Check digits are used for barcodes, product codes, International
Standard Book Numbers (ISBN) and Vehicle Identification Numbers (VIN).
• Check digits are used to identify errors in data entry caused by mis-typing or
mis-scanning a barcode. They can usually detect the following types of error:
» an incorrect digit entered, for example, 5327 entered instead of 5307
» transposition errors where two numbers have changed order for example
5037
instead of 5307
» omitted or extra digits, for example, 537 instead of 5307 or 53107 instead of
5307
» phonetic errors, for example, 13, thirteen, instead of 30, thirty.

You might also like