0% found this document useful (0 votes)
20 views1 page

cs gyaat

Uploaded by

hasanfahd09
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)
20 views1 page

cs gyaat

Uploaded by

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

9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir 9.2, 10.2, 11.

th Majid Tahir 9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir 9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir 9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com 1 at www.majidtahir.com 1 at www.majidtahir.com 1 at www.majidtahir.com

Syllabus Content: Entering Values in One-Dimension Array VB Code in Console Mode Python One-dimensional array with values in it.
Dim name(5) As String
10.2 Arrays BEGIN
Dim marks(5) As Integer num = [1, 2, 3]
DECLARE count : Integer
For count = 1 To 5 num.append(4)
use the technical terms associated with arrays including upper and lower bound DECLARE name [5] : String // for declaring 5 elements in ARRAY
select a suitable data structure (1D or 2D array) to use for a given task DECLARE marks [5] : Integer Console.WriteLine("input name " & count) num.extend([5, 6])
use pseudocode for 1D and 2D arrays (pseudocode will use square brackets to contain the name(count) = Console.ReadLine() print(num) # will print this in output [1, 2, 3, 4, 5, 6]
FOR count = 1 to 5 // for inputting 5 names and grades
array subscript, for example a 1D array as A[1:n] and a 2D array as C[1:m, 1:n]) Console.WriteLine("input marks " & count)
PRINT (“Enter Name “& count)
write program code using 1D and 2D arrays
write algorithms/program code to process array data including: INPUT name (count) marks(count) = Console.ReadLine() Another example of One-Dimensional Array
o Sort using a bubble sort PRINT (“Enter grade for “& name(count)) While marks(count) > 100 Or marks(count) < 0 Module Module1
o Search using a linear search INPUT marks (count) Console.WriteLine("Re-Enter marks" & count) Sub Main()
NEXT count marks(count) = Console.ReadLine() Dim count As Integer
FOR count 1 to 5 // for displaying 5 names and grades End While Dim name(4) As String
An array is a special variable that has one name, but can store multiple values. Each value is stored in an Dim marks(4) As Integer
element pointed to by an index. PRINT (name (count) & “has marks " & marks(count)) Next
NEXT count Dim gender(4) As String
The first element in the array has index value 0, the second has index 1, etc For count = 1 To 5
END For count = 0 To 4
One Dimensional Arrays Console.WriteLine(name(count) & " scored: " & marks(count)) Console.WriteLine("please enter your name" & count)
A one dimensional array can be thought as a list. An array with 10 elements, called names, can store 10 PYTHON Code: Next name(count) = Console.ReadLine()
names and could be visualized as this: Lower bound of ARRAY can start from 0 or 1 Console.WriteLine("please enter your gender" & count)
name = [] Output of VB code (Console mode)
gender(count) = Console.ReadLine()
marks = [] Console.WriteLine("please enter your marks" & count)
Index Name Index Name
0 Majid
for count in range(5): marks(count) = Console.ReadLine()
1 Majid
name.append (str(input("Enter name: "))) Next count
1 Tahir 2 Tahir marks.append (int(input("Enter marks "))) For count = 0 To 4
2 Naila 3 Naila print("Name ", name, " scored ", marks) Console.WriteLine("your name is : " & name(count))
3 Hassan 4 Hassan Console.WriteLine("your gender is : " & gender(count))
Console.WriteLine("your marks are : " & marks(count))
4 Adil 5 Adil Next count
5 Ali 6 Ali Console.ReadKey()
6 Osman 7 Osman End Sub
End Module
7 Abdullah 8 Abdullah
8 Haider 9 Haider
Multi-Dimensional Arrays or Two dimensional Arrays (2D Array):
9 Hamza 10 Hamza
A multi-dimensional array can be thought of as a table, each element has a row and column index.
Following example declares a two-dimensional array called table with 3 rows and 4 colums
Arrays (One-dimensional arrays) OUTPUT screen and would be declared in PseudoCode as follows:
In order to use a one-dimensional array in a computer program, you need to consider:
DECLARE table(3, 4) : INTEGER
• What the array is going to be used for, so it can be given a meaningful name
• How many items are going to be stored, so the size of the array can be determined.
Visual Basic(Console mode)
• What sort of data is to be stored, so that the array can be the appropriate data type.
Dim table(3, 4) : As Integer
DECLARATION of Blank Array with 10 slots:
Pseudocode: VB code example: Python Code
row, col = 3, 4
DECLARE names[10]: STRING Dim names(9) As String table = [[0 for x in range(row)] for y in range(col)]
PYTHON Code # Creates a list containing 5 lists, each of 8 items, all set to 0
name[]

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 1 www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 2 www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 3 www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 4

9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir 9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir 9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir 9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com 1 at www.majidtahir.com 1 at www.majidtahir.com 1 at www.majidtahir.com

Multi-Dimensional Arrays: Searching & Sorting Algorithms in an ARRAY Bubble Sort:


A multi-dimensional array can be thought of as a table, each element has a row and
column index.
Linear Search: Bubble sort is a sorting algorithm that compares each value in the list with the next value, and
swaps the value if not in order. Algorithm is repeated many times in loop to arrange all values in
Linear search is a method of searching a list in which each element of an array is
Following example declares a two-dimensional array called matrix and would be order. We call it Bubble Sort because small values rise to the top slowly like bubbles rise in
declared by checked in order, from the lower bound to the upper bound, until the item is found, or water. After every sorting from start to end of list, biggest value goes at the bottom.
the upper bound is reached.

PSEUDOCODE Example of Two-Dimension Array


BEGIN
DECLARE table(3, 4) : Integer Searching G in the sorted list via Linear search.
FOR row = 1 To 3
FOR column = 1 To 4 The pseudocode linear search algorithm and identifier table to find if an item is in the
PRINT("Please Input Value in Row: ",row, "column : ", column) 1D array(myList) is given below.
INPUT table(row, column) DECLARE count, num As Integer
NEXT DECLARE found As Boolean = False When we have completed the first pass through the entire array, the largest value is in the
NEXT //Creating array to search item (Free notes @ www.majidtahir.com) correct position at the end of the array. The other values may or may not be in the correct order.
DECLARE Mylist() As Integer = {4, 2, 8, 17, 9, 3, 7, 12, 34, 21} We need to work through the array again and again. After each pass through the array the next
FOR row = 1 To 3 largest value will be in its correct position, as shown in Figure below.
OUTPUT ("Please enter any integer to be checked in List")
FOR column = 1 To 4
PRINT ("Row = " & row & "column = " & column & “has Value”) INPUT num
For count = 1 To 10
PRINT (table(row, column))
If num = Mylist(count) Then
NEXT
NEXT found = True
End If
END
Next count
VB Code for 2-D Array is:
VB Code Example of Two-Dimension Array If found = True Then
OUTPUT ("Item Found = ", num)
Sub Main() Else
Dim table(2, 3) As Integer OUTPUT ("Item Found is unsuccessful")
For row = 0 To 2 End If
For column = 0 To 3
Console.WriteLine("Please Input Value in Row: " & row & "column : " & column)
table(row, column) = Console.ReadLine() PYTHON Linear Search Algorithm
Next
MyList = [4,2,8,17,9,3,7,12,34,21]
Next
Console.Clear()
found = False
num = int(input("Please enter number to be found")) OUTPUT
For row = 0 To 2 uperbound = len(MyList) In effect we perform a loop within a loop, a nested loop. This method is known as a bubble
For column = 0 To 3 for index in range(upperbound): sort. The name comes from the fact that smaller values slowly rise to the top, like bubbles in a
Console.WriteLine("Row = " & row & "column = " & column & “has Value”) if MyList[index]==num: liquid.
Console.WriteLine(matrix(row, column)) found = True
Next if(found):
Next print ("item found")
Console.ReadKey() else:
End Sub
print("item not found")

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 5 www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 6 www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 7 www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 8

9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir 9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com 1 at www.majidtahir.com

Refrences:
Bubble sort PSEUDOCODE: Computer Science by David Watson & Helen Williams
Visual Basic Console Cook Book
DECLARE MyList [10] : INTEGER = {4,2,8,17,9,3,7,12,34,21} Computer Science AS and A level by Sylvia Langfield and Dave Duddell
DECLARE count, temp, top : INTEGER https://www.sitesbay.com/javascript/javascript-looping-statement
DECLARE swap : BOOLEAN = False http://wiki.jikexueyuan.com/project/lua/if-else-if-statement.html
top = LENGTH (MyList())
WHILE top <> 0
FOR count = 1 to (top-1)//(top-1)for loop to run till second last value
IF Mylist(count)> Mylist(count + 1)//compare second last with last value
THEN
temp = Mylist(count)
Mylist(count) = Mylist(count + 1)
Mylist(count + 1) = temp
swap = True
End If
top = top-1
NEXT count
END WHILE

Alternate sample program of (Bubble sort)


DECLARE myList : ARRAYS[0:8] OF INTEGER = [4,2,8,17,9,3,7,12,34,21]
DECLARE upperBound, lowerbound, count, temp, top : INTEGER
DECLARE swap : BOOLEAN

upperBound LENGTH(MyList)
lowerBound 0
top upperBound

REPEAT
FOR index = lowerBound TO (top – 1)
Swap FALSE
IF MyList [count] > MyList [count + l]
THEN Temp MyList[count]
MyList[count] MyList[count + 1]
MyList[count + 1] Temp
swaps TRUE
END IF
NEXT
top top – 1
UNTIL (NOT swap) OR (top = 0)

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 9 www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 10

You might also like