Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 9 Arrays Richard Gesick.

Similar presentations


Presentation on theme: "Lecture 9 Arrays Richard Gesick."— Presentation transcript:

1 Lecture 9 Arrays Richard Gesick

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, it’s 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 1 2 3 4 5 6 myArray

5 Creating Arrays <data type>[] <name> = new <data type> [<size>]; 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: An array of floats: An array of booleans:
short[] someArray = new short[50]; An array of floats: float[] myArray = new float[25]; An array of booleans: bool[] list = new bool[65]; An array of chars: char[] characters = new char[255];

7 Initializing Arrays with Values
<data type>[] <name> = new <data type> [<size>] {VALUES}; Or <data type>[] <name> = {VALUES};

8 Modifying an Array You must specify which slot you are putting information in Example: int[] myArray = new int[50]; myArray[3] = 12; This won’t work: myArray = 12; Data type on the left of = is an array Data type on right of = is an int 1 2 3 4 49 12

9 Accessing Information
Copying information out of a particular slot Example: int clientAge; clientAge = myArray[4]; 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 byte[50]; for (int counter=0; counter < 50; counter++) { myList[counter] = 42; }

11 Line by Line (a smaller example)
byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; }

12 Line by Line byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 1 2 3 4

13 Line by Line byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 1 2 3 4

14 Line by Line byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 1 2 3 4

15 Line by Line byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 1 2 3 4

16 Line by Line byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 1 2 3 4 42

17 Line by Line 1 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 1 1 2 3 4 42

18 Line by Line 1 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 1 1 2 3 4 42

19 Line by Line 1 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 1 1 2 3 4 42

20 Line by Line 1 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 1 1 2 3 4 42 42

21 Line by Line 2 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 2 1 2 3 4 42 42

22 Line by Line 2 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 2 1 2 3 4 42 42

23 Line by Line 2 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 2 1 2 3 4 42 42

24 Line by Line 2 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 2 1 2 3 4 42 42 42

25 Line by Line 3 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 3 1 2 3 4 42 42 42

26 Line by Line 3 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 3 1 2 3 4 42 42 42

27 Line by Line 3 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 3 1 2 3 4 42 42 42

28 Line by Line 3 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 3 1 2 3 4 42 42 42 42

29 Line by Line 4 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 4 1 2 3 4 42 42 42 42

30 Line by Line 4 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 4 1 2 3 4 42 42 42 42

31 Line by Line 4 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 4 1 2 3 4 42 42 42 42

32 Line by Line 4 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 4 1 2 3 4 42 42 42 42 42

33 Line by Line 5 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 5 1 2 3 4 42 42 42 42 42

34 Line by Line 5 byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } false counter 5 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 Let’s 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 = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } 1 2 3 4 smallestSoFar 42 17 42 -8 4

37 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } 1 2 3 4 smallestSoFar 42 17 42 -8 4

38 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } 1 2 3 4 smallestSoFar 42 42 17 42 -8 4

39 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 1 1 2 3 4 smallestSoFar 42 42 17 42 -8 4

40 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 1 1 2 3 4 smallestSoFar 42 42 17 42 -8 4

41 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 1 Is 42 > 17? 1 2 3 4 smallestSoFar 42 42 17 42 -8 4

42 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 1 1 2 3 4 smallestSoFar 17 42 17 42 -8 4

43 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 1 1 2 3 4 smallestSoFar 17 42 17 42 -8 4

44 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 2 1 2 3 4 smallestSoFar 17 42 17 42 -8 4

45 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 2 1 2 3 4 smallestSoFar 17 42 17 42 -8 4

46 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } Is 17 > 42? counter 2 1 2 3 4 smallestSoFar 17 42 17 42 -8 4

47 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 2 1 2 3 4 smallestSoFar 17 42 17 42 -8 4

48 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 3 1 2 3 4 smallestSoFar 17 42 17 42 -8 4

49 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 3 1 2 3 4 smallestSoFar 17 42 17 42 -8 4

50 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } Is 17 > -8? counter 3 1 2 3 4 smallestSoFar 17 42 17 42 -8 4

51 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 3 1 2 3 4 smallestSoFar -8 42 17 42 -8 4

52 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 3 1 2 3 4 smallestSoFar -8 42 17 42 -8 4

53 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 4 1 2 3 4 smallestSoFar -8 42 17 42 -8 4

54 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 4 1 2 3 4 smallestSoFar -8 42 17 42 -8 4

55 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } Is -8 > 4? counter 4 1 2 3 4 smallestSoFar -8 42 17 42 -8 4

56 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 4 1 2 3 4 smallestSoFar -8 42 17 42 -8 4

57 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 5 1 2 3 4 smallestSoFar -8 42 17 42 -8 4

58 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 5 1 2 3 4 smallestSoFar -8 42 17 42 -8 4

59 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; } counter 5 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, you’ll almost always use a loop


Download ppt "Lecture 9 Arrays Richard Gesick."

Similar presentations


Ads by Google