Basic algorithms won't be taught here
You need to know what an algorithm consists of:
1. Assignment
2. Sequence
3. Selection
4. Iteration
Sometimes assignment is not include as a separate point. So the Sequence , Selection and Iteration is the most important parts of an algorithm
This is a different form of representing an algorithm
The program is written using command words such as SET and DO
Let's see an example:
This is in pseudocode
INPUT Number Total←0 WHILE Number <> -1 DO Total←Total+Number INPUT Number ENDWHILE OUTPUT Total
In structured english:
ENTER Number SET Total to 0 WHILE Number Not equal to 0 INCREMENT TOTAL BY Number,ENTER Number DISPLAY Total
You will need to know some points
For assignment
SET X to 36
INCREMENT X BY 1
For selection
IF A IS GREATER THAN B THEN ..... ELSE .....
For Repetion
REPEAT UNTIL A IS GREATER THAN B...
WHILE A IS GREATER THAN B ...
For input and output
OUTPUT A DISPLAY A INPUT A ENTER A
When you assign a value to a variable this is called assignment
x ← 20
We use an arrow in pseudocode to show that it is an assignment, the equal sign (=) is used in conditions and for comparisons
Totalling is a specific part of assignment
As totalling has the same principle but, the value is incremented by a certain value
x ← x + 1
However, x must be first initialised to 0
Most questions asks you to draw an identifier table for your algorithm or code
This means to draw a table which contains the list of variable used in the program and their corresponding purposes and datatypes
This is a simple example
Variable Name | Datatype | explanation |
---|---|---|
Total | REAL | Used to total up the class marks |
Count | INTEGER | Used to increment the loop by 1 for every iteration |
DoB | DATE | Used to record the date of birth of the user |
Studentlist | ARRAY[1:10] OF STRING | Used to store the names of 10 students in an array |
If they ask you to draw an identifier table then you don't have to declare any variables
Selection is when the output or code to performed depends on a certain condition being true
IF (condition) THEN OUTPUT (statement) ELSE OUTPUT (statement) END IF
This is in psudeocode and this is known as the IF statement
Notice how it is indented and also the keywords like IF are uppercase
If statements can have many condition to compare - for example if you want to check condition 1 and if it is false then check the second condition
IF (condition1) THEN OUTPUT (statement) ELSE IF (condition2) OUTPUT (statement) ELSE OUTPUT (statement) END IF END IF
So the program will check the next condition if the previous one is false. If condition 1 is true it will not check the 2nd condition.
Also this is not Nested if statement
Is an If statement within the if statement
IF (condition1) THEN IF (Condition2) THEN OUTPUT (statement1) ENDIF ELSE OUTPUT (statement2) END IF
This ensures that both condition 1 and 2 must be true to do statement1
So first it checks the first condition then the second condition - Infact this is the same as an AND operator condition but, an order to it
This is used when there are many conditions and a single output for each
CASE OF (Variable) (value1):(OUTPUT) (value2):(OUTPUT) (value2):(OUTPUT) . . . OTHERWISE:(OUTPUT) ENDCASE
The above code shows that the output to be performed depends on the value the variable contains
Below is more complex example
CASE OF GRADE 'A':OUTPUT"Excellent" 'A','B':OUTPUT"Good" 'C' TO 'E':OUTPUT"Moderate" . . . OTHERWISE:OUTPUT"Fail" ENDCASE
Python doesn't have CASE statements but only elif
This method is used when the loop is supposed to interate for a specified number of times and is use when the number of times is known
FOR COUNT ← 1 TO 5 OUTPUT (Statement) ENDFOR
If you want, you can use NEXT COUNT instead of ENDFOR
The code interated or loops until a certain condition is true and the code is run atlEast once
REPEAT OUTPUT (statement) UNTIL (condition)
While loops are better especially when there is a rogue value
First it checks whether the condition is true and then loops while the condition is true. The program doesn't have to run atlEast once
WHILE (condition) DO OUTPUT (statment) ENDWHILE
Conditon means such as comparing variables or checking a variable is larger than a value. You need to know what are arithmetical operators
Also the conditions could have boolean operators such AND or OR or NOT
An example is WHILE X>3 OR X = -1 DO
When you need to loop a program but you don't know how times it will be running for and to loop it until you input all the values in an unknown list - we use a rogue value
A rogue value is a input value which terminates the loop when entered and this is usually a value which is not common in the set of input values - example (-1)
It is better to use a while loop rather than a repeat loop because the repeat loop will run atlEast once
So if the list is empty and so the first input value is a rogue value then this would either complicate the repeat loop or cause an error
Let's see a simple example
INPUT Number Total←0 WHILE Number <> -1 DO Total←Total+Number INPUT Number ENDWHILE OUTPUT Total
We will not discuss flowcharts as it is simple and just requires you to convert the psueduocode in to a graphical manner
You wiil need to know the shapes used for each type such as for conditions and outputs
This is used to store data of the same data type, we need to know how to declare an array
DECLARE (Arrayname):ARRAY[x:y] OF DATATYPE
Data type means whether it was REAL or STRING..
x and y are the boundaries, x usually start with 1 but, it could also start with 0
If we need to modify a value in the array:
Arrayname[i] ← 20
More about arrays will be talked in the next chapter
This program must be remembered as they usually ask this
This is used to find a value in an array
i = 0 FOUND = FALSE MAX = 5 // This is actually the upper index INPUT Search REPEAT i ← i+1 If Array[i] = Search THEN FOUND ← TRUE END If UNTIL FOUND = TRUE OR i = MAX IF FOUND = TRUE THEN OUTPUT"The value is at position",i ELSE OUTPUT"The value is not in the array" END If
This is infact an easy code to remember and we could use repeat loop because the program must run atlEast once to compare
The program checks whether the value is present in the array and loops until it is present or if it reaches the end of the array.
If found the value is displayed
This is an A2 topic so we will discuss this later
In order for contents to be sorted in an array we could use some functions to do this, however we need to know the method used and so remember the code below
NEW ← MAX-1 INPUT ArrayName FOR COUNT ← 1 TO MAX-1 FOR i ← 1 TO NEW If ArrayName[i]>ArrayName[i+1] THEN TempVariable ← ArrayName[i] ArrayName[i] ← Arrayname[i+1] ArrayName[i+1] ← TempVariable END If ENDFOR NEW ← NEW-1 ENDFOR
We will explain the above code:
So we first enter the array to be sorted
The reason why we use two loops will be explained
First inside the inner loop, the value in an element is compared with the next element. If the current element is larger than the next element then the values are swapped using a temporary variable as shown above
This gives us the idea that this sorts from Lowest to highest (ascending)
Moreover, the loop increments i to repeat the process and make sure the first element is compared with all other elements
The reason why we use MAX - 1 is that we only need to compare the elements one less than the number of elements in the array or else a value out of the array will be compared
The inner loop makes sure that the highest value is gone to the end of the array.
After one cycle has passed the program must repeat the process, this is because one cycle only makes sure that a single value is gone to the end(highest value). There is the new value(which was before the second element)and now it is at the top(but this depends)
To make it more efficient(this is important) we do not have to compare for each element in the array every time, this is because we know that for the first cycle the highest value is at the end. So for every cycle, we can compare one less of elements for every cycle
That's why we use NEW-1 , the code will still work if you don't put that but, it will compare the whole array and sometimes this is very inefficient if the array is very large
This is a more improved version and in my opinion I think you should use this
NEW ← MAX-1 INPUT ArrayName REPEAT SORTED ← TRUE FOR i ← 1 TO NEW If ArrayName[i]>ArrayName[i+1] THEN TempVariable ← ArrayName[i] ArrayName[i] ← Arrayname[i+1] ArrayName[i+1] ← TempVariable SORTED ← FALSE END If ENDFOR NEW ← NEW-1 UNTIL SORTED = TRUE
So this checks until the array is completely sorted and there is no more sorting done then obviously this means the array is already sorted and so this will end the sort when the sorted variable is true
We will need to to know the pseudocode and python for how to read and write files
This creates a file or overwrites an existing file
We will take the Doc.txt as an example
OPENFILE "Doc.txt" FOR WRITE WRITEFILE "Doc.txt","Hello" CLOSEFILE "Doc.txt"
Instead of writing "Hello" we can write using a variable which stores a string
This is used when you need to add something to an existing file
OPENFILE "Doc.txt" FOR APPEND WRITEFILE "Doc.txt","Hello" CLOSEFILE "Doc.txt"
The only thing which changes is the mode(append)
OPENFILE "Doc.txt" FOR READ READFILE "Doc.txt",Variable CLOSEFILE "Doc.txt"
The variable stores the string or line which is read from the file.
Remember this only records a single line from the file and everytime the file is read, it reads the next line
If we want to read the whole file - line by line then we use:
OPENFILE "Doc.txt" FOR READ WHILE NOT EOF("Doc.txt") DO READFILE "Doc.txt",Variable OUTPUT Variable ENDWHILE CLOSEFILE "Doc.txt"
The EOF() function checks whether the end of the file is reached. If the end is reached then the function will return true
Closefile is required
Python uses the same principle but, it doesn't have an EOF function and so it is a bit different
Usually these text files are asked with built-in functions so let us see what are built in functions
These are some built-in functions used in pseudocode and in python. You will get this sheet in the exam however, you need to know the ones for python
We will use x as an example for a variable
Purpose | Psudocode | Python |
---|---|---|
Returns the length - returns an integer | LENGTH(x) | len(x) |
Returns the ASCII value of a character - returns an integer | ASC(x) | Not required |
Returns the character of the ASCII value - returns a char | CHR(x) | chr(x) |
Returns an truncated integer - returns an integer | INT(x) | int(x) |
Converts a number to a string - returns a string | NUM_TO_STRING(x) | str(x) |
Converts a string to a number - returns a real | STRING_TO_NUM(x) | float(x) |
Converts a char to uppercase - returns a char | UCASE(x) | Not required as these are not functions but methods |
Converts a char to lowercase - returns a char | LCASE(x) | Not required |
Converts a string to uppercase - returns a string | TO_UPPER(x) | Not required - same as UCASE |
Converts a string to lowercase - returns a string | TO_LOWER(x) | Not required - same as LCASE |
Slices a string(first part) - returns a string | LEFT(x,y) | StringVariable[:x] |
Slices a string(middle part) - returns a string | MID(x,y,z) | StringVariable[x:y] |
Slices a string(last part) - returns a string | RIGHT(x,y) | StringVariable[-x:] |
Returns a character of a string - returns char | String[x] | Variable[x] |
We will talk about slicing and INT(x) more
This is to obtain part of the array or the string
Let's take an example and use the word MR Beast
MR Beast |
---|
Each character is separated and has its own index where the first character is index 1
When we use LEFT("MR Beast",4) we should get "MR B"
This means from the start, take the first 4 characters
When we use MID("MR Beast",4,2) we should get "BE" - so we start at the 4th character and take the next two characters
When we use RIGHT("MR Beast",4) we should get "East" - so this starts 4 characters before the end
In python it is a bit different
The INT(x) function only takes the whole number part of the real number
Example if we take INT(56.6) then we would get 56 not 57
The random function is used to generate any integer between two values, inclusive
RANDINT(0,20)
So this program returns an integer from 1 to 20 - it is different everytime
In python:
randint(0,20) or randrange(0,21)
These are things you might like. Clicking these ads can help us improve our free services in the future...