Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Computer science

The document outlines the program development life cycle, which includes stages of analysis, design, coding, and testing. It details methods for algorithm design, such as flowcharts and pseudocode, and explains common algorithms like linear search and bubble sort. Additionally, it covers validation techniques for input data to ensure accuracy and appropriateness, including range checks and type checks.

Uploaded by

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

Computer science

The document outlines the program development life cycle, which includes stages of analysis, design, coding, and testing. It details methods for algorithm design, such as flowcharts and pseudocode, and explains common algorithms like linear search and bubble sort. Additionally, it covers validation techniques for input data to ensure accuracy and appropriateness, including range checks and type checks.

Uploaded by

danayousif1711
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Computer science – Algorithm Design & Problem Solving

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.

Using a linear search:


To find an item stored in an array, the array needs to be searched. One method
of searching is a linear search.
Each element of the array is checked in order, from the lower bound to the upper
bound, until the item is found or the upper bound is reached.

DECLARE myList : ARRAY[0:8] OF INTEGER


DECLARE upperBound : INTEGER
DECLARE lowerBound : INTEGER
DECLARE index : INTEGER
DECLARE item : INTEGER
DECLARE found : BOOLEAN
upperBound ← 8
lowerBound ← 0
OUTPUT "Please enter item to be found"
INPUT item
found ← FALSE
index ← lowerBound
REPEAT
IF item = myList[index]
THEN
found ← TRUE
ENDIF
index ← index + 1
UNTIL (found = TRUE) OR (index >
IF found
THEN
OUTPUT "Item found"
ELSE
OUTPUT "Item not found"
ENDIF

A sorting algorithm takes a set of data and rearranges it to be in a specific order,


e.g. in ascending alphabetical order. One example of this is a bubble sort.
When writing a program you may need to sort a set of data into a specific order,
for example, ascending numerical order or descending alphabetical order.
One method of sorting data is using a bubble sort.
Using Bubble Sort:
Lists can be more useful if the items are sorted in a meaningful order. For
example, names could be sorted in alphabetical order, or temperatures could be
sorted in ascending or descending order. One method of sorting is a bubble sort.

DECLARE myList : ARRAY[0:8] OF INTEGER


DECLARE upperBound : INTEGER
DECLARE lowerBound : INTEGER
DECLARE index : INTEGER
DECLARE swap : BOOLEAN
DECLARE temp : INTEGER
DECLARE top : INTEGER
upperBound ← 8
lowerBound ← 0
top ← upperBound
REPEAT
FOR index = lowerBound TO top – 1
Swap ← FALSE
IF myList[index] > myList[index + 1]
THEN
temp ← myList[index]
myList[index] ← myList[index + 1]
myList[index + 1] ← temp
swap ← TRUE
ENDIF
NEXT
top ← top -1
UNTIL (NOT swap) OR (top = 0)

Totalling:
Totalling is adding a group of values together to give a total.
For example, a person buys items at a supermarket.

To do this in a program you need to:


• Initialise a variable to store the total,
e.g. Total <- 0.
• Add the value to the current contents of the total,
e.g. Total <- Total + Value.

Example for totalling:


Totalling the 10 values input by the user:
Total <- 0 //Initialise the total variable to 0
FOR Value <- 0 TO 9 // value from 0 to 9
Total <- Total + //Add the value to the total
NEXT Value
OUTPUT Total
values[10] = [5, 8, 12, 7, 6, 9, 3, 15, 4, 10] // Array already initialised with 10
values
total = 0 // Initialize total to 0
FOR i = 0 TO 9
total = total + values[i] // Add each value to total
Next i
OUTPUT "The total of the 10 values is: ", total

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.

Count the number of items the user enters:


Count<- 0 //initialise the count variable
Number <- 0
WHILE Number <> -1 DO // loop until the user enters -2
Number <- INPUT("Enter a number. Enter -1 to stop")
Count<- Count + 1 //add 1 to count each time a number is
input
ENDWHILE

Finding the minimum:


The minimum value is the smallest value within a set.
For example, the marks for a class are entered and the lowest mark is the
minimum.
To do this in a program you need to:
• Initialize a minimum variable to be a large value, beyond those that will be
entered,
e.g. Minimum9999.

If it is, it replaces the minimum value,


e.g. minimum  number.
This means the new value is the minimum, so the next time a value is compared,
it is checking it with the new minimum value.
Example: Find the smallest number input by a user:
Minimum ← 9999 //initialise minimum to a large value
Number ←1
WHILE Number >= 1 DO //loop until the user enters 0
Number ← INPUT("Enter a number or 0 to stop")
IF Number < Minimum THEN
//Check if the number entered is smaller than the
current min
Minimum ←Number
//if true then make a minimum because the
number
ENDIF
ENDWHILE

Finding the maximum:


The maximum value is the largest value within a set. For example, the marks for
a class are entered and the highest mark is the maximum.
To do this in a program you need to:
• Initialise a maximum variable to be a small value, beyond those that will be
entered
e.g. Maximum ← - 9999.
• Compare each value to the maximum variable,
e.g. IF Number > Maximum.
• If it is, it replaces the maximum value, e.g. Maximum ← Number.
This means the new value is the maximum, so the next time a value is
compared, it is checking it with the new maximum value.

Example Find the largest number input by a user:


Maximum ← -9999 //initialise maximum to small value
Number ← 1
WHILE Number >= 1 DO //loop until the user enters 0
Number = INPUT ( "Enter a number or 0 to stop")
IF Number > Maximum THEN
//Check if the number entered is larger than the
current maximum
Maximum ← Number //if true then make maximum because
the number
ENDIF
ENDWHILE

Finding the average KEYWORD:


The average here refers to the mean. This is the total of all the values added
together, then divided by how many numbers there are.
For example, if the data is 1, 3, 5, 8, 4, 2, 6, 9.
average = ( 1 + 3 + 5 + 8 + 4 + 4 + 6 + 9) /8 = 40 / 8 = 5
To do this in a program you need to use the count and total from earlier in this
section.
The average is then calculated with the formula

Example Find the average of the numbers input by a user:


Count←0 //initialise the count to 0
Total← 0 //initialise the total to 0
Number ← 1
WHILE Number >= 1 DO //loop until the user enters 0
Number ←INPUT ( "Enter a number or 0 to stop")
Total←Total + Number //add the number entered to the total
Coun← Count + 1 //add 1 to the count
ENDWHILE
Check Digit:
A check digit is a single digit added to the end of a number sequence to help
detect errors in data entry. It's calculated using a specific formula based on the
other digits.
The system recalculates the check digit when the sequence reaches the other
end to verify accuracy. If the calculated digit matches the original, the data is
considered valid.

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 whether a number is higher than 1 and less than 10.


Number ←INPUT("Enter a number between 1 and 10")
IF Number < 1 OR Number > 10 THEN
OUTPUT ("Invalid")
ELSE
OUTPUT ("Valid")
ENDIF

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 to display the output Invalid if there is no data entered.


INPUT Data
IF Data = NULL THEN //check if the data entered is null
OUTPUT ( "Invalid" )
ENDIF

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.

A double-entry check is where the same data is entered twice, usually by


different people. The computer will then check whether there are any differences
in what they each entered.

You might also like