More 2D Array and Loop Examples Functions and Parameters

Slides:



Advertisements
Similar presentations
CHAPTER 10 ARRAYS II Applications and Extensions.
Advertisements

Lists Introduction to Computing Science and Programming I.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
CS 5 Today HW 9 (lab + 2 probs) due Sunday, 11/8 at midnight Dizzying arrays of possibilities… John Conway Carl Gauss This week’s credits: Exam 2 on Mon.,Tue.
UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao.
Computer Science 210 Computer Organization Arrays.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Arrays.
EECS 110: Lec 12: Mutable Data
User-Written Functions
Matrices Rules & Operations.
EGR 2261 Unit 10 Two-dimensional Arrays
(Numerical Arrays of Multiple Dimensions)
Computer Science 210 Computer Organization
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Today’s Material Arrays Definition Declaration Initialization
Foundations of Programming: Arrays
EGR 115 Introduction to Computing for Engineers
Introduction To Matrices
Basic Array Definition
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Chapter 7: Arrays.
Java Review: Reference Types
Computer Science 210 Computer Organization
CSC 253 Lecture 8.
CSC 253 Lecture 8.
More Loop Examples Functions and Parameters
HMC’s Galileo Auditorium Some of Cope’s generated MP3s:
CS 220: Discrete Structures and their Applications
CISC101 Reminders Slides have changed from those posted last night…
Chapter 8 Slides from GaddisText
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Engr 0012 (04-1) LecNotes
2-d Arrays.
2D Array and Matrix.
Lecture 13: Two-Dimensional Arrays
Multidimensional Arrays
Building Java Programs
Area Models A strategy for Multiplication
EECE.2160 ECE Application Programming
Multi-Dimensional Arrays
EECE.2160 ECE Application Programming
Functions continued.
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
CIS 110: Introduction to Computer Programming
EECS 110: Lec 12: Mutable Data
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
2D Array, Nested Loops.
Hossain Shahriar CISC 101: Fall 2011 Hossain Shahriar
Presentation transcript:

More 2D Array and Loop Examples Functions and Parameters

A few matrix and array problems Given a matrix (2D array with equal dimension), how to compute the sum for the top-right half? [[3,2,6,8], [9,2,5,7], [0,3,2,3], [1,2,3,4]] The result should be 42

The key is to figure out the indices 0 1 2 3 columns 1 2 3 [[3,2,6,8], [9,2,5,7], [0,3,2,3], [1,2,3,4]] rows When row is 0, column goes from 0 to 3 When row is 1, column goes from 1 to 3 When row is 2, column goes from 2 to 3 When row is 3, column goes from 3 to 3 for row in range( 4 ): for col in range( row, 4 ): # do work

def sum_upper_right( matrix ): ''' Sum up the upper-right corner of a matrix. Matrix is a 2D array with equal dimensions ''' sum = 0 for row in range( len( matrix ) ): # row for col in range( row, len( matrix[0] ) ): # column sum += matrix[row][col] return sum matrix = [[3,2,6,8], [9,2,5,7], [0,3,2,3], [1,2,3,4]] value = sum_upper_right( matrix ) print( 'the sum of right upper corner is ', value ) matrix_tophalf.py

Given a matrix (2D array with equal dimension), how to compute the maximum for each row and each column? # compute row max for a given ‘row’ row_max = matrix[row][0] for i in range( len( matrix[row] ) ): if matrix[row][i] > row_max: row_max = matrix[row][i] But how to go through a column to compute the maximum? # compute column max for a given ‘column’ col_max = matrix[0][col] for i in range( len( matrix ) ): if matrix[i][col] > col_max: col_max = matrix[i][col]

In addition to the row and column maximum, find the maximum of the entire matrix? def find_max( matrix, row_max, col_max ): ''' Given a matrix, find and return the global max, an array of row max and an array of column max ''' max = matrix[0][0] # current max for i in range( len( matrix) ): # find each row max row_max[i] = find_row_max( matrix, i ) if row_max[i] > max: max = row_max[i] for i in range( len( matrix[0] ) ): # find each column max col_max[i] = find_col_max( matrix, i ) if col_max[i] > max: max = col_max[i] return max array_max.py

Functions and Parameters We’ve learned how to develop functions def find_max( a_list ): max = a_list[0] for i in range( len(a_list) ): if a_list[i] > max: max = a_list[i] return max def sum_list( aList ): sum = 0 for i in range( len(a_list) ): sum += a_list[i] return sum In both cases, ‘a_list’ is called a parameter for the function A function can have multiple parameters Two types of parameters, mutable and immutable Let’s try out the examples (param_passing.py)

Pass By Value: parameters immutable def main() """ calls conform """ print(" Welcome to Conformity, Inc. ") fav = 7 conform(fav) print(" My favorite number is", fav ) def conform(fav) """ sets input to 42 """ fav = 42 return fav 7 fav type: int PASS BY VALUE 7 42 fav type: int “Pass by value" means that the data's value is copied when sent to a function...

Passing by reference: parameters are mutable def main() """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print(" My favorite numbers are", fav ) def conform2(fav) """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42 7 11 fav fav[0] fav[1] type: list fav type: list What gets passed by value here?

Passing list content by reference… def main() """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print(" My favorite numbers are", fav ) def conform2(fav) """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42 42 42 7 11 fav fav[0] fav[1] The reference is copied, i.e., passed by value, but the contents arn’t ! fav and it can change data elsewhere!

Watch out! You can change the contents of lists in functions that take those lists as input. (actually, lists or any mutable objects) Those changes will be visible everywhere. (immutable objects are safe, however)

But lists are passing by value!!! def main() """ calls conform3 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform3(fav) print(" My favorite numbers are", fav ) def conform3(fav) """ creates a new fav!!! """ fav = [ 42, 42 ] 7 11 fav fav[0] fav[1] type: list fav type: list

But lists are passing by value!!! def main() """ calls conform3 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform3(fav) print(" My favorite numbers are", fav ) def conform3(fav) """ creates a new fav!!! """ fav = [ 42, 42 ] 7 11 fav[0] fav[1] fav 42 fav[0] fav[1] fav

Mutable vs. Immutable Pass by REFERENCE Pass by VALUE tuple dictionary float string list bool int x = [5,42,'hi'] s = 'hi' Reference Pointer id 'hi' 5 42 'hi' x s x[0] x[1] x[2] Lists and dictionaries are handled by reference (the variables really hold a memory address) Other types of data, incl. strings, are handled by value: they hold the actual data

Lists’ flexibility Lists can hold ANY type of data x = [ 42, 75, 70 ] 42 75 70 list int int int x We can equally well imagine them as vertical structures. 42 list int x 75 int 70 int

Lists’ flexibility Lists can hold ANY type of data 42.0 75.0 70.0 42 7 double double double x 42 7 -11 list int int int x “go” “red” “sox!” list String String String x

2d lists or arrays Lists can hold ANY type of data -- including lists ! x = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list x

2d arrays Lists can hold ANY type of data -- including lists ! x = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list list x[0] x list x[1] list x[2]

Jagged arrays Lists can hold ANY type of data -- including lists ! x = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list list x[0] x list x[1] list x[2] Rows within 2d arrays need not be the same length

Rectangular arrays What does x[1] refer to? list list x[0] x x[0][0] list x[1] list x[2] x[2][3] What does x[1] refer to? What value is changed with x[1][2]=42 ? How many rows does x have, in general ? How many columns does x have, in general ?