Chapter 9 Arrays.

Slides:



Advertisements
Similar presentations
 Functions breakdown: Functions purpose Modularity Declaring and defining a function Calling a function Parameter passing Returning a value Reusability.
Advertisements

Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
Variables Conditionals Loops The concept of Iteration Two types of loops: While For When do we use them? Iteration in the context of computer graphics.
Lesson Four: More of the Same
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.
Lesson Three: Organization
Using Coordinate Geometry to Create Moving Images In unit 29 we created a simple drawing for the background of a children’s game. We are going to start.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
Classes / Objects An introduction to object-oriented programming.
Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/6/08.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
Introduction to Image processing using processing.
B. RAMAMURTHY Simulating Motion and Implementing Animation.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Lesson Two: Everything You Need to Know
Test 2 Review. General Info. All tests are comprehensive. You are still responsible for the material covered prior to the first exam. You will be tested.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
Review Expressions and operators Iteration – while-loop – for-loop.
+ This step by step tutorial demonstrates drawing a keyboard illustration using rectangles, grids, move and transform effects.
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.
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.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
CSC 211 Java I for loops and arrays.
David Meredith Aalborg University
Loop Lab CSD 340 (Blum).
EGR 2261 Unit 10 Two-dimensional Arrays
An Introduction to Programming with C++ Sixth Edition
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Data Types and Structures
Reading Netpbm Images.
Chapter 7 Part 1 Edited by JJ Shepherd
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
20 minutes maximum exhibits
Chapter 7 Functions.
Chapter 9 Arrays.
Chapter 6 Loops (iteration).
Chapter 7 Functions.
Structures Lesson xx In this module, we’ll introduce you to structures.
Learning Objective LO: We’re learning to understand when it is appropriate to use particular data types.
Arrays, For loop While loop Do while loop
One-Dimensional Array Introduction Lesson xx
Chapter 9 Arrays.
Organizing Memory in Java
For Net Art Lecture 2 J Parker
Chapter 10 Algorithms.
EKT150 : Computer Programming
ARRAYS 1 GCSE COMPUTER SCIENCE.
Chapter 10 Algorithms.
Variables Title slide variables.
More programming with "Processing"
Chapter 8 Objects.
IAT 265 Lecture 2: Java Loops, Arrays Processing setup, draw, mouse Shapes Transformations, Push/Pop.
ARRAYS 2 GCSE COMPUTER SCIENCE.
Moodle Training — Advanced Topics —
Just a question for Mac users:
JavaScript: Arrays.
Arrays CSE 120 Winter 2019 Instructor: Teaching Assistants:
Beginning C Lecture 5 Lecturer: Dr. Zhao Qinpei
LCC 6310 Computation as an Expressive Medium
Chapter 15, Images …a few points
Chapter 10 Algorithms.
Chapter 4, Variables Brief Notes
Chapter 13, Math A few Examples.
Web Programming and Design
Data Structures Photocopiable/digital resources may only be copied by the purchasing institution on a single site and for their own use © ZigZag.
Presentation transcript:

Chapter 9 Arrays

What is an Array An array is a special variable, which can hold a collection of elements of the same type. Examples: A list of colors A list of objects, such as rain drops or cars A list of players in a game A sequential list of numbers to associate with other variables such as x, y, w, h, color, etc.

Characteristics of Arrays Arrays start with 0. Arrays have a fixed size. Each element in an array has a unique position, in which the order is crucial. Each index/position of an array is matched with an element. E.g. array of your siblings you might be #2. The size can be: - A number - A variable of int type - An expression that calculates an integer

Declaring and Initializing Arrays Declaring: int [] myArray; String [] myWord = new String[6]; Declaring and initializing with values : float[] myNums = { 1.0, 3.2, 6, 9.5};

…from processing.org/tutorials/arrays, an easier way to remember array declarations   // Declare int[] data;   // Create data = new int[5]; // Assign data[0] = 19; data[1] = 40; data[2] = 75; data[3] = 76; data[4] = 90; // Declare & create int[] data = new int[5]; // Assign data[0] = 19;   data[1] = 40; data[2] = 75; data[3] = 76; data[4] = 90; // Declare, create, assign int[] data = { 19, 40, 75, 76, 90 };

Examples of specifying size color [] swatches = new color[4]; int [] storeLoc = new int [12]; int cities = 5; string [] mycities = new string [cities]; int [] family = new int [siblings+parents]

In summary, 3 ways to fill with value (initialize) Hard code the values in each spot. Type out a list in curly brackets separated by commas. Iterate through the elements in an array. See next slide…

Table: Three ways to initialize Hard code the values in each spot   String [] stuff = new String [4]; stuff[0] = "Buick"; stuff[1] = "Nissan"; stuff[2] = "Toyota"; stuff[3] = "Ford"; Type out a list in curly brackets separated by commas. String [] siblings = {"Ernest", "Mary", "Therman"}; The above two methods are inefficient for large arrays. The third option is most widely used. Also, there are more advanced ways such as filling with data from a text file. Iterate through the elements in an array. This also allows you to access each index value. for(int i = 0; i < stuff.length; i++ { }

Accessing elements in Arrays Several ways… Iterating through each element is one of them. See examples 9-5, 9-6, and 9-7. (remix)

Create a Bar Chart As promised, here is the bar chart shown during class Look at bar chart example at: https://processing.org/tutorials/arrays? Then remix your own way. PImage tiger ; int [] clem = {48,28,38,49,27,63,41,59,77, 27}; void setup() { size(300,200); tiger = loadImage("tiger.jpg"); } void draw() { //Tinted and added image tint(120); image(tiger, 0, 0 ); //black rectangle at top fill(0); rect(0,0,width, 40); //move remainder of items over 10px //then added text translate(10, 0); fill(#750FD1); textSize(22); text("Clemson's 2018 Scores", 0, 25); //Loop for the bars for (int j = 0; j < clem.length; j++) { fill(#E57417); rect(j*20, 170-clem[j], 16, clem[j] ); fill(255); textSize(11); text(clem[j] , j*20, 185);

Time for Fun Programming again #54 https://www.funprogramming.org A bit of a remix with names and descriptions of computer company founders

Do It Yourself: Create a String array with about 5 names of family members. Iterate through each. If time permits, use a for() to place a rectangle around each name. println() or text() to see results (See solution at http://moorec.people.cofc.edu/fam.txt )

Array with Objects Example 9-9 on pg. 177; not most visual appeal, but perfect example of how to use object as array. For time sake, copy the class. I’ll explain. Then let’s type the main program from scratch.

Interactive Stripes Example 9-10; lots of possibilities with this one. Once again, copy the class and let me explain. Then type the main program from scratch.

Let’s do a little sharing…I’ll explain //inspired by adult coloring books int xloc= 0; int yloc= 0; int size= 160; int spacer = size/2; //Program will randomly pick from these colors. //swatches better than totally random because you can better target your choices. color [] c = {#0ff0ff, #00ff22, #ff6609, #ccff00, #660033}; void setup() { size(660,390); background(255); frameRate(10); } void draw() { //noLoop() ; stroke(#ff6633); for(int xloc=0 ; xloc<=width ; xloc += spacer) { for(int yloc = 25; yloc <= height; yloc += spacer) { fill(c[int(random(0,4) )], 30); //fill with array "c" ellipse(xloc, yloc, size, size); stroke(#993366); ellipse(xloc, yloc, size*.5, size*.9); //smaller ellipse is oval fill(c[4]); ellipse(xloc, yloc, size/10, size/10); } //end of nested for This is my fork. I probably don’t remember how I did it, ha ha.

Another Example If time permits, look at another example class Jitterbug { float x; float y; int diameter; float speed = 5; Jitterbug(float tempX, float tempY, int tempDiameter) { x = tempX; y = tempY; diameter = tempDiameter; } void move() { x += random(-speed, speed); y += random(-speed, speed); x = constrain(x, 0, width); y = constrain(y, 0, height); void display() { ellipse(x, y, diameter, diameter) ; Jitterbug[] bugs = new Jitterbug[10]; void setup() { size(240, 120); for( int i = 0; i< bugs.length; i++) { bugs[i] = new Jitterbug(random(width), random(width), i*3); } void draw() { strokeWeight(.25); for(int i = 0; i<bugs.length; i++){ bugs[i].display(); bugs[i].move();