Arrays. An array is a collection “The Dinner offers an array of choices.” In computer programming, an array is a collection of variables of the same data.

Slides:



Advertisements
Similar presentations
Physical Computing INST. Kerem Odabaşı - YTU - Interactive Telecomunication Design Dept.
Advertisements

Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
C Language.
Image Processing … computing with and about data, … where "data" includes the values and relative locations of the colors that make up an image.
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
Code Elements and Processing Coordinate System. Code elements: pages Comments: are documentation notes that are ignored by the computer but are.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Computer Science 209 Software Development Equality and Comparisons.
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.
FUNDAMENTALS OF PROGRAMMING SM1204 Semester A 2010/2011.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
DSA Processing. Links Processing.org My Processing page Ben Fry Ben Fry’s Thesis on Computational Information DesignThesis Casey Reas siteCasey Reas Casey.
©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.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
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.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
Practical Electronics & Programming
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Variables and Arithmetic. From last class More drawing functions: strokeWeight(n); // higher the n value broader the stroke fill(k) ; // single parameter.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Objects. The Heart of the Matter - The "Black Box" Object-oriented software is all about objects. An object is a "black box" which receives and sends.
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
Lesson Two: Everything You Need to Know
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
Computer Science I Arrays. Parallel structures. Classwork/Homework: Build your own bouncing things.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
G RAPHICS & I NTERACTIVE P ROGRAMMING Lecture 2 More Programming with Processing.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
FUNDAMENTALS OF PROGRAMMING SM1204 SEMESTER A 2012.
Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Arrays. Arrays are objects that help us organize large amounts of information.
Review Objects – data fields – constructors – Methods Classes.
Arrays. 2 Why do we care (about arrays)? What if you have a whole bunch of cars (or aliens or balls or ???) bouncing around the screen? How do we keep.
CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Task 1 and Task 2. Variables in Java Programs Variables start with lower case letter Variables are descriptive of what they store Variables are one word.
Time Manager Class Activity Material Manager Writer leader Communicator Programmer Start a journey from the capital AbuDhabi to Alasmaa School using your.
Lecture 9 Arrays Richard Gesick.
Code Elements and Processing Coordinate System
Computers & Programming Languages
For Net Art Lecture 2 J Parker
Mouse Inputs in Processing
Code Elements and Processing Coordinate System
Default Arguments.
Arrays in Java.
Chapter 4, Variables Brief Notes
How About Some PI? Trigonometry Feb 18,2009.
Presentation transcript:

Arrays

An array is a collection “The Dinner offers an array of choices.” In computer programming, an array is a collection of variables of the same data type Arrays can be collections of ints, chars, floats, booleans,…, any data type. Example: String---a collection of characters

What good is a collection? Can you think of times when it would be useful to store a collection of values? –How about the display on the monitor---isn’t that a collection of pixel values? –Test grades---isn’t that a collection –College courses… –The path of a missile… –The names of all students… –Just about any list… We use collections all of the time!

How to Create an array An array of integers: int[ ] numbers = {1,2,3,4,5,6,7,8,9,10}; –This is an array with 10 elements An array with no values specified: int[ ] numbers = new int[10]; –This is an array large enough to store 10 elements but the elements have not been specified

Creating arrays An array of 10 doubles: double [ ] tomsGrades = {0,1.1,2.2,3,4.4,5.5,6,7,8,9.9}; double [ ] fredsGrades = new double[10]; An array of 10 characters char [ ] sally = {‘a’,’b’,’c’,’b’,…,’a’}; String sue = “abcbacdfaaaa”;  this is actually more than just an array char [ ] jane = new char[26];

Accessing the elements Each element in an array has a name---it’s the array plus an offset also called an index int [ ] evens = {2, 4, 6, 8, 10}; Name of array element: Start of array named evens 0 offset evens[0]evens[1] evens[2]evens[3]evens[4]

for-loops and arrays They were designed to be used together int[ ] numbers = new int[100]; for (int i = 0; i<100; i++) { numbers[i] = i; } OR int[ ] numbers = new int[100]; for (int i = 0; i<numbers.length; i++) { numbers[i] = i; }

Let it Snow int num = 11; int spacing = 40; int diameter = 20; float[] x = new float[num]; float[] y = new float[num]; void setup() { size(400, 400); noStroke(); smooth(); fill(255, 180); for (int i = 0; i < num; i++) { x[i]=i*spacing; y[i]=random(0,spacing); } } //continued on next page

Snow (continued) void draw() { background(0); for (int i = 0; i < num; i++) { ellipse(x[i], y[i], diameter, diameter); } // move down the canvas for (int i = 0; i < num; i++) { (y[i])++; }

Another example int numLines = 12; float[] x = new float[numLines]; float[] speed = new float[numLines]; float offset = 8; // Set space between lines void setup() { size(100, 100); smooth(); strokeWeight(10); for (int i = 0; i < numLines; i++) { x[i] = i; // Set initial position speed[i] = (i / offset); // Set initial speed } } //continued on next page

Continued… void draw() { background(204); for (int i = 0; i < x.length; i++) { x[i] += speed[i]; // Update line position if (x[i] > (width + offset)) { // If off the right, x[i] = -offset * 2; // return to the left } float y = i * offset; // Set y-coordinate for line line(x[i], y, x[i] + offset, y + offset); // Draw line }

Your Turn Modify the Snow example presented earlier to make the snow flakes reappear at the top of the canvas once they have moved off the bottom of the canvas.

Review and More Fun With Arrays int num = 50; int[] x = new int[num]; int[] y = new int[num]; void setup() { size(100, 100); noStroke(); smooth(); fill(255, 102); } //continued on next page

More Fun With Arrays (cont) void draw() { background(0); // Shift the values to the right for (int i = num - 1; i > 0; i--) { x[i] = x[i-1]; y[i] = y[i-1]; } // Add the new values to the beginning of the array x[0] = mouseX; y[0] = mouseY; // Draw the circles for (int i = 0; i < num; i++) { ellipse(x[i], y[i], i / 2.0, i / 2.0); }