1 / 18

Arrays

Arrays. Spring 2010. 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 int , char , double , boolean ,…, any data type. What good is a collection?.

selima
Télécharger la présentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays Spring 2010

  2. 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 int, char, double, boolean,…, any data type.

  3. 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!

  4. 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 (by default all will be 0)

  5. 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’}; char [ ] jane = new char[10];

  6. Name of array element: evens[2] evens[3] evens[4] evens[0] evens[1] 2 4 6 8 10 0 1 2 3 4 offset Start of array named evens 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};

  7. for-loops and arrays • They were designed to each other 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; }

  8. An 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] = 0.1 + (i / offset); // Set initial speed } }

  9. 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 } }

  10. Strings: Like an Array • Strings are similar to arrays of characters, but these are two very distinct data types String word = ”dictionary”; Char word[] = {’d’,’i’,’c’,’t’,’i’,’o’,’n’,’a’,’r’,’y’}; • Strings have special String-only functions that differ from those of arrays. Some of those String-only functions are substring(), toLowerCase() and charAt().

  11. Arrays of Strings String[] trees = { "lychee", "coconut", "fig"}; trees = shorten(trees); // Remove the last element from the array println(trees); // Prints "lychee coconut" trees = shorten(trees); // Remove the last element from the array println(trees); // Prints "lychee"

  12. Arrays of Strings String[] trees = { "lychee", "coconut", "fig"}; trees = append(trees, "guava"); // What does this do? trees = shorten(trees); println(trees); // What will be printed now?

  13. Arrays of Strings: length() vs length String[] trees = { "lychee", "coconut", "fig"}; trees = append(trees,"guava"); println(trees.length); //What is printed and why? println(trees[2].length()); //What is printed and why?

  14. Assignment 6 • Image Processing is the signal processing of an image. Begin with a png or jpg image of your choice. Using a mouse or keyboard event, trigger an image processing function which reads from and modifies the image pixel array. You must use a for loop to index into the array of pixels. The example code below has an example of reading and writing into that array. Use the image of your choice to be the background. • 10% Aesthetics • 10% zipped .pde file type • 10% image included in zip file • 20% for loop • 30% reading the pixel array • 20% modifying the pixel array

  15. Assignment 6 //Author: Susan Reiser //Oct 8, 2009 float radius = 1.0; int h = 0, dimension; float s = 1; PImage img; void setup() { size(500,500); background(0); colorMode(HSB,360,100,100); noStroke(); smooth(); img = loadImage("space.jpg"); //load background dimension = img.height * img.width; image(img,0,0); frameRate(10000); }

  16. Assignment 6 void draw() {} void keyPressed() { if (key == 'h') { img.loadPixels(); //increments hue 60 degrees // % makes sure hue never grows beyond 360 for (int i=0; i < dimension; i++) { img.pixels[i] = color((hue(img.pixels[i])+60)%360, saturation(img.pixels[i]), brightness(img.pixels[i])); } img.updatePixels(); image(img, 0, 0); } }

  17. Assignment 6 void mousePressed() { //loads the pixels from the display img.loadPixels(); } void mouseDragged(){ int h, s, b, j, i, currentIndex = mouseX-1 + (mouseY- 1)*img.width; color currentPixelColor; img.loadPixels(); //sums the nine pixels h,s,b centered at the mouse for (h=0, s=0,b=0,j = 0; j<3; j++) { for (i = 0; i<3; i++) { h += hue(img.pixels[currentIndex+i]); s += saturation(img.pixels[currentIndex+i]); b += brightness(img.pixels[currentIndex+i]); } currentIndex += img.width; }

  18. Assignment 6 //sets fill to the mean of the 9 pixels fill(color(h/9, s/9,constrain(b/9+20,0,100), 50)); //draws a circle wth the averaged fill at the location ellipse(mouseX, mouseY, 20,20); } void mouseReleased() { /* refreshes the pixels and redraws the image to the display */ img.updatePixels(); image(img, 0, 0); }

More Related