1 D Arrays 10 5 2007 1 Opening Discussion Let s look at solutions to the interclass problem Minute essay responses Doing derivatives Strings String tokenization GUIs Text adventure Arrays Do you have any questions about the assignment 2 Midterm Results You should ACE questions that basically come off of quizzes 3 Motivating Arrays Imagine if I gave you the following assignment I want you to read a file and count how often each letter occurs in it Given what we have covered what would you do What happens if I said count the occurrences of all ASCII characters The problem is that right now we declare variables that represent one memory location and must give each a name Arrays allow us to declare a variable linked to many memory locations and index into the array with an integer This allows us to get at the pieces using a loop 4 Declaring Arrays The syntax for declaring arrays is fairly simple After the variable name you put square brackets with a size in it type name number Old style C only allows constants for the number Normally literals or defines Apparently the newer standard allows variable sized arrays where the size can be any variable already in scope The declaration sets aside the number of elements you ask for with each one the size of the type So an array of ten doubles gets a block of 80 bytes 5 Accessing Arrays When we access the array we put square brackets after the name again The square brackets are an operator The index we want goes inside the brackets This can be used in any expression that accepts the type of the array The index should be between 0 and number 1 This is why most programmers always start their for loops counting at 0 6 Range Checking C does not do range checking on arrays This means if you declare an array with 10 elements and do something with the 15th C doesn t mind It s an error but C doesn t care If you go too far the operating system will care and throw a segmentation fault Accessing out of bounds indexes is a very common bug Writing to them can be particularly devastating These bugs are VERY hard to find so try hard to not go out of bounds on your arrays 7 Passing Arrays You can pass arrays into functions Typically you simply leave out the size of the array when you do this and have empty square brackets The variable sized arrays in the new standard have a different syntax for passing arrays Arrays do not know their size so when you pass them to a function you must almost always pass the number of elements as well Arrays are passed by reference Unlike normal values changing an array in a function does mess with the original 8 const Arguments If a function should not mess with an array that is passed to it you should declare the array to be const This is a good software engineering practice to get into as it allows the compiler to double check what you are doing 9 Minute Essay What are some things you might want to use arrays for in your programming Remember that there is no class next week That means you have lots of time to work on your assignment Interclass Problem This one is for the Monday after I get back Write functions that will find the minimum and maximum value in arrays of doubles 10
View Full Document