0% found this document useful (0 votes)
19 views13 pages

A2 CS Task 6 24-9-2024

Task

Uploaded by

samiullah
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)
19 views13 pages

A2 CS Task 6 24-9-2024

Task

Uploaded by

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

Page 1 of 13

Page 2 of 13
Page 3 of 13
Page 4 of 13
Page 5 of 13
Page 6 of 13
10 5 6 7 1 12 13 15 21 8

Q1) A 1-dimensional array DataList stores the following data:

a) Write program code to:


• declare the 1D array, DataList, with ten elements.
• initialize DataList in the main program using the data values shown above.
DataList = []*10
DataList = [10, 5, 6, 7, 1, 12, 13, 15, 21, 8]
(b) (i) The function InsertionSort() takes input an array as parameter, and returns the sorted array by
applying the Insertion Sort algorithm. Write program code for InsertionSort().
def InsertionSort(Array):
for x in range(0, len(Array)):
key = Array[x]
j = x-1
while(j >= 0 and key < Array[j]):
Array[j+1] = Array[j]
j = j-1
Array[j+1] = key
return Array
b) (ii) Write a procedure ArrayOut to output all the contents of the array DataList in a single line to the
screen. The procedure should use iteration. Pass the array into the procedure as a parameter.
def ArrayOut(Array):
for x in range(len(Array)):
print(Array[x], end= " ")

Page 7 of 13
(c) Make necessary changes to the main program to output the unsorted DataList, as well as the sorted
DataList. Also, output
10 5 6 7 1 12 13 15 21 8 messages to the user to
indicate the outputs
before and after sorting.
print("Before")
PrintArray(DataList)
TheData = InsertionSort(DataList)
print("After")
PrintArray(TheData)

(d) Execute your program and take a screenshot of program output along with your program code.

Q1) A 1-dimensional array DataList stores the following data:

b) Write program code to:


• declare the 1D array, DataList, with ten elements.
• initialize DataList in the main program using the data values shown above.

Save your program as question 1.


Copy and paste the program code into part 1(a) in the evidence document.

(b) (i) The function InsertionSort() takes input an array as parameter, and returns the sorted array by
applying the Insertion Sort algorithm. Write program code for InsertionSort ().

Save your program.


Copy and paste the program code into part 1(b)(i) in the evidence document.

b) (ii) Write a procedure ArrayOut() to output all the contents of the array DataList in a single line to the
screen. The procedure should use iteration. Pass the array into the procedure as a parameter.

Save your program.


Copy and paste the program code into part 1(b)(ii) in the evidence document.

(c) Make necessary changes to the main program to output the unsorted DataList, as well as the sorted
DataList. Also, output messages to the user to indicate the outputs before and after sorting.

Page 8 of 13
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.

(d) Execute your program and take a screenshot of program output along with your program code.

Copy and paste the program code, along with the screenshot of program output
into part 1(d) in the evidence document.

Page 9 of 13
Q1) A program stores the following ten integers in a 1D array with the identifier arrayData:

21 5 7 13 3 5 19 30 21 0

a) Write program code to:


• declare the 1D array, arrayData, with ten elements.
• initialise arrayData in the main program using the data values shown above. [2]

arrayData = []*10
arrayData = [21, 5, 7, 13, 3, 5, 19, 30, 21, 8]

(b) (i) Write program code for the function bubbleSort(), which uses the Bubble Sort algorithm to sort
arrayData. The function should take an array as parameter and return the sorted array to the main
program. [4]

def BubbleSort(Array):
for X in range (0, len(Array)-1):
for y in range (0, (len(Array)-X-1)):
if Array[y]> Array[y+1]:
temp = Array[y]
Array[y]= Array[y+1]
Array[y+1]= temp
return Array
(b) (ii) Write program code for the function binarySearch(), which takes a sorted array and an integer as
parameters and performs a binary search on the members of the array to find the parameter value. It
returns True if it was found and False if it was not found. [6]
def binarySearch(Array,n):
global pos
l = 0
u = len(Array)-1
while l <= u:
mid = (l+u) // 2
if Array[mid] == n:
pos = mid
return True
else:
if Array[mid] < n:
l = mid+1
else:
u = mid-1
return False

Page 10 of 13
c) Modify your program code to appropriately prompt the user to input the binarySearch parameter.
Make changes to your code, wherever necessary, so that if the binarySearch was successful It outputs
the index of the parameter along with an appropriate message. If not, then it should also output an error
message if the parameter is not found. [2]

def binarySearch(Array,n):
global pos
l = 0
u = len(Array)-1
while l <= u:
mid = (l+u) // 2
if Array[mid] == n:
pos = mid
return True
else:
if Array[mid] < n:
l = mid+1
else:
u = mid-1
return False
def BubbleSort(Array):
for X in range (0, len(Array)-1):
for y in range (0, (len(Array)-X-1)):
if Array[y]> Array[y+1]:
temp = Array[y]
Array[y]= Array[y+1]
Array[y+1]= temp
return Array
arrayData = []*10
arrayData = [21, 5, 7, 13, 3, 5, 19, 30, 21, 8]
pos = -1
list = BubbleSort(arrayData)
searchValue = int(input("Enter search value: "))
returnvalue = binarySearch(list, searchValue)
if returnvalue:
print("Value found at ",pos)
else:
print("Not Found")
(d) Test your program with one value that is in arrayData and one value that is not in arrayData. Attach
screenshot of program output along with your program code. [2]

Page 11 of 13
Q1) A program stores the following ten integers in a 1D array with the identifier arrayData:

21 5 7 13 3 5 19 30 21 0

b) Write program code to:


• declare the 1D array, arrayData, with ten elements.
• initialise arrayData in the main program using the data values shown above. [2]

Save your program as question 1.


Copy and paste the program code into part 1(a) in the evidence document.

(b) (i) Write program code for the function bubbleSort(), which uses the Bubble Sort algorithm to sort
arrayData. The function should take an array as parameter and return the sorted array to the main
program. [4]

Save your program.


Copy and paste the program code into part 1(b)(i) in the evidence document.

(b) (ii) Write program code for the function binarySearch(), which takes a sorted array and an integer as
parameters and performs a binary search on the members of the array to find the parameter value. It
returns True if it was found and False if it was not found. [6]

Save your program.


Copy and paste the program code into part 1(b)(ii) in the evidence document.

c) Modify your program code to appropriately prompt the user to input the binarySearch parameter.
Make changes to your code, wherever necessary, so that if the binarySearch was successful It outputs
the index of the parameter along with an appropriate message. If not, then it should also output an error
message if the parameter is not found. [2]

Save your program.


Copy and paste the program code into part 1(c) in the evidence document.

(d) Test your program with one value that is in arrayData and one value that is not in arrayData. Attach
screenshot of program output along with your program code. [2]

Page 12 of 13
Copy and paste the program code, along with the screenshot of program output
into part 1(d) in the evidence document.

Page 13 of 13

You might also like