Chapter 9 Arrays.

Slides:



Advertisements
Similar presentations
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Advertisements

 Functions breakdown: Functions purpose Modularity Declaring and defining a function Calling a function Parameter passing Returning a value Reusability.
Lesson Four: More of the Same
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Lesson Three: Organization
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
CS0007: Introduction to Computer Programming Introduction to Arrays.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Lesson Two: Everything You Need to Know
1.2 Primitive Data Types and Variables
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/20/08.
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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Some of Chap 17.
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
An Introduction to Programming with C++ Sixth Edition
Chapter 10 Arrays.
© 2016 Pearson Education, Ltd. All rights reserved.
Data Types and Structures
Arrays Arrays exist in almost every computer language.
Chapter 7 Part 1 Edited by JJ Shepherd
Student Book An Introduction
CompSci 230 Software Construction
Chapter 7 Functions.
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Chapter 10 Arrays.
Arrays, For loop While loop Do while loop
Object-oriented Design in Processing
Chapter 9 Arrays.
Chapter 10 Algorithms.
EKT150 : Computer Programming
CISC101 Reminders Quiz 2 this week.
Chapter 10 Algorithms.
Variables Title slide variables.
More programming with "Processing"
Variables and Expressions
Object-oriented Design in Processing
CS2011 Introduction to Programming I Arrays (I)
Chapter 8 Objects.
MSIS 655 Advanced Business Applications Programming
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Just a question for Mac users:
Loops and Arrays in JavaScript
CHAPTER 2 Arrays and Vectors.
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Suggested self-checks: Section 7.11 #1-11
CHAPTER 2 Arrays and Vectors.
Programming games Share your plans for your virtual something.
Chapter 15, Images …a few points
Object-oriented Design in Processing
Chapter 10 Algorithms.
Chapter 4, Variables Brief Notes
Arrays 2-May-19.
C++ Array 1.
Chapter 13, Math A few Examples.
Web Programming and Design
Object-oriented Design in Processing
Chapter 9 Arrays.
MIS 222 – Lecture 12 10/9/2003.
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 link 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. Then do #7

Create a Bar Chart Remember https://processing.org/tutorials/arrays? Let’s do again. Then remix your own way.

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 moorec.people.cofc.edu/fam.txt)

I started my own homework…

Jitterbug Just another example. Nothing spectacular, but you can gain more experience on synthesizing current and past knowledge. Jitterbug[] bugs = new Jitterbug[10]; void setup() { size(240, 120); for( int i = 0; i< bugs.length; i++) { float x = random(width); float y = random(height); int r = i*2; //i did a remix here bugs[i] = new Jitterbug(x, y, r); } void draw() { for(int i = 0; i<bugs.length; i++){ bugs[i].display(); bugs[i].move(); Main program on left and class on right.

Create class without looking: Since we ran out of time, ignore the steps on these two pages. Instead we will open example 9-11. Then study and modify it. Create class without looking: The class is called Ball The data is: float x for horiz position float y for vert position float w for width of circle The method functions as follows: Fills with a color No stroke Draws an ellipse with x, y, w, w arguments

Continue Incrementally: Now pretend you only want an array that displays 3 balls. No movement, no gravity. Modify page 181 to only display 3 balls from an array.