Computer Science (Algorithms and Programming)

(Validation: range checks, length checks, type checks, presence checks, format checks, check digits.) (Verification: Double entry, Screen/visual check.)​, Variables and Constants, If and Case Statements, File Handling Statements, Functions and Procedures.

Validation and Verification

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 Check

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

From Text Books

Length Check

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:

Length Check

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

From Text Books

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).

Type Check

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)

From Text Books​​

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.

Presence Check

OUTPUT “Please enter your email address “

REPEAT

    INPUT EmailAddress

        IF EmailAddress = ” ” THEN


            OUTPUT ” * = Required “

        ENDIF

UNTIL EmailAddress <> ” “

From Text Books​​

Format Check

A format check checks that the characters entered conform to a pre-defined pattern, for example, in Chapter 9 the cub number must be in the form CUB9999.

Check Digit

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).

From Text Books​​

Verification

Verification is checking that data has been accurately copied from one source to another – for instance, input into a computer or transferred from one part of a computer system to another.

Screen / Visual Check

A screen/visual check is a manual check completed by the user who is entering the data. When the data entry is complete the data is displayed on the screen and the user is asked to confirm that it is correct before continuing. The user either checks the data on the screen against a paper document that is being used as an input form or, confirms whether it is correct from their own knowledge.

From Text Books​​

Double Entry

From Text Books​​

For double entry the data is entered twice, sometimes by different operators. The computer system compares both entries and if they are different outputs an error message requesting that the data is entered again.

Programming

Differences between Variables and Constants​

Variables / Constants are used to store items of data (P1). The data stored in variables / constants are accessed by an identifier // named data stores (P2). The value of a variable may change during the execution of a program (P3). The value of a constant will remain the same during the execution of a program (P4).

  • DECLARE FirstVar : INTEGER
  • CONSTANT FirstConst ← 500

From Text Books

Selection (IF Statements) 

If Statements

IF Age > 17 THEN

    OUTPUT “You are an adult”

ELSE

    OUTPUT “You are a child”

ENDIF

Selection (Case Statements) ​

Case Statements

CASE OF OpValue

    “+” : Answer ̈ Number1 + Number2

    “-” : Answer ̈ Number1 – Number2

    “*” : Answer ̈ Number1 * Number2

    “/” : Answer ̈ Number1 / Number2

OTHERWISE OUTPUT “Please enter a valid choice”

ENDCASE

From Text Books

Repeat Loop

This loop runs until a specific condition is met, with the number of iterations unknown. The actions within the loop are guaranteed to execute at least once, making it a post-condition loop.

Repeat Loop (Post-Condition)​

REPEAT
    OUTPUT Counter
    Counter <– Counter + 1
UNTIL Counter > 5

From Past Papers​

For Loop

A variable is initialized with an initial value and a final value. It undergoes incremental increments by one until it reaches the end value, completing the iteration.

For Loop (Count-Controlled)

FOR Number <– 1 TO 5
    OUTPUT Number
NEXT Number

From Past Papers​

While Loop​

This loop is used when actions need to be repeated as long as a specific condition is true. If the condition is false initially, the loop actions are skipped. It’s known as a pre-condition loop.

While Loop (Pre-Condition)​

WHILE Counter <= 5
    OUTPUT Counter
    Counter <– Counter + 1
ENDWHILE

From Past Papers​

String Handling

Length

finding the number of characters in the string. For example, the length of the string “Computer Science” is 16 characters as spaces are counted as a character.

Length

LENGTH (“Computer Science”)

LENGTH (MyString)

Substring

Extracting part of a string. For example, the substring “Science” could be extracted from “Computer Science”.

Substrings

SUBSTRING (“Computer Science”, 10, 7)

SUBSTRING (MyString, 10, 7)

      OUTPUT: Science

From Text Books

String Handling

Upper

Converting all the letters in a string to uppercase. For example, the string “Computer Science” would become “COMPUTER SCIENCE”.

Upper Case

UCASE (“Computer Science”)

UCASE (MyString)

Lower

Converting all the letters in a string to lowercase. For example, the string “Computer Science” would become “computer science”.

Lower Case

LCASE(“Computer Science”)

LCASE(MyString)

From Text Books

Functions

A function is a subroutine that can be used in an assignment statement. A function is a set of programming statements grouped together under a single name that can be called to perform a task at any point in a program. In contrast to a procedure, a function will return a value back to the main program.

Functions

FUNCTION Same (A : INTEGER, B : REAL) RETURNS

BOOLEAN

    IF A = ROUND (B,0)

        THEN

            RETURN TRUE

        ELSE

           RETURN FALSE

    ENDIF

ENDFUNCTION

From Text Books

Procedures

A procedure is a subroutine that may not return a value. A procedure is a set of programming statements grouped together under a single name that can be called to perform a task at any point in a program.

Procedures

PROCEDURE CheckPatient (HospitalNumber :INTEGER)

        IF HospitalNumber >=1 AND HospitalNumber <=1000

            THEN

                OUTPUT “Name of Patient “,Patient (HospitalNumber)

                   IF Reading[HospitalNumber,1] <= TempHigh AND

                   Reading [HospitalNumber,1] >= TempLow AND

                   Reading [HospitalNumber,2] <= PulseHigh AND

                   Reading[HospitalNumber,2] >= PulseLow 

                     THEN

                         OUTPUT “Normal readings”

                    ENDIF

        ENDIF

ENDPROCEDURE

From Text Books

Bubble Sort​

From Text Books​​

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

Bubble Sort

InputArray = [list of sortable items]
ArrayLength = length(inputArray)

FOR passIndex <– 0 to ArrayLength – 1
    FOR currentIndex <– 0 to ArrayLength – passIndex – 1

        IF InputArray[currentIndex] > InputArray[currentIndex + 1] THEN
            Swap elements if they are in the wrong order
            temp = InputArray[currentIndex]
            InputArray[currentIndex] = InputArray[currentIndex + 1]
            InputArray[currentIndex + 1] = temp
        END IF

    NEXT currentIndex
NEXT passIndex

# The sorted array is now in inputArray

Linear Search​

From Text Books​​

Linear Search

# Input: target value to be found (searchValue),
# Array to be searched (searchArray)

found = false
position = -1

# Iterate through the array
FOR index <– 0 to length(searchArray) – 1
    REPEAT
        IF searchArray[index] = searchValue THEN
        # Target value found
        found = true
        position = index
        END IF
    UNTIL found = true
NEXT INDEX

# Output: Whether the value is found and its position (if found)
output “Value found: “, found
output “Position: “, positionIF searchArray[index] = searchValue

# Target value found

Current Chapter: Algorithms and Programming

Automated and Emerging Technologies

Automated Systems and how they work,​ What is Robotics and characteristics of a robot, About Expert Systems and Inference Engine...

⠀​

⠀​

Database

Tables​: Records and Fields, Data Types: text/alphanumeric, character, Boolean, integer, real, date/time. SQL Scripts,, Primary Keys...

© Copyright 2024 - Made with Passion