1 / 18

Arrays

Arrays. Based on lecture notes from Dr. Deepak Kumar of Brynmawr Book pages 301-312. Variables. int x = 0; float delta = 0.483; color darkOliveGreen = color(85, 107, 47); String colorName = "Dark Olive Green"; PImage castle = loadImage (" myCastle.jpg ");.

kelli
Download Presentation

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 Based on lecture notes from Dr. Deepak Kumar of Brynmawr Book pages 301-312

  2. Variables • int x = 0; • float delta = 0.483; • color darkOliveGreen = color(85, 107, 47); • String colorName = "Dark Olive Green"; • PImagecastle = loadImage("myCastle.jpg");

  3. A Set of Sample Values Declaration float petroleum = 40.0; float coal = 23.0; float naturalGas = 22.0; float nuclear = 8.0; float renewable = 4.0; float hydropower = 3.0; float[] consumption; consumption = new float[6]; Creation index consumption

  4. A Set of Sample Values //Declare and create an array with size 6 float[] consumption = new float[6]; //store values consumption[0] = 40.0; consumption[1] = 23.0; consumption[2] = 22.0; consumption[3] = 8.0; consumption[4] = 4.0; consumption[5] = 3.0; Fixed size

  5. A Set of Sample Values //Define, create and initialize the data in an array float[] consumption = {40.0, 23.0, 22.0, 8.0, 4.0, 3.0};

  6. Arrays • // An array to hold the names of all the days in a weekString[] weekDays = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; • // two arrays, each containing high and low temperature valuesfloat[] highTemps, lowTemps; • int[] count; // an array of integers • PImage[] photos; // an array of photos • // An array to hold the names of months in a year String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; • // The colors in a rainbow color[] rainbow = {color(255, 0, 0), color(255, 127, 0), color(255, 255, 0), color (0, 255, 0), color (0, 0, 255), color (111, 0, 255), color (143, 0, 255)};

  7. Indexing, Size and Loops int[] n = newint[1000]; for (inti=0; i < n.length; i++) { n[i] = i; } int[] n = newint[1000]; for (inti=n.length-1; i>=0; i--) { n[i] = i; }

  8. for-each Loop • Syntax • for (variable : arrayName) { // do something with the value of variable } • Example String[] energySource = {"Petroleum", "Coal", "Natural Gas", "Nuclear", "Renewable", "Hydropower"}; for(String str : energySource) { println(str); }

  9. Example: A Simple Bar Graph String[] energySource = {"Petroleum", "Coal", "Natural Gas", "Nuclear", "Renewable", "Hydropower"}; float[] consumption = {40.0, 23.0, 22.0, 8.0, 4.0, 3.0};void setup() { size(400, 400); smooth(); } // setup() void draw() { // set up plot dimensions relative to screen size float x = width*0.1; float y = height*0.9; float delta = width*0.8/consumption.length; float w = delta*0.8; background(255); for (float value : consumption) { // draw the bar for value // first compute the height of the bar relative to sketch window float h = map(value, 0, 100, 0, height); fill(0); rect(x, y-h, w, h); x = x + delta; } } // draw()

  10. Array Operations • String[] energySource = {"Petroleum", "Coal", "Natural Gas", "Nuclear", "Renewable", "Hydropower"}; • float[] consumption = {40.0, 23.0, 22.0, 8.0, 4.0, 3.0};

  11. Printing println(energySource); println(consumption.length); println(consumption);

  12. Try it Given the following arrays, • String[] energySource = {"Petroleum", "Coal", "Natural Gas", "Nuclear", "Renewable", "Hydropower"}; • float[] consumption = {40.0, 23.0, 22.0, 8.0, 4.0, 3.0}; write commands to print the values from energySource and consumption in the format shown here:

  13. Min, Max and Soring • float smallest = min(consumption); • float largest = max(consumption); • println(sort(consumption)); • println(sort(energySource));

  14. Other Array Operation • Reverse the ordering of elements in an array • reverse() • Expand the size of the array • append(), expand() • Shorten it • shorten() • Concatenate or split arrays • concat(), subset(), splice() • Copy the contents of an array • arrayCopy()

  15. Analysis and Design Lets analyze the problem Need to display blank board Initialize board to some representation of the pictures: lets use number pairs (0,0), (1,1), (2,2)…(7,7) in the case where n = 4, number of tiles = 16, there are 8 pairs of pictures Let the pictures be pic0, pic1, pci2,..pic7 Lets identify the data structures and design the algorithm before development of the code.

  16. Initialize Abstraction of the board Random placement

  17. Algorithm • Initialize board • Display blank board • Setup random number for the tiles for the pictures • User selects tile 1: openTile1  row1, col1, tileNum1 • User selects tile 2: openTile2row2, col2, tileNum2 • Match the pair of tiles opened: matchPair() • If match, • Increment number of correct, • If all tiles done, display number of tries • Else no match, close tiles.

  18. Functional Decomposition • Processing functions: setup, draw, mousePressed void initializeBoard(int n) void findRandomPair(int j) void openTile1() void openTile2() void matchPair() Void closeTiles()

More Related