Arrays in Python & C++
Arrays in Python & C++
<list>.pop(i) Deletes the ith element of the list and returns its value.
? ? ? ? ?
temps[0] temps[1] temps[2] temps[3] temps[4] 19
Indexes
Subscripts can be constants or variables
or expressions
If i is 5, a[i-1] refers to a[4] and a[i*2]
refers to a[10]
you can use i as a subscript at one
point in the program and j as a
subscript for the same array later - only
the value of the variable matters
Variable Subscripts
temps = [0.0]*5
m=3
. . . . . .
What is temps[m + 1] ?
What is temps[m] + 1 ?
7000 7004 7008 7012 7016
EXAMPLE
SIZE = 50
idNumber = [“ “]*SIZE
hourlyWage = [0.0] *SIZE parallel arrays
25
SIZE = 50
idNumber = [“ “] *SIZE // Parallel arrays hold
hourlyWage =[0.0] *SIZE // Related information
. . . .
. . . .
. . . .
idNumber[48] 8754 hourlyWage[48] 67.96
NUM_STATES = 50
NUM_MONTHS = 12
stateHighs = [[0]*NUM_MONTHS for i in range(NUM_STATES)]
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10][11]
[0]
[1]
[2]
row 2, 66 64 72 78 85 90 99 105 98 90 88 80
.
col 7 . stateHighs[2][7]
might be
.
Arizona’s
high for [48]
August [49]
39
Processing a 2-d array by
rows
finding the total for the first row
for i in range(NUM_MONTHS):
total = total + a[0][i]
finding the total for the second row
for i in range(NUM_MONTHS):
total = total + a[1][i]
Processing a 2-d array by
rows
total for ALL elements by adding first
row, then second row, etc.
for i in range(NUM_STATES):
for j in range(NUM_MONTHS):
total = total + a[i][j]
Processing a 2-d array by
columns
total for ALL elements by adding first
column, second column, etc.
for j in range(NUM_MONTHS):
for i in range(NUM_STATES):
total = total + a[i][j]
Finding the average high temperature for Arizona
total = 0
for month in range(NUM_MONTHS):
total = total + stateHighs[2][month]
average = round (total / NUM_MONTHS)
average
85
43
Passing an array as an
argument
Arrays (lists) are passed by reference =
they CAN be changed permanently by
the function
Definition def fun1 (arr):
Call the function as
x = fun1 (myarr)
Arrays versus Files
Arrays are usually smaller than files
Arrays are faster than files
Arrays are temporary, in RAM - files are
permanent on secondary storage
Arrays can do random or sequential,
files we have seen are only sequential
Using Multidimensional Arrays
46
NUM_DEPTS = 5 # mens, womens, childrens, electronics, furniture
NUM_MONTHS = 12
NUM_STORES = 3 # White Marsh, Owings Mills, Towson
monthlySales = [[[0]*NUM_MONTHS for i in range(NUM_DEPTS)] for
j in range(NUM_STORES)]
monthlySales[3][7][0]
sales for electronics in August at White Marsh
5 DEPTS
rows
12 MONTHS columns 47
Example of filling a 3-d array
def main():
NUM_DEPTS = 5 # mens, womens, childrens, electronics, furniture
NUM_MONTHS = 12
NUM_STORES = 3 # White Marsh, Owings Mills, Towson
monthlySales = [[[0]*NUM_MONTHS for i in range(NUM_DEPTS)] for j in
range(NUM_STORES)]
storeNames = ["White Marsh", "Owings Mills", "Towson"]
deptNames = ["mens", "womens", "childrens", "electronics", "furniture"]
for store in range(NUM_STORES):
print (storeNames[store], end=" ")
for dept in range(NUM_DEPTS):
print (deptNames[dept], end = " ")
for month in range(NUM_MONTHS):
print("for month number ", month+1)
monthlySales[store][dept] [month] = float(input("Enter the sales "))
print()
print()
print (monthlySales)
Find the average of
monthly_sales
total = 0
for m in range(NUM_MONTHS):
for d in range(NUM_DEPTS):
for s in range(NUM_STORES):
total += monthlySales
[s][d][m]
average = total /
(NUM_MONTHS * NUM_DEPTS * NUM_STORES)
Problem: student data in a file
The data is laid out as
Name, section, gpa
John Smith, 15, 3.2
Ralph Johnson, 12, 3.9
Bob Brown, 9, 2.5
Etc.
Read in the data
inf = open(“students”,”r”)
studs = []
for line in inf:
data = line.split(“,”)
studs.append(data)
inf.close()
#studs looks like [[“John Smith”,15,3.2],
#[“Ralph Johnson”,12,3.9],[“Bob Brown”…]]
Find the student with highest
GPA
max = 0
for j in range(1, len(studs)):
if studs[max][2] < studs[j][2]:
max = j
#max is now location of highest gpa
studs[max][0] is the name of the student
studs[max][1] is the student’s section