ITM 352 Array Processing.

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

NUMERIC ARRAYS DEBBI HAMNER CIT 336 TEACHING PRESENTATION.
CSE 154 LECTURE 6: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Lecture 5 – Function and Array SFDV3011 – Advanced Web Development 1.
Chapter 8 Arrays and Strings
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
ITM ITM 352 Array Processing. ITM Simple Array Processing  Arrays can be processed using iteration. Standard array-processing loop:  What.
ITM © Port, Kazman1 ITM 352 Simple Arrays. ITM © Port, Kazman2 Arrays r Collections r Array basics m Declaration, allocation, and initialization.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CSE 154 LECTURE 15: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
Arrays and Strings. Arrays in PHP Arrays are made up of multiple memory blocks, each with the same name and differentiated by an index number Each block.
Web Database Programming Using PHP
Flood fill algorithm Also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array, When applied.
PHP (Session 1) INFO 257 Supplement.
Arrays Chapter 7.
Chapter 6: Using Arrays.
Lecture 5 of Computer Science II
Outline lecture Revise arrays Entering into an array
EGR 2261 Unit 10 Two-dimensional Arrays
Manipulating Pictures, Arrays, and Loops part 2
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
© 2016 Pearson Education, Ltd. All rights reserved.
Web Database Programming Using PHP
Merging Merge. Keep track of smallest element in each sorted half.
PHP Arrays Functions.
Class06 Arrays MIS 3502 Jeremy Shafer Department of MIS
GC211Data Structure Lecture2 Sara Alhajjam.
ITM 352 Cookies.
Arrays and files BIS1523 – Lecture 15.
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
Siti Nurbaya Ismail Senior Lecturer
Array List Pepper.
Manipulating Pictures, Arrays, and Loops part 2
Arrays MIS 3502 Jeremy Shafer Department of MIS Fox School of Business
Java How to Program, Late Objects Version, 10/e
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
EKT150 : Computer Programming
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Object Oriented Programming in java
Locators 3 a 1 g 4 e 12/29/2018 7:56 AM Locators Locators
Web DB Programming: PHP
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Starting Out with Programming Logic & Design
CS2011 Introduction to Programming I Arrays (I)
24 Searching and Sorting.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Arrays Chapter 7.
Lecture 4 2d Arrays CSE /26/2018.
Given value and sorted array, find index.
Multidimensional array
ITM 352 Simple Arrays.
Web Programming– UFCFB Lecture 21
Data Structures & Algorithms
Manipulating Pictures, Arrays, and Loops
2-Dimensional Lists (Matrices) in Python
CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ
Insertion Sort Array index Value Insertion sort.
PHP Array.
ITM 352 Functions.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Announcements.
Presentation transcript:

ITM 352 Array Processing

Simple Array Processing Arrays can be processed using iteration. Standard array-processing loop: What about associative arrays? for ($i=0; $i < count($angles); $i++) { print "angle $i: ". $angles[$i]; } foreach ($products as $key => $value) { print 'key ' . $key . ' has value ' $value; }

Multi-dimensional Arrays Arrays can hold any PHP data type including other arrays This is useful for creating multi-dimensional arrays $row1 = array('a','b','c'); $row2 = array('d','e','f'); $row3 = array('g','h','i'); // make 2-dim array of rows $matrix = array($row1, $row2, $row3); echo $matrix[0][0]; echo $matrix[1][2]; echo $matrix[2][1]; $matrix[2][2] = 'I';

Multi-dimensional Arrays (cont.)‏ You can mix indexed and associative arrays! (you will find this useful later)‏ $product1 = array('name' => 'small gumball', 'price' => 0.02); $product2 = array('name' => 'medium gumball', 'price' => 0.05); $product3 = array('name' => 'large gumball', 'price' => 0.07); // Create array of all products $products = array($product1, $product2, $product3); // Now print the array of products for ($i=0; $i<count($products); $i++) { echo "Product: $i is {$products[$i]['name']} and costs {$products[$i]['price']} each<BR>"; }

Element Existence Sometimes it's useful to know an element exists in an array. Use the array_key_exists() function for this: $product = array('name' => 'small gumball', 'filling' => NULL, 'price' => 0.04); if( array_key_exists('name', $product) )‏ echo "Product is {$product['name']}"; Why not just use isset()? array_key_exists('filling', $product) // returns? isset($product['filling']) // returns?

Searching You can test for an element in array with in_array() if (in_array('small gumball', $product))‏ print('we sell small gumballs'); You can search for an element key by value in an array with array_search() print "the key for small gumball is " . array_search('small gumball', $product);

Sorting There are many ways to sort the elements in array such as sort(),asort(),ksort(): $a = array('3', 'a', 'c', 'b', '2', '1'); sort($a); See the on-line manual at php.net for help with functions Take care with sorting functions as they do not return the sorted array. The array is directly manipulated (the parameter is passed by reference).

Keys and Values Use array_keys() to get an array of an array's keys $product = array('name' => 'small gumball', 'filling' => 'solid', 'price' => 0.04); Use array_keys() to get an array of an array's keys $prodKeys = array_keys($product); Use array_values() to get an array of an array's values $prodValues = array_values($product); When would you want to use either of these?

Removing and Inserting Elements $product = array('small gumball','solid', 0.04); Usually you will simply create a new array if you need to insert or remove elements $prodNoFilling = array($product[0], $product[2]); You can use array_splice() to insert or remove elements directly array_splice($product, 1, 1); // removes elem 1 array_splice($product, 1, 1, 'hollow'); // replaces elem 1 array_splice($product, 1, 0, 'logo'); // inserts at elem 1 Challenge: What happens to $product if you executed the above as is?

Vector Functions Merging arrays – splices together $nums1 = array(1,2,3,4,5, 5.0); $nums2 = array(5,6,7,8,9); Merging arrays – splices together $mergeNums = array_merge($nums1, $nums2); Array Unique – all unique values (uses ==)‏ $unionNums = array_unique($nums1); Array Intersection – all common values (uses ==)‏ $intsctNums = array_intersect($nums1, $nums2); Array Difference – return all values of $nums1 that are not in $nums2 $diffNums = array_diff($nums1, $nums2);