Presentation is loading. Please wait.

Presentation is loading. Please wait.

1D Arrays and Lots of Brackets

Similar presentations


Presentation on theme: "1D Arrays and Lots of Brackets"— Presentation transcript:

1 1D Arrays and Lots of Brackets
Ps Module 7 – Part I 1D Arrays and Lots of Brackets 4/6/2019 CSE 1321 Module 7

2 2. Creating and Initializing Arrays
You must choose: 1) Create an empty arrays and assign initial values with a loop later on: CREATE myArray[5] OR 2) Create and initialize the array in one line. This is helpful when we already know those value (e.g. days of the week, etc.). 4/6/2019 CSE 1321

3 Array Creation Example
We create arrays in Python by first importing the array module. from array import * Creation is different. Must include the typecode (‘i’ in the line below). myArray = array('i',[10, 20, 30, 40, 50]) Typecode Value b Represents signed integer of size 1 byte/td> B Represents unsigned integer of size 1 byte c Represents character of size 1 byte i Represents signed integer of size 2 bytes I Represents unsigned integer of size 2 bytes f Represents floating point of size 4 bytes d Represents floating point of size 8 byte 4/6/2019 CSE 1321

4 3. Accessing Information
Copy information out of a particular slot Example: CREATE clientAge // Integer clientAge ← myArray[4] This copies information from the fifth slot (slot four) into the variable clientAge 4/6/2019 CSE 1321

5 Horrible, but works… // creates empty array with 5 slots, 0-4 CREATE myArray [5] //Assigns literal values to each index in the array. myArray[0] ← 10 myArray[1] ← 20 myArray[2] ← 30 myArray[3] ← 40 myArray[4] ← 50 Ps 4/6/2019 CSE 1321

6 Accessing and Modifying
#Accessing secondNumber = myArray[1] print(secondNumber) #Modifying myArray[3] = 42 print(myArray[3]) 4/6/2019 CSE 1321 Module 4

7 Ps Traversing the Array
You will use a loop to visit every cell of the array Problem: create an array of 5 bytes and fill each slot with the number 42 Solution: CREATE myArray [5] FOR i ← 0 to 4 myArray[i] ← 42 END FOR Ps 4/6/2019 CSE 1321

8 Python – Traversing an Array
for x in myArray: #do something. Assignment IS allowed! x = x+10 print(x) 4/6/2019 CSE 1321

9 Another Array Example from array import * size = 5 myArray = array('i',[0] * size) # All zeroes for x in myArray: print(x) # To fill with 10, 20, 30, 40 and for x in range(len(myArray)): myArray[x] = (x + 1) * 10 4/6/2019 CSE 1321

10 Ps Another Trace CREATE smallestSoFar smallestSoFar ← randomArray[0]
FOR counter ← 1 to 4 IF (smallestSoFar > randomArray[counter]) THEN smallestSoFar ← randomArray[counter] END IF END FOR // Done counter 5 1 2 3 4 smallestSoFar -8 42 17 42 -8 4

11 Finding the Minimum using a Function
def function(*args): temp = myArray[0] for x in myArray: if (x < temp): temp = x print(temp) NOTE: Python is different. In Python, Object references are passed by value. A function receives a reference to (and will access) the same object in memory as used by the caller. However, it does not receive the box that the caller is storing this object in; as in pass-by-value, the function provides its own box and creates a new variable for itself. 4/6/2019 CSE 1321

12 Finding the sum or average using a method
METHOD FINDAVERAGE ( parameter: nums[]) BEGIN sum ← 0 FOR i ← 0 to nums.length - 1 // MOST IMPORTANT LINE IS HERE sum = sum + nums[i] ENDFOR average = sum / nums.length return average END FINDAVERAGE Ps 4/6/2019 CSE 1321

13 Finding a sum and or average using a method
def function(*args): temp = 0 for x in myArray: temp = temp + x print("Average:", temp/len(myArray)) print("Sum: ", temp) 4/6/2019 CSE 1321

14 Ps Defining a 2D array CREATE array nums [numRows][numColumns]
4/6/2019 CSE 1321

15 Python – Define a 2D Array
In Python, 2D Arrays must be initialized when they are created. numbers = [[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]] 4/6/2019 CSE 1321

16 Working with 2D arrays Usually involves nested loops, as shown below Problem Statement: Create an array of 4 rows and 5 columns. Populate the array with the numbers 1-20. Create array grid[4][5] count ← 1 FOR each element in a row FOR each element in a column     grid[row][col] = count count ← count + 1 END INNER FOR END FOR 4/6/2019 CSE 1321

17 Working with 2D arrays from array import *
numbers = [[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]] for r in numbers: for c in r: print(c, end = " ") print() #what value does this line print? print(numbers[3][4]) 4/6/2019 CSE 1321

18 Python - Working with Lists
colors = list() #add colors colors.append('purple') colors.append('pink') colors.append('green') #find if list contains an element if('pink' in colors): print('pink is in the list') print('\n') #show the index of pink print(colors.index('pink')) #remove a color colors.remove('green') #output list for x in range(len(colors)): print (colors[x]) 4/6/2019 CSE 1321


Download ppt "1D Arrays and Lots of Brackets"

Similar presentations


Ads by Google