Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Similar presentations


Presentation on theme: "CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick."— Presentation transcript:

1 CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

2 CSE 1301 Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays – Initialization – Searching

3 CSE 1301 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 CSE 1301 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 123456 myArray

5 CSE 1301 Creating Arrays [] = new [ ]; 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 CSE 1301 Examples An array of shorts: 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 CSE 1301 Initializing Arrays with Values [] = new [ ] {VALUES}; Or [] = {VALUES};

8 CSE 1301 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: int[] myArray = new int[50]; myArray = 12; – Data type on the left of = is an array – Data type on right of = is an int 01234 … 49 12

9 CSE 1301 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 CSE 1301 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 CSE 1301 Line by Line (a smaller example) byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; }

12 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 00000

13 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 00000 0 counter

14 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 00000 0 counter

15 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 00000 0 counter

16 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 420000 0 counter

17 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 420000 1 counter

18 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 420000 1 counter

19 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 420000 1 counter

20 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 000 1 counter

21 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 000 2 counter

22 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 000 2 counter

23 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 000 2 counter

24 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 00 2 counter

25 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 00 3 counter

26 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 00 3 counter

27 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 00 3 counter

28 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 0 3 counter

29 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 0 4 counter

30 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 0 4 counter

31 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 0 4 counter

32 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 4 counter

33 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 5 counter

34 CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } 01234 42 5 counter false

35 CSE 1301 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 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 smallestSoFar

37 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 smallestSoFar

38 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 42 smallestSoFar

39 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 42 smallestSoFar 1 counter

40 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 42 smallestSoFar 1 counter

41 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 42 smallestSoFar 1 counter Is 42 > 17?

42 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 17 smallestSoFar 1 counter

43 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 17 smallestSoFar 1 counter

44 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 17 smallestSoFar 2 counter

45 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 17 smallestSoFar 2 counter

46 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 17 smallestSoFar 2 counter Is 17 > 42?

47 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 17 smallestSoFar 2 counter

48 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 17 smallestSoFar 3 counter

49 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 17 smallestSoFar 3 counter

50 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 17 smallestSoFar 3 counter Is 17 > -8?

51 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 smallestSoFar 3 counter

52 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 smallestSoFar 3 counter

53 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 smallestSoFar 4 counter

54 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 smallestSoFar 4 counter

55 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 smallestSoFar 4 counter Is -8 > 4?

56 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 smallestSoFar 4 counter

57 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 smallestSoFar 5 counter

58 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 smallestSoFar 5 counter

59 CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for 01234 421742-84 smallestSoFar 5 counter

60 CSE 1301 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 "CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick."

Similar presentations


Ads by Google