0% found this document useful (0 votes)
9 views7 pages

T4 Worksheet

The document provides a worksheet focused on programming with arrays and lists, including tasks for inputting monthly temperatures and rainfall, implementing a Bubble Sort algorithm, and calculating student marks. It contains code examples and prompts for user input, as well as instructions for creating specific programs. The document is structured into tasks that guide the user through various programming concepts and exercises.

Uploaded by

Mofizur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

T4 Worksheet

The document provides a worksheet focused on programming with arrays and lists, including tasks for inputting monthly temperatures and rainfall, implementing a Bubble Sort algorithm, and calculating student marks. It contains code examples and prompts for user input, as well as instructions for creating specific programs. The document is structured into tasks that guide the user through various programming concepts and exercises.

Uploaded by

Mofizur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Worksheet 4 Arrays and lists

Unit 6 Programming

Name:...................................................................................................... Class: ......................

Task 1
1. Julie has collected data on the average monthly temperatures for each month of the year.
She is writing a program to print these out in a list, e.g.
January 15.0
February 13.8
March 16.5
etc.
The start of her program defines a list called month and initialises it.

months = ["January", "February", "March", "April”, "May", "June",


"July",
"August", "September", "October", "November", "December"]
She wants to allow the user to input the monthly temperatures into a list named
temperature which has length 12.
Write a program for inputting the data in Julie’s program, prompting the user with
statements such as "Enter temperature for January: ", "Enter temperature for February: ",
etc., and then prints the data, giving the month and temperature for each month.

months = ["January", "February", "March", "April", "May", "June",


"July", "August", "September", "October", "November",
"December"]
average_temps = []
for i in range(0,12):
temp = int(input(f"Enter temperature for {months[i]}: "))
average_temps.append(temp)

for i in range(0, 12):


print(f"The average tempreture in {months[i]} is
{average_temps[i]} degrees")

1
Worksheet 4 Arrays and lists
Unit 6 Programming

2
Worksheet 4 Arrays and lists
Unit 6 Programming

2. Write a program to input the monthly rainfall for each month from January to December into
a list and print:
 a list of each month’s rainfall
 the total annual rainfall to one decimal place
 the monthly average over a year
 the number of months that have rainfall above the average value

annual_rainfall = 0
annual_average = 0
above_average = 0
monthly_rainfall = []
months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"]

for i in range(0, 12):


rainfall = float(input(f"What was the rainfall on
{months[i]}: "))
monthly_rainfall.append(rainfall)
annual_rainfall += rainfall
print(monthly_rainfall)
annual_rainfall = round(annual_rainfall, 1)
print(annual_rainfall)
annual_average = annual_rainfall/12
print(annual_average)

for i in range(0, 12):


if monthly_rainfall[i] > annual_average:
above_average += 1

print(above_average)

3
Worksheet 4 Arrays and lists
Unit 6 Programming

Task 2
The simplest (and slowest) method of sorting an array of values into alphabetical or numerical
sequence is the Bubble Sort. It works like this:

Suppose you have an array of 6 usernames: Carl, Tasmin, Eric, Zoe, Alan and Mark.

Go through the list, comparing each name with the one next to it. If the item is greater, swap it
with the other one.

After going through the list once, it looks like this: Carl, Eric, Tamsin, Alan, Mark, Zoe.

The last element of the array is now in the correct place.

(i) Repeat the operation on the first 5 names. What is the order now?

(ii) Show the order of the names after each further pass through the array until the names
are sorted.

The program for Bubble Sort is:


userName = ["Carl","Tamsin","Eric","Zoe","Alan","Mark"]
numItems = len(userName)
for i in range(0,numItems - 1):
for j in range(0, numItems - i - 1):
if userName[j] > userName[j+1]:
Swap the names in the array
print(userName)
(iii) Write Python statements to replace the statement Swap the names in the array to show
how this operation will be performed.

4
Worksheet 4 Arrays and lists
Unit 6 Programming

Task 3
Write a program to do the following:

For each of three students


 Input the name of the student into a list called name
 Input five marks for the student into a 2-dimensional list called marks
 Calculate and output the student’s total and the average mark
The global variables have already been set up for you.
name = [""]*3
total = [0]*3
averageMark = [0]*3
mark = [
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
]

5
Worksheet 4 Arrays and lists
Unit 6 Programming

6
Worksheet 4 Arrays and lists
Unit 6 Programming

Paper 2 Example Question 4

Suggested time: 15 minutes

A program is needed that must meet the following requirements:


 Use a list of six race times that show the performance of six runners in seconds
 Print the longest race time
 Print the shortest race time
 Print the average race time

Open file Q04.

Amend the code to:


 Create a FOR loop
 Determine the shortest race
 Determine the longest race
 Determine the total race time for all racers
 Calculate the average race time
 Print the shortest race time
 Print the longest race time
 Print the average race time

Do not add any additional functionality.


Save your amended code file as Q04FINISHED.py

(Total for Question 4 = 9 marks)

You might also like