1D Arrays and Lots of Brackets

Slides:



Advertisements
Similar presentations
1 Various Methods of Populating Arrays Randomly generated integers.
Advertisements

Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Computer Science 210 Computer Organization Arrays.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Arrays 1.
String and Lists Dr. José M. Reyes Álamo.
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Foundations of Programming: Arrays
List comprehensions (and other shortcuts) UW CSE 160 Winter 2017.
JavaScript: Functions.
Counted Loops.
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Two-Dimensional Arrays
Lecture 9 Arrays Richard Gesick.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Arrays An Array is an ordered collection of variables
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
One-Dimensional Array Introduction Lesson xx
List comprehensions (and other shortcuts) UW CSE 160 Spring 2018.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
String and Lists Dr. José M. Reyes Álamo.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Lecture 4 2d Arrays CSE /26/2018.
Multidimensional Arrays
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Building Java Programs
1D Arrays and Lots of Brackets
1D Arrays and Lots of Brackets
Review of Classes and Arrays
INC 161 , CPE 100 Computer Programming
Methods and Data Passing
Lecture 14 2D Arrays Richard Gesick.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
C++ Array 1.
1D Arrays and Lots of Brackets
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Review of Classes and Arrays
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays Introduction to Arrays Reading for this Lecture:
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
1D Arrays and Lots of Brackets
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

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. 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

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

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

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

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

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

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

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 50... for x in range(len(myArray)): myArray[x] = (x + 1) * 10 4/6/2019 CSE 1321

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

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

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

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

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

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

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

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

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