KEMBAR78
Searching - 9618 Computer Science | PDF | String (Computer Science) | Data Type
0% found this document useful (0 votes)
63 views10 pages

Searching - 9618 Computer Science

The document provides an overview of string manipulation functions, email validation, and procedures for handling student details using pseudocode. It includes examples of functions like LEFT, RIGHT, and MID for string extraction, as well as procedures for validating email addresses and printing student details from a formatted string. Additionally, it covers array concepts, including declaration, input/output operations, and sorting/searching algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views10 pages

Searching - 9618 Computer Science

The document provides an overview of string manipulation functions, email validation, and procedures for handling student details using pseudocode. It includes examples of functions like LEFT, RIGHT, and MID for string extraction, as well as procedures for validating email addresses and printing student details from a formatted string. Additionally, it covers array concepts, including declaration, input/output operations, and sorting/searching algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Serious Revision Lessons

STRING Manipulation:

Example of a string: Sentence“My name is Akathandeka M, 2024”

STRING Manipulation functions: LEFT, RIGHT, MID, STR_TO_NUM, NUM_TO_STR, LENGTH, ASC,
UCASE, LCASE

LEFT FUNCTION: Extracts characters from the string, starting from the left. It takes 2 arguments,
the string and number of characters.

e.g LEFT(Sentence, 10) will return – “My name is”

e.g LEFT(Sentence, 22) will return – “My name is Akathandeka”

Extract “2024”

RIGHT(Sentence,4) – “2024”

Extract “2024” and turn into to a number

STR_TO_NUM(RIGHT(Sentence,4)) - 2024

RIGHT(Sentence,18) – “Akathandeka M, 2024”

Example of a string: Sentence“My name is Akathandeka M, 2024”

MID function has 3 arguments, which are: the string, starting position and number of characters
to be extracted

E.g to extract akathandeka:

MID(Sentence, 12, 11)

MID(Sentence, 23, 1) – “,”

STRING Manipulation functions: LEFT, RIGHT, MID, STR_TO_NUM, NUM_TO_STR, LENGTH, ASC,
UCASE, LCASE

UCASE(MID(Sentence,12,11))
Question 1

A valid email address has only 1 @ sign, and has a minimum of 2 characters before the @ sign.
E.g ru@gmail.com is a valid email address, r@gmail.com is an invalid email address while
rude@@gmail.com is invalid

Write code for function called EmailValid, that takes the parameter emailadd and validates it as
per the given rules and returns true if the email address is valid and false if its invalid:

FUNCTION EmailValid (byval EmailAdd : STRING) RETURNS BOOLEAN


DECLARE ThisChar : CHAR
DECLARE TestChar : CHAR=”@”
DECLARE AtsCount : INTEGER
DECLARE AtPos : INTEGER
DECLARE FoundFlag : Boolean
FoundFlagFALSE

FOR Count1 TO LENGTH(EmailAdd)


ThisCharMID(EmailAdd,Count,1)
IF ThisChar=TestChar THEN
FoundFlagTRUE
AtsCountAtsCount+1
AtPosCount
END IF
NEXT Count
IF AtsPos>2 AND AtsCount=1 THEN
RETURN TRUE
ELSE
RETURN FALSE
END IF
END FUNCTION

Question 2
A line of text is extracted from a text file, it contains Name, surname, Age, AverageMark, position in class.
These are separated by the # sign. Write a procedure, PrintStnDetails() to extract and print the details of the
student separately and correct labelled. The procedure receives the line of text as a parameter “Details” e.g, :
Firstname: Mthabisi
Surname: Ndlovu
Age: 18
AverageMark: 60
ClassPos: 4

“Mthabisi#Ndlovu#18#60#4”

PROCEDURE PrintStnDetails(ByVal Details : STRING)


DECLARE Firstname, Surname : STRING
DECLARE Age : INTEGER
DECLARE AverageMark : REAL
DECLARE ClassPos,PrevHashPos : INTEGER
DECLARE TestChar : CHAR =”#”
DECLARE ThisChar : CHAR
DECLARE FoundFlag : BOOLEAN
FoundFlag False
DECLARE HashCount :INTEGER

FOR Count  1 TO LENGTH(Details)


ThisCharMID(Details, Count,1)
IF ThisChar = TestChar THEN
HashCount  HashCount + 1
FoundFlag  TRUE
CASE OF HashCount
CASE 1:
FirstName  LEFT (Details,Count-1)
PrevHashPosCount
CASE 2:
Surname  MID(Details, PrevHashPos+1,(Count-1)-PrevHashPos))
PrevHashPosCount
CASE 3:
AgeSTR_TO_NUM(MID(Details,PrevHashPos+1, (Count-1)-PrevHashPos))
PrevHashPosCount
CASE 4:
AverageMark STR_TO_NUM(MID(Details, PrevHashPos+1, (Count-1)-PrevHashPos))
PrevHashPosCount
ClassPos STR_TO_NUM(MID(Details, Count+1,LENGTH(Details)-Count))
END CASE
END IF
Number 3:
Write a procedure ExtractFile that reads a text file called “Students.txt” and extracts a line of text
it contains Name, surname, Age, AverageMark, position in class. These are separated by the # sign. The
procedure PrintStnDetails() takes a parameter Details and then extracts and prints the details of the student
separately and correctly labelled.
e.g, :
Firstname: Mthabisi
Surname: Ndlovu
Age: 18
AverageMark: 60
ClassPos: 4

“Mthabisi#Ndlovu#18#60#4”

START
PROCEDURE ExtractFile()
DECLARE Myfile : STRING
Myfile”Students.txt”
OPEN FILE Myfile FOR READ
WHILE NOT EOF
READFILE Myfile, Details
CALL PrintStnDetails(Details)
ENDWHILE
CLOSE FILE
END

Number 4:
A line of text called StudentsDet contains students details e.g “Mthabisi*Ndlovu*18*2024”.
Write a procedure that extracts the first names of each student and writes them onto a file.
“Students.txt”

START
PROCEDURE SaveNames(ByVal StudentsDet : STRING)
DECLARE Myfile : STRING
Myfile”Students.txt”
DECLARE ThisChar : CHAR
DECLARE TestChar : CHAR =”*”
DECLARE StarPos : INTEGER
DECLARE FirstName : STRING
FOR Count1 TO LENGTH(StudentDet)
ThisCharMID(StudentDet, Count,1)
IF ThisChar=TestChar THEN
StarPosCount
FirstNameLEFT(StudentDet,StarPos-1)
OPEN FILE Myfile FOR APPEND
WRITEFILE Myfile, FirstName
CLOSE FILE
END PROCEDURE
Array:
An array is a data structure for storage of multiple values under the same identifier/name.
Values stored in an array must be of the same datatype.
Array Terms:
1. Identifier/Name – An array has a name which is used for all the elements in the array. E.g
StudentMarks
2. Element – the individual items in the array are called elements of an array
3. Index – each array space is identified with an integer value e.g StudentMarks(5). 5 is the
index for the 5th or 6th space in the array depending on the lowerbound.
4. Lowerbound – Is the index of the first space or element in the array. It is usually a 0 or a 1
5. Upperbound – Is the index of the last space or element in the array.
6. Size - Number of elements in an array. You can obtain it by 1. Counting the number of
elements, 2. It will be given, 3. Check the Lowerbound and the Upperbound 4.LENGTH function
7. Dimension – number of rows and number of columns that an array has. e.g 1D Array, 2D
Array
8. Datatype – Elements of an array must all be of the same datatype.

Declaration of a 1D array
When declaring an array you specify the name/identifier, the Lower and Upper Bounds and the
Datatype.
Psuedocode declaration of a Student Names array:
DECLARE StudentNames : ARRAY[1:20] OF STRING
DECLARE StudentNames : ARRAY[0:19] OF STRING
DECLARE StudentNames[1:20] : STRING
Inputting and Outputting values in an array
Because an array stores multiple values we need a loop in order to read or write values onto it. A
loop’s COUNT matches and therefore points to the array’s INDEX.

For example: Write a program that asks the user to input 6 students names onto an array and
display them.
START
DECLARE StudentNames : ARRAY[1:6] OF STRING
FOR Count1 TO 6
OUTPUT “Enter a student name”
INPUT StudentNames(Count)
NEXT Count

FOR Count1 TO 6
OUTPUT StudentNames(Count)
NEXT Count
END

Array Question 1:
Write a program that asks the user to input 10 numbers in an array and calculate their total.

START
DECLARE Numbers : ARRAY[1 : 10] OF INTEGER
DECLARE Count, Total : INTEGER
Total0
FOR Count1 TO 10
OUTPUT “enter a number”
INPUT Numbers(Count)
TotalTotal+Numbers(Count)
NEXT Count
OUTPUT “The total is: “, Total
END

Array Question 2:
Write a program that asks the user to input 15 numbers in an array and calculate their total and
average.

START
DECLARE Numbers : ARRAY[1 : 15] OF INTEGER
DECLARE Count, Total : INTEGER
DECLARE Average : REAL
Total0
FOR Count1 TO 15
OUTPUT “enter a number”
INPUT Numbers(Count)
TotalTotal+Numbers(Count)
NEXT Count

FOR Count1 TO 15
OUTPUT Numbers(Count)
NEXT Count

AverageTotal/15
OUTPUT “The total is: “, Total
END

Inputting and Outputting into a 2D array

START
DECLARE Numbers : ARRAY[1 : 15, 1 : 3] OF INTEGER
DECLARE RowCount, ColCount, Total : INTEGER
DECLARE Average : REAL
Total0
FOR RowCount1 TO 15
FOR ColCount1 TO 3
OUTPUT “enter a number”
INPUT Numbers(RowCount,ColCount)
TotalTotal+Numbers(RowCount,ColCount)
NEXT ColCount
NEXT RowCount

FOR RowCount1 TO 15
FOR ColCount1 TO 3
OUTPUT Numbers(RowCount,ColCount)
NEXT ColCount
NEXT RowCount
AverageTotal/15
OUTPUT “The total is: “, Total
END

Other Array functions:


1. Inserting values into an array
2. Reading values from an array
3. Sort: using Bubble sort and Insertion Sort
4. Search: Linear Search and Binary Search

Sorting an array using Bubble sort


Write a procedure Bubblesort(), that takes an array StudentMarks as a parameter and sorts
it in ascending order
DECLARE StudentMarks : ARRAY[1 TO 10] OF INTEGER = {23,1,16, 34,5,90, 100,63,81,88}

PROCEDURE Bubblesort(ByVal StudentMarks : INTEGER)


DECLARE upperBound : INTEGER
DECLARE lowerBound : INTEGER
DECLARE index : INTEGER
DECLARE swap : BOOLEAN
DECLARE temp : INTEGER
DECLARE top : INTEGER
upperBound ← 10
lowerBound ← 1
top ← upperBound
REPEAT
FOR index = lowerBound TO top - 1
Swap ← FALSE
IF StudentMarks[index] >StudentMarks[index + 1]
THEN
temp ← StudentMarks[index]
StudentMarks [index] ← StudentMarks [index + 1]
StudentMarks [index + 1] ← temp
swap ← TRUE
ENDIF
NEXT
top ← top -1
UNTIL (NOT swap) OR (top = 0)
END PROCEDURE
PROCEDURE LinearSearch(ByVal Mylist : INTEGER)
DECLARE upperBound : INTEGER
DECLARE lowerBound : INTEGER
DECLARE index : INTEGER
DECLARE SearchItem : INTEGER
DECLARE FoundIndex : INTEGER
DECLARE FoundFlag : BOOLEAN
upperBound ← 10
lowerBound ← 1
OUTPUT "Please enter item to be found"
INPUT SearchItem
FoundFlag ← FALSE
Index ← lowerBound
REPEAT
IF SearchItem = myList[index]
THEN
FoundFlag ← TRUE
FoundIndexIndex
ENDIF
index ← index + 1
UNTIL (FoundFlag = TRUE) OR (index > upperBound)
IF FoundFlag THEN
OUTPUT “The number was found in index”,FoundIndex
ELSE
OUTPUT “The number was not found”
END IF
END PROCEDURE
BINARY SEARCH
PROCEDURE BinarySearch(ByVal MyList : CHAR)
DECLARE upperBound : INTEGER
DECLARE lowerBound : INTEGER
DECLARE MidIndex : INTEGER
DECLARE SearchItem : INTEGER
DECLARE FoundIndex : INTEGER
DECLARE FoundFlag : BOOLEAN
upperBound ← 9
lowerBound ← 0
OUTPUT "Please enter item to be found"
INPUT SearchItem
found ← FALSE
REPEAT
MidIndex ← INT ( (upperBound + lowerBound) / 2 )
IF SearchItem = myList[MidIndex]
THEN
FoundFlag ← TRUE
FoundIndexMidIndex
ELSE IF Searchitem > myList[MidIndex]
THEN
lowerBound ← MidIndex + 1
ELSE IF SearchItem < myList[MidIndex]
THEN
upperBound ← MidIndex - 1
ENDIF
UNTIL (FoundIndex = TRUE) OR (lowerBound = upperBound)
IF FoundFlag
THEN
OUTPUT "Item found in index", FoundIndex
ELSE
OUTPUT "Item not found"
ENDIF
END PROCEDURE

You might also like