0% found this document useful (0 votes)
24 views

Pseudocode Notes

The document discusses various programming concepts like variables, arrays, data types, procedures, functions, and file handling. It provides syntax examples for declaring variables and arrays, performing linear search and bubble sort, using procedures and functions, and reading from and writing to files.

Uploaded by

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

Pseudocode Notes

The document discusses various programming concepts like variables, arrays, data types, procedures, functions, and file handling. It provides syntax examples for declaring variables and arrays, performing linear search and bubble sort, using procedures and functions, and reading from and writing to files.

Uploaded by

Rushil Mohabeer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Declaring variables and arrays

//Declare a variable with identifier ‘Counter’ of Type Integer

Declare counter as Integer

//Declare a Variable with Identifier ‘Name’ of type string

Declare Name as String

//declare a 1D array of m integer elements with identifier names

Declare Names: Array[1:m] of integer

//note that we can also start an array from 0, thus for m elements starting from 0:

Declare Names: Array[0:m-1] of Integer

//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

Other data types that are often used:

• 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

Linear search is a basic way of searching for a value in an array

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:

Declare found as Boolean  False

Declare count as Integer  1

For count = 1 to m

If nums[i] = x Then

Found = True

Output(“Found at index: “, count)

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)

Standard Bubble sort

Declare temp as Integer

Declare i,j as Integer

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

if nums[j] > nums[j+1] Then

temp  nums[j]

nums[j]  nums[j+1]

nums[j+1]  temp

End If

Next j

Next i

VB.Net implementation:

Dim temp As Integer


Dim i, j As Integer
i = 1 : j = 1
For i = 1 To m
For j = 1 To m - i
If nums(j) > nums(j + 1) Then
temp = nums(j)
nums(j) = nums(j + 1)
nums(j + 1) = temp
End If
Next
Next

Efficient bubble sort

Declare temp as Integer

Declare Sorted as Boolean = False

Declare 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 i

jj+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):

Dim temp1, temp2 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 5 - j
If arr(i, 1) > arr(i + 1, 1) Then
Sorted = False
temp1 = arr(i, 1)
temp2 = arr(i, 2)
arr(i, 1) = arr(i + 1, 1)
arr(i, 2) = arr(i + 1, 2)
arr(i + 1, 1) = temp1
arr(i + 1, 2) = temp2
End If
Next
j += 1
End While
Record structure

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

Declare Name as String

Declare Grade as Integer

End Type

Declare Students: Array[1:20] of ClassRoom

//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

Procedures are an easy way to break down a program into modules

Procedures can take parameters ByValue or ByReference

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

Syntax for procedures:

Suppose we have a procedure that takes a number and Outputs the factorial of that number

Procedure Factorial(ByValue num as Integer)

If num = 0 Then

Output(1)

Else

Declare result as Integer = 1

While num > 0

result  result * num

num  num - 1

End While

Output(result)

End If

End Procedure

Start

Declare number as Integer

Output(“Input number”)

Input(number)

Call Factorial(number)

End
VB.Net implementation:

Sub Factorial(ByVal num As Integer)


If num = 0 Then
Console.WriteLine(1)
Else
Dim result As Integer = 1
While num > 0
result = result * num
num = num - 1
End While
Console.WriteLine(result)
End If
End Sub
Sub Main(args As String())
Console.WriteLine("Input number: ")
Dim number As Integer = Console.ReadLine()
Call Factorial(number)
Console.ReadKey()
End Sub

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

Function LinearSearch(ByValue arr() as Integer, ByValue search as Integer) Returns Integer

//Notice that I passed the arr by value, but the function will take it byRef anyways

Declare index as Integer  1

For index = 1 to n

If arr(index) = search Then

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

//Assume arr is already initialized

Declare search as Integer //value to be searched for

Input(search)

If LinearSearch(arr,search) = 0 Then

Output(“Not Found”)

Else

Output(“Found at Index LinearSearch(arr,search))

End If

End

Note: Once the ‘return’ statement is executed, the function stops and we go back to main program

File handling

Often, data needs to be stored permanently, that is when a file is used

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:

Declare Line as String

OpenFile “Students.txt” for read

While Not EOF(“Students.txt”) //While not end of file

ReadFile “Students.txt”, line

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

Declare name as String

Declare counter as integer  1

For counter = 1 to 10

Input(name)

WriteFile “Names.txt” , 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

OpenFile <Name of file> For Append

WriteFile <Name of file> , <line>

CloseFile <Name of file>

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

You might also like