ITM 352 Simple Arrays.

Slides:



Advertisements
Similar presentations
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Advertisements

Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Lecture 5 – Function and Array SFDV3011 – Advanced Web Development 1.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
For loops in programming Assumes you have seen assignment statements and print statements.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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 Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for-each PROS easy to use access to ALL items one-by-one ability to change the state.
for-each PROS CONS easy to use access to ALL items one-by-one
Outline lecture Revise arrays Entering into an array
ITM 352 Data types, Variables
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Manipulating Pictures, Arrays, and Loops part 2
Arrays Low level collections.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Loops BIS1523 – Lecture 10.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
Containers and Lists CIS 40 – Introduction to Programming in Python
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Arrays in PHP are quite versatile
PHP Arrays Functions.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Arrays Declarations CSCI N305
Chapter 7 Part 1 Edited by JJ Shepherd
ITM 352 Expressions, Precedence, Working with Strings Class #5
JavaScript: Functions.
While Loops in Python.
While Loops BIS1523 – Lecture 12.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Manipulating Pictures, Arrays, and Loops part 2
Arrays, For loop While loop Do while loop
Algorithm design and Analysis
Functions BIS1523 – Lecture 17.
7 Arrays.
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Arrays .
Arrays, Part 1 of 2 Topics Definition of a Data Structure
String and Lists Dr. José M. Reyes Álamo.
MSIS 655 Advanced Business Applications Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Python Primer 1: Types and Operators
Arrays Topics Definition of a Data Structure Definition of an Array
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Review for Final Exam.
ITM 352 Array Processing.
For loops Taken from notes by Dr. Neil Moore
Manipulating Pictures, Arrays, and Loops
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Topics Definition of a Data Structure Definition of an Array
ITM 352 Functions.
Week 7 - Monday CS 121.
Presentation transcript:

ITM 352 Simple Arrays

Arrays Collections Array basics Declaration, allocation, and initialization Subscripting Processing arrays by iteration and recursion Reading and writing arrays

Collections Sometimes you need to manage multiple pieces of data as a group of things rather than individual values. A collection is a compound data type that contains multiple values to do just this. We have already seen a kind of collection: strings (a collection of characters). the operation $aString[5] returns the 6th character (counting from zero).

Order: Index vs. Association Strings could be used to create another type of collection, lists (like a list you might create on paper for groceries). $groceries = “Item 1: Eggs, Item 2: Milk, Item 3: Spam" Both are ordered but the particular sequence may or may not be important. Strings: sequence of characters. Order is important Grocery List: collection of items. Order is unimportant Task List: sequence of tasks. Order is important

Order: Index vs. Association (cont.) When a collection must have a particular sequence, its values are indexed by position number Strings: $name = "Fred", $name[0] = 'F', $name[1] = 'r', $name[2] = 'e', … When a collection doesn't have a particular sequence, its values are associated by keys Grocery List: Value for key 'Item 3' is 'Spam'

Why Not Variable Variables? $product1 = "Phone"; $product2 = "Candy"; $product3 = "Soda"; for($i=1; $i<=3; $i++) { $ident = "product$i"; print $$ident . "<BR>"; } This is sort of a collection, but not terribly convenient or efficient. Why?

Arrays A general kind of ordered collection. Special features: Built-in to PHP, has special syntax. Elements can be any type of data (including other arrays!) Collection can change size anytime Arrays may indexed or associative (or both!) Note: All arrays in PHP are associative Keys are automatically set to integers if you don’t specify them

Arrays (cont.) Definition: An array is a named collection of values $indexedArray value 'green' 'blue' 'yellow' 'purple' 'red' element position (identified by index) 1 2 3 4

Arrays (cont.) Definition: The values can be of different types. $indexedArray value 17 'Hi' 9.33 NULL true element position (identified by index) 4 1 2 3 $associativeArray value 17 'Hi' 9.33 NULL true element position (identified by key) 'age' 'greet' 'amount' 'notset' 'is_easy'

Array Terminology (indexed) Array name $temperature[2] $temperature[$2] $temperature[2] = 32; Index - also called a subscript or index - must be an int, - or an expression that evaluates to an int Indexed variable - also called an element or subscripted variable Value of the indexed variable; also called an element of the array

Array Terminology (associative) Array name $product['price'] $product['price'] = 32; key - also called a subscript or index - must be an int, - or an expression that evaluates to an int keyed variable - also called an element or subscripted variable Value of the indexed variable; also called an element of the array

Subscripting Principal feature of arrays: ability to access each element easily. Use notation to access elements of an array. If the key-expression has integer value n, this returns the n'th element in array-name, counting from zero (!) If the key-expression is a string, it returns the value associated with that key array-name[key-expression]

Subscripting Examples $grades[0] = 75; echo $grades[0]; $i = 2; $clocks[$i-1] = time(); $clocks[] = time()+1; printf("<BR>time[1] is %s and time[2] is %s", $clocks[$i-1], $clocks[2]); $name['first'] = "Rumple"; $name['last'] = "Stilskin"; echo "<BR>name is {$name['first']} {$name['last']}"; $thingy[] = 1; $thingy[] = 4; echo "<BR> $thingy[0] and $thingy[1]"; This is how you add elements to an existing indexed array.

Subscript Errors Using a subscript larger than length-1 or less than 0 or a key that does not exist will not cause the program to stop executing. No element will be returned Take care to use the correct subscripts! Be careful of interpolation problems when accessing array elements within a string echo "The product is $product['name']"; // won't work! echo "The product is $product[name]"; echo "The product is {$product['name']}";

Declaring and Initializing arrays One method: initialization list using array(). This allocates the array as a 3-element array and initializes its elements the same as: $angles[0] = 3.5; $angles[1] = 9.7; $angles[2] = 15.01; Key-value pairs can also be used $angles = array(3.5, 9.7, 15.01); $product = array('name' => 'gumball', 'price' => 0.07, 'quantity' => 1500);

Finding the Length of an Array Size of $anArray can be determined using count($anArray) or sizeof($anArray). This is useful when using an array of unknown size in a loop: $counter=0; while( $counter < sizeof($anArray) ) { echo "element $counter is {$anArray[$counter]}"; $counter++; } How big is $anArray? We don’t need to know! Just use count($anArray)or sizeof($anArray).

Using sizeof() $colors[] = "red"; // Set up the array $colors[] = "blue"; $colors[] = "purple"; $colors[] = "yellow"; $count=0; // Initialize array counter while( $count < sizeof($colors) ) { // Print out each array index and value echo "Color $counter is {$colors[$count]}<BR>"; $count++; }

Generating HTML with Loops <?php $users = array('Jason', 'Phil', 'Herbert', 'Anil'); echo '<center>'; echo '<table border = 2 >'; echo '<th>Number</th><th>Username</th>'; for ($i = 0; $i < count($users); $i++) { echo '<tr>'; echo "<td>$i</td>"; echo "<td>$users[$i]</td>"; echo '</tr>'; } echo '</table>'; ?>