cs gyaat
cs gyaat
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
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
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