Computer science
Computer science
When creating a new computer program, they will use a structured, organized
plan of how to create the program. This is called the program development life
cycle.
This includes the stages of
Analysis
Design
Coding and
Testing
Analysis:
The first stage of the program development life cycle that involves investigating
the problem. In this stage, the problem is explored, and the requirements of the
program are identified.
Decomposition is used to split the problem into subproblems.
Design:
Once the requirements have been identified, the program designers can begin
planning how the program will work in the design phase.
This can include an overview of the program using a structure diagram, and the
designing of algorithms using flowcharts and pseudocode.
Structure diagram:
It is developed by decomposing a program into its subprograms. The diagram
has the name of the program at the top, and below this its subprograms. Each
subprogram can be split down further as well if required.
Example 1: A calculator needs to allow the user to enter numbers and symbols,
calculate the result and output this.
The name of the program is at the top. The boxes below are ready for
subprograms.
A flowchart is a diagrammatic representation of an algorithm.
Figure shows some of the set of standard symbols.
Pseudocode refers to any code that is not written on a computer to run.
This means it uses keywords, and constructs such as IF statements, WHILE loops,
etc.
These constructs occur in almost all languages, so any programmer would be
expected to read them and translate them into the actual programming
language they are using.
Coding:
Once you have decomposed your problem into subproblems, and designed the
algorithms using flowcharts and/ or pseudocode can start writing the program in
your chosen programming language.
This is often referred to as coding.
Testing:
When you have finished your program, you need to carry out testing to make
sure it:
• fully works
• does not crash
• meets all requirements.
You will need to identify appropriate test data to use to test the program. There
are four types of test data:
• Normal - data that the program should accept.
• Abnormal - data that the program should not accept.
• Extreme - data that is at the edge of what is allowed.
• Boundary - data that is on the edge of being accepted and being rejected
.
Common Algorithms:
A search algorithm checks a set of data to identify whether a specific value
exists in the data, or not.
Linear search: In a program you might need to look for a specific value in a set of
data.
A linear search will check each item one at a time, starting with the first item and
continuing until it either finds the item, or it checks the last value.
Totalling:
Totalling is adding a group of values together to give a total.
For example, a person buys items at a supermarket.
Counting:
Counting is working out how many of something there are.
For example, counting how many fingers you have or counting how many items
you bought when you went shopping.
To do this in a program you need to:
• Initialise a variable to start the count, e.g. Countitems <- 0.
Example: 1
Step 1: Start with a Set of Numbers Let's say we have the number 12345.
Step 2: Calculate the Check Digit. Use a simple method to calculate the check
digit.
For example, let's add up all the digits:1+2+3+4+5=151+2+3+4+5=15.
Now, to get a single-digit check digit, we might take the last digit of the sum,
which is 5.
Step 3: Add the Check Digit Attach this check digit to the end of the original
number.
So, 12345 becomes 123455.
Step 4: Input the Numbers and Check Digit. Enter the number 123455 (the
original number plus the check digit).
Step 5: Recalculate the Check Digit Perform the same calculation on the first
five digits of the input number 12345:1+2+3+4+5=151+2+3+4+5=15. The
last digit is 5.
Step 6: Compare Results Compare the newly calculated check digit (5) with the
original check digit (5).
Step 7: Check for Accuracy Since both check digits match, the data is likely
correct. If the check digit didn't match, it would indicate that there might be an
error in the data.
Example:
This algorithm calculates the check digit using the method 5 digit number input.
9999, 4 digits used to calculate 5th check digit, and compares it to the input
value:
INPUT Code:
Digitl ← Code DIV 1000 //extract 1st digit
Code ←Code - (1000 * Digitl)
digit2 ←Code DIV 100 //extract 2nd digit
Code ←Code - (100 * Digitl)
Digit3 ←Code DIV 10 //extract 3rd digit
Code ←Code - (10 * Digitl)
Digit4 ←DIV(Code, 1) //extract 4th digit
CheckDigit ←Code
Total ← (Digitl * 4) + (Digit2 * 3) + (Digit2 * 2) + Digit3 //calculate check digit
from data entered multiply by position and add together
NewCheckDigit ←MOD(Total, 11) //find Mod 11 of total
NewCheckDigit ←11 - NewCheckDigit //subtract result from
newCheckDigit
//check if the calculated check digit is the same as the last digit
in the //code
IF NewCheckDigit = CheckDigit THEN
OUTPUT("Correct data entry")
ELSE
OUTPUT ("Incorrect data entry")
ENDIF
Example 2:
Step 1: Start with a Set of Numbers
Let's say we have the number 87654321.
Step 2: Calculate the Check Digit
We'll use a method called the Modulus 11 method, which involves the following
steps: Multiply each digit by a weight, starting from 2 and increasing by 1 each
time, from right to left.
Add the results of these multiplications.
Divide the sum by 11 and take the remainder (this is the Modulus 11).
Subtract the remainder from 11 to get the check digit.
Let's break it down:
Start with the weights: 2, 3, 4, 5, 6, 7, 8, 9
Multiply each digit by the corresponding weight:
Add up these results:72+56+42+30+20+12+6+2=240
Divide by 11:240÷11=21 remainder 9
Subtract the remainder from 11 to get the check digit:11−9=2. So, the check
digit is 2.
Step 3: Add the Check Digit
Attach the check digit to the end of the original number. So, 87654321 becomes
876543212.
Step 4: Input the Numbers and Check Digit
Enter the number 876543212 (the original number plus the check digit).
Step 5: Recalculate the Check Digit
Perform the same calculation on the first eight digits of the input number
87654321:
Repeating the steps above, you would again calculate the sum of the weighted
digits as 240.
Modulus 11 of 240 is 9, and subtracting this from 11 gives 2.
Step 6: Compare Results
Compare the newly calculated check digit (2) with the original check digit (2).
Step 7: Check for Accuracy
Since both check digits match, the data is correct.
If the check digit didn’t match, it would indicate an error in the data entry.
Validation on input:
Validation is the checking of data that is input to make sure it is reasonable,
and/or within set bounds.
For example, make sure that a number is entered for an age or limit the range of
numbers that can be entered.
Different types of validation:
Range check
Length check
Type check
Presence check
Format check
Range check verifies that data values fall within a predefined range or set of
acceptable values.
This type of validation is essential for numerical data, such as age, salary, or
temperature.
Check if a number is higher than 1 and less than 10. It will continually ask for a
value to be input until the number is valid.
Number ←INPUT("Enter a number between 1 and 10")
WHILE Number < 1 OR Number > 10 DO
OUTPUT("Invalid please try again")
Number ←INPUT ( )
ENDWHILE
Length check will check the number of characters that are present.
This could be in a string, for example, the length of “hello world” is 11, and the
space is also a character.
It could be in a variable. Example:
TheData <-- "123 ABC!"
OUTPUT(LENGTH(TheData))
Check if the data input has 10 or less characters to be invalid, otherwise (10 or
more) it is valid.
Data ←INPUT ()
IF LENGTH(Data) < 10 THEN
OUTPUT("Invalid")
ELSE
OUTPUT ("Valid")
ENDIF
Check the length but will continually ask for this to be input until it is 10 or more
characters long.
Data ←Input ()
WHILE LENGTH(Data) < 10 DO
OUTPUT("Invalid, please try again")
Data INPUT ()
ENDWHILE
Type check:
A type of validation that checks data is the correct data type.
Data can be in different forms, including an integer (whole number), real
(decimal number), string (any characters), Boolean (true or false). There are two
ways of writing a type check:
Use a function such as:
1. .GetDataType () to return the data type, that you can then compare to the
one you want. 2. Is Integer () to return True if it is an integer and False
otherwise.
Check if the data entered is an integer value to be valid.
INPUT Data //check if the data entered is an Integer
IF Data.GetDataType() <> Integer THEN //if it is not an integer
OUTPUT ("Invalid")
ENDIF
Write algorithm will continually ask for the data to be input until it is a string
value.
INPUT Data //loop while the data entered is not a string
WHILE Data.IsString = FALSE DO
OUTPUT("Invalid please try again")
INPUT Data
ENDWHILE
Presence check:
A presence check makes sure that some data has been entered.
Programming languages can make use of the value null, or for a string value an
empty string represented by “ ”.
Write an algorithm that continually takes an input while there is no data entered.
INPUT Data
WHILE Data = "" DO //loop while there is nothing in data
OUTPUT("Invalid please try again")
INPUT Data
ENDWHILE
Format check:
Some data may need to be entered in a specific way,
for example, a date must be: _ _/ _ _ / _ _ _ _, where each space is a number _a
format check makes sure the data is entered this way.
An ID number entered needs to be 1 number followed by 2 characters. This
algorithm checks the characters and outputs whether they meet the required
format.
INPUT IdNumber //check if the first character is a number,
and characters 2 and 3 are strings
IF(SUBSTRING(IdNumber, 0, 1) .IsNumeric = TRUE AND
SUBSTRING(IsNumber, 1, 2) = TRUE) THEN
OUTPUT ("Valid")
ELSE
OUTPUT ( "Invalid" )
ENDIF
The algorithm checks whether an input is in the format two numbers,/, two
numbers, /, four numbers.
INPUT Date //loop while the data is not in the correct format
WHILE (SUBSTRING(Date, 0, 2) .IsNumeric = FALSE OR //2 numbers
2, 1) <> 11 / 11 OR // 1st slash
3' 2) .IsNumeric = FALSE OR //2 numbers
5, 1) <> 11 / 11 OR / /2nd slash
SUBSTRING(Date,
SUBSTRING(Date,
SUBSTRING(Date,
SUBSTRING(Date, 6, 4) .IsNumeric = FALSE) DO II 4 numbers, year
OUTPUT("Invalid")
INPUT Date
ENDWHILE
OUTPUT ("Valid")
Verification means checking that data is entered accurately and that is the same
as the original.
Two forms of verification are a visual check and a double entry.
A visual check is where you compare the data entered to the original. For
example,
reading each line from a paper copy and checking it is identical on the computer.