Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays

Description:

... An array of 10 characters char [ ] sally = { a , b , c , b ... Review and More Fun With Arrays int num = 50 ... Document presentation format: On ... – PowerPoint PPT presentation

Number of Views:165
Avg rating:3.0/5.0
Slides: 15
Provided by: Rebecc210
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
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 ints, chars, floats,
    booleans,, any data type.
  • Example String---a collection of characters

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---isnt that
    a collection of pixel values?
  • Test grades---isnt 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 int10
  • This is an array large enough to store 10
    elements but the elements have not been specified

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 double10
  • 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 char26

6
Accessing the elements
  • Each element in an array has a name---its 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 be used together
  • int numbers new int100
  • for (int i 0 ilt100 i)
  • numbersi i
  • OR
  • int numbers new int100
  • for (int i 0 iltnumbers.length i)
  • numbersi i

8
Let it Snow
  • int num 11
  • int spacing 40
  • int diameter 20
  • float x new floatnum
  • float y new floatnum
  • void setup()
  • size(400, 400)
  • noStroke()
  • smooth()
  • fill(255, 180)
  • for (int i 0 i lt num i)
  • xiispacing
  • yirandom(0,spacing)

  • //continued on next page

9
Snow (continued)
  • void draw()
  • background(0)
  • for (int i 0 i lt num i)
  • ellipse(xi, yi, diameter, diameter)
  • // move down the canvas
  • for (int i 0 i lt num i)
  • (yi)

10
Another example
  • int numLines 12
  • float x new floatnumLines
  • float speed new floatnumLines
  • float offset 8 //
    Set space between lines
  • void setup()
  • size(100, 100)
  • smooth()
  • strokeWeight(10)
  • for (int i 0 i lt numLines i)
  • xi i
    // Set initial position
  • speedi 0.1 (i / offset)
    // Set initial speed

  • //continued on next page

11
Continued
  • void draw()
  • background(204)
  • for (int i 0 i lt x.length i)
  • xi speedi // Update
    line position
  • if (xi gt (width offset)) // If off
    the right,
  • xi -offset 2 // return
    to the left
  • float y i offset // Set
    y-coordinate for line
  • line(xi, y, xi offset, y offset)
    // Draw line

12
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.

13
Review and More Fun With Arrays
  • int num 50
  • int x new intnum
  • int y new intnum
  • void setup()
  • size(100, 100)
  • noStroke()
  • smooth()
  • fill(255, 102)
  • //continued on next page

14
More Fun With Arrays (cont)
  • void draw()
  • background(0)
  • // Shift the values to the right
  • for (int i num - 1 i gt 0 i--)
  • xi xi-1
  • yi yi-1
  • // Add the new values to the beginning of the
    array
  • x0 mouseX
  • y0 mouseY
  • // Draw the circles
  • for (int i 0 i lt num i)
  • ellipse(xi, yi, i / 2.0, i / 2.0)
Write a Comment
User Comments (0)
About PowerShow.com