Pseudocode Notes
Pseudocode Notes
//note that we can also start an array from 0, thus for m elements starting from 0:
//Declare a 2D array of m rows and n columns with identifier ‘Values’ of type Real(Start indexes at 1)
Declare Values: Array[1:m, 1:n] //m rows, n columns always read in that order like a matrix
• Char(single characters)
• Boolean(True or False)
• Real(Decimal numbers such as 69.5)
• Date(stores a date value in the format #mm/dd/yyyy#)
Linear search
It works by simply comparing each element of the array with the search value
Assume a 1D array nums of m elements is already initialized, search for the index where the number
x is found in the array and output a suitable message if it is not found:
For count = 1 to m
If nums[i] = x Then
Found = True
End If
Next count
Bubble sort
Bubble sort is one of the most basic ways to sort an array, there are two possible implementations
the standard way and the efficient way
Bubble sort works by comparing each adjacent elements and swapping them if they are out of order.
It works in two steps, a first pass, for m array elements, the first pass will iterate m times
And a second pass, for m array elements, the second pass iterates m – 1 times
Suppose we have a 1D array with identifier ‘nums’ of m integer elements that is already initialized
and we want to sort it in ascending order(note that we are using indexes 1 to m here)
i 1, j 1
for i = 1 to m
for j = 1 to m – i //after each pass the last element is already sorted, no need to sort it again
temp nums[j]
nums[j] nums[j+1]
nums[j+1] temp
End If
Next j
Next i
VB.Net implementation:
i 1, j 1
Sorted = True
For i = 1 to m – j
Sorted False
Temp nums[i]
Nums[i] nums[i + 1]
Nums[i + 1] temp
End If
Next i
jj+1
End While
VB.Net implementation:
Dim temp As Integer
Dim sorted As Boolean = False
Dim i, j As Integer
i = 1 : j = 1
While sorted = False
sorted = True
For i = 1 To m - j
If nums(i) > nums(i + 1) Then
sorted = False
temp = nums(i)
nums(i) = nums(i + 1)
nums(i + 1) = temp
End If
Next
j = j + 1
End While
Note that a 2D array can also be sorted, the VB.Net implementation for sorting an array with 5 rows
and 2 columns is given below (Note we are sorting row-wise here):
This is a user-defined data type, it is often used to store data about one object consisting of multiple
elements
For example consider a classroom with 20 students, and we want to store their names and grade in
an array of records
Type ClassRoom
End Type
//suppose we want to add the data about a student named Rushil at the 10 th position in the array,
his grade is 69
Students[10].Name “Rushil”
Students[10].Grade 69
Note: An array of records can be sorted just like a 1D array using bubble sort the only difference is
that we compare on the numerical value, here it is grade and the variable temp is declared as type
ClassRoom, try to write pseudocode to sort the array of records yourself.
Procedures
ByValue: A copy of the variable is passed from the calling program to the procedure, hence the value
cannot change in the calling program
ByReference: The address of the variable being passed from the calling program is given to the
procedure hence, the variable can be modified by the procedure
Note: Functions also take parameters byValue and ByReference exactly the same
Note: Arrays passed to the procedure and to a function always get passed ByReference no matter
what
Suppose we have a procedure that takes a number and Outputs the factorial of that number
If num = 0 Then
Output(1)
Else
num num - 1
End While
Output(result)
End If
End Procedure
Start
Output(“Input number”)
Input(number)
Call Factorial(number)
End
VB.Net implementation:
Functions
Functions work the same as procedures, except functions always have to return a value
For example, suppose we have a function that performs a linear search on an array, it will take the
array(n elements, starting from 1) as a parameter as well as a search value ByValue and will then
return the index where the value is found or returns 0 if not found
//Notice that I passed the arr by value, but the function will take it byRef anyways
For index = 1 to n
Return index //Index of value returned and execution of calling program resumes
End If
Next index
Return(0) //Only reaches this statement if loop completed and hence value not found
End Function
Start
Input(search)
If LinearSearch(arr,search) = 0 Then
Output(“Not Found”)
Else
End If
End
Note: Once the ‘return’ statement is executed, the function stops and we go back to main program
File handling
Suppose we have a file students.txt containing the names of all the students in a school and we want
to output all the names:
Output(line)
End While
CloseFile “Students.txt”
Now suppose we have a blank file Names.Txt and we want to allow the user to input 10 names into
the file:
OpenFile “Names.txt” For write
For counter = 1 to 10
Input(name)
Next counter
CloseFile “Names.txt”
Note that the opening the file for write will completely clear the file and add the new data if we want
to add on to an existing file we open it in append mode
Note, if we try to open a file for read but it does not exist, this will generate a run-time error but if
we try to open a file for write or append but it does not exist, the file gets created first