CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick
CSE 1301 Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays – Initialization – Searching
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…
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 myArray
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!
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];
CSE 1301 Initializing Arrays with Values [] = new [ ] {VALUES}; Or [] = {VALUES};
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 … 49 12
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
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; }
CSE 1301 Line by Line (a smaller example) byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; }
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; }
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter
CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter false
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
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 smallestSoFar
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 smallestSoFar
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 smallestSoFar
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 smallestSoFar 1 counter
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 smallestSoFar 1 counter
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 smallestSoFar 1 counter Is 42 > 17?
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 smallestSoFar 1 counter
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 smallestSoFar 1 counter
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 smallestSoFar 2 counter
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 smallestSoFar 2 counter
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 smallestSoFar 2 counter Is 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 smallestSoFar 2 counter
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 smallestSoFar 3 counter
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 smallestSoFar 3 counter
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 smallestSoFar 3 counter Is 17 > -8?
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 smallestSoFar 3 counter
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 smallestSoFar 3 counter
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 smallestSoFar 4 counter
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 smallestSoFar 4 counter
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 smallestSoFar 4 counter Is -8 > 4?
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 smallestSoFar 4 counter
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 smallestSoFar 5 counter
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 smallestSoFar 5 counter
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 smallestSoFar 5 counter
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