Arrays - PowerPoint PPT Presentation

1 / 60
About This Presentation
Title:

Arrays

Description:

Hold several values of the same type (homogeneous) Based on a slot number (the index number) ... int clientAge; clientAge = myArray[4]; This copies information ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 61
Provided by: jeffch8
Category:
Tags: arrays | clientage

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
2
Overview
  • Arrays and their properties
  • Creating arrays
  • Accessing array elements
  • Modifying array elements
  • Loops and arrays
  • Initialization
  • Searching

3
Arrays and Their Properties
  • Hold several values of the same type
    (homogeneous)
  • Based on a slot number (the index number)
  • Instant access
  • Linear (one after the other)
  • Static once their size is set, its set

4
What arrays look like
  • Things to notice
  • There are 7 slots, with index numbers 0 6
  • The name of the array is myArray
  • Easy to be off by one

0
1
2
3
4
5
6
myArray
5
Creating Arrays
  • ltdata typegt ltnamegt new ltdata typegt ltsizegt
  • new brings complex variables to life in C
  • Notice that we can create an array of any data
    type, just by changing the data type!

6
Examples
  • An array of shorts
  • short someArray new short50
  • An array of floats
  • float myArray new float25
  • An array of booleans
  • bool list new bool65
  • An array of chars
  • char characters new char255

7
Initializing Arrays with Values
  • ltdata typegt ltnamegt new ltdata typegt ltsizegt
    VALUES
  • Or
  • ltdata typegt ltnamegt VALUES

8
Modifying an Array
  • You must specify which slot you are putting
    information in
  • Example
  • int myArray new int50
  • myArray3 12
  • This wont work
  • int myArray new int50
  • myArray 12
  • Data type on the left of is an array
  • Data type on right of is an int

0
1
2
3
4
49

12
9
Accessing Information
  • Copying information out of a particular slot
  • Example
  • int clientAge
  • clientAge myArray4
  • This copies information from the fifth slot (slot
    four) into the variable clientAge

10
Initializing Arrays(large arrays)
  • For most arrays, you will use a loop to
    initialize
  • Problem create an array of 50 bytes and fill
    each slot with the number 42
  • Solution
  • byte myList new byte50
  • for (int counter0 counter lt 50 counter)
  • myListcounter 42

11
Line by Line(a smaller example)
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

12
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

0
1
2
3
4
0
0
0
0
0
13
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
0
0
1
2
3
4
0
0
0
0
0
14
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
0
0
1
2
3
4
0
0
0
0
0
15
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
0
0
1
2
3
4
0
0
0
0
0
16
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
0
0
1
2
3
4
42
0
0
0
0
17
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
1
0
1
2
3
4
42
0
0
0
0
18
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
1
0
1
2
3
4
42
0
0
0
0
19
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
1
0
1
2
3
4
42
0
0
0
0
20
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
1
0
1
2
3
4
42
42
0
0
0
21
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
2
0
1
2
3
4
42
42
0
0
0
22
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
2
0
1
2
3
4
42
42
0
0
0
23
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
2
0
1
2
3
4
42
42
0
0
0
24
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
2
0
1
2
3
4
42
42
42
0
0
25
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
3
0
1
2
3
4
42
42
42
0
0
26
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
3
0
1
2
3
4
42
42
42
0
0
27
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
3
0
1
2
3
4
42
42
42
0
0
28
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
3
0
1
2
3
4
42
42
42
42
0
29
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
4
0
1
2
3
4
42
42
42
42
0
30
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
4
0
1
2
3
4
42
42
42
42
0
31
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
4
0
1
2
3
4
42
42
42
42
0
32
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
4
0
1
2
3
4
42
42
42
42
42
33
Line by Line
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
5
0
1
2
3
4
42
42
42
42
42
34
Line by Line
false
  • byte myList new byte5
  • for (int counter 0 counter lt 5 counter)
  • myList counter 42

counter
5
0
1
2
3
4
42
42
42
42
42
35
Finding the Smallest Element
  • If you were given an array of numbers, how would
    YOU do it?
  • Computers can only compare two at a time
  • Go through entire list
  • Keep track of smallest so far
  • Lets assume
  • We have an array of 5 integers
  • The array contains random unknown numbers
  • The name of the array is called randomArray

36
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

0
1
2
3
4
smallestSoFar
42
17
42
-8
4
37
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

0
1
2
3
4
smallestSoFar
42
17
42
-8
4
38
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

0
1
2
3
4
smallestSoFar
42
42
17
42
-8
4
39
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
1
0
1
2
3
4
smallestSoFar
42
42
17
42
-8
4
40
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
1
0
1
2
3
4
smallestSoFar
42
42
17
42
-8
4
41
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

Is 42 gt 17?
counter
1
0
1
2
3
4
smallestSoFar
42
42
17
42
-8
4
42
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
1
0
1
2
3
4
smallestSoFar
17
42
17
42
-8
4
43
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
1
0
1
2
3
4
smallestSoFar
17
42
17
42
-8
4
44
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
2
0
1
2
3
4
smallestSoFar
17
42
17
42
-8
4
45
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
2
0
1
2
3
4
smallestSoFar
17
42
17
42
-8
4
46
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

Is 17 gt 42?
counter
2
0
1
2
3
4
smallestSoFar
17
42
17
42
-8
4
47
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
2
0
1
2
3
4
smallestSoFar
17
42
17
42
-8
4
48
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
3
0
1
2
3
4
smallestSoFar
17
42
17
42
-8
4
49
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
3
0
1
2
3
4
smallestSoFar
17
42
17
42
-8
4
50
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

Is 17 gt -8?
counter
3
0
1
2
3
4
smallestSoFar
17
42
17
42
-8
4
51
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
3
0
1
2
3
4
smallestSoFar
-8
42
17
42
-8
4
52
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
3
0
1
2
3
4
smallestSoFar
-8
42
17
42
-8
4
53
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
4
0
1
2
3
4
smallestSoFar
-8
42
17
42
-8
4
54
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
4
0
1
2
3
4
smallestSoFar
-8
42
17
42
-8
4
55
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

Is -8 gt 4?
counter
4
0
1
2
3
4
smallestSoFar
-8
42
17
42
-8
4
56
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
4
0
1
2
3
4
smallestSoFar
-8
42
17
42
-8
4
57
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
5
0
1
2
3
4
smallestSoFar
-8
42
17
42
-8
4
58
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
5
0
1
2
3
4
smallestSoFar
-8
42
17
42
-8
4
59
Another Trace
  • int smallestSoFar
  • smallestSoFar randomArray0
  • for (int counter 1 counter lt 5 counter)
  • if (smallestSoFar gt randomArraycounter)
  • smallestSoFar randomArraycounter
  • //if
  • //for

counter
5
0
1
2
3
4
smallestSoFar
-8
42
17
42
-8
4
60
Summary
  • Arrays hold values that are all the same type
  • Arrays are static in size
  • Creating arrays always follows a format
  • You can create an array of any type
  • To initialize or search arrays, youll almost
    always use a loop
Write a Comment
User Comments (0)
About PowerShow.com