1 / 23

Arrays 1.

Arrays 1. 0. Admin. 1. What is an array (review)? 2. Defining an array. 3. Accessing an array. 4. Loading methods. 5. Parallel / multi-dimensional arrays. 0. Admin. Next time: report due. Correction to schedule: Next class 11/13 we will do searching and sorting arrays.

creda
Download Presentation

Arrays 1.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays 1. 0. Admin. 1. What is an array (review)? 2. Defining an array. 3. Accessing an array. 4. Loading methods. 5. Parallel / multi-dimensional arrays.

  2. 0. Admin. • Next time: report due. • Correction to schedule: • Next class 11/13 we will do searching and sorting arrays.

  3. 1. What is an array? • An array is a… • Contiguous collection of homogeneous elements. • [Don’t call your friends this!] • What does “contiguous” mean? • What does “homogeneous” mean?

  4. What is an array (cont.)? • For example, consider an array of the daily high temperatures for a given week using 4 byte integers. Address Index Value 2000 [0] 45 2004 [1] 57 … .. .. 20?? [6] 32 What is address?

  5. 2. Defining an array. • To define an array, we must specify the type of element and allocate enough memory for the number of elements • E.g. float [] Attendance = new float [52]; creates an array of 52 float elements. char [ ] Name = new char [40]; creates an array of 40 characters, etc.

  6. Defining an array (cont.). • It is, however, better to use a named constant for the size, so that this can be easily changed. If all the algorithms that access an array use a hardcoded value, what would be the problem? const int MAX = 52; float [ ] Attendance = new float [MAX];

  7. Defining an array (cont.). • One can also have arrays of composite variables, e.g. an array of strings, which is essentially an array of character arrays or one can have arrays of more abstract entities, such as objects.

  8. 3. Accessing an array. • To access an element, we simply use/find the relevant subscript or index. • E.g. MyArray [5] accesses the 6th element. • Subscripted variables behave just like any other variable E.g. one can say MyArray [i] = Convert.ToDouble (Console.ReadLine()); or Console.Write (MyArray[i]);

  9. 4. Loading methods. • To load an array means to fill its elements with data. To unload an array means to display the contents of an array (or to move/copy them somewhere else). When loading or otherwise processing an array it is important to avoid a bounds error---meaning?

  10. Loading methods (cont).. • An easy way to load an array is to use a for loop. for (int i = 0; i < MAX; i++) { Console.Write ( “Enter attendance for week” + i+1); Attendance[i] = Convert.ToDouble (Console.ReadLine()); } The same approach can be used for unloading, totaling or any other uniform operation.

  11. Loading methods (cont).. However, this is a count-controlled loop. What is its limitation? It assumes we know how many times the loop will run, which assumes……??? A way round this is to allow two termination conditions. The loading loop terminates if we either reach the end of the array or find a sentinel.

  12. Loading methods (cont).. Since the user may not completely fill the array, we also need to distinguish the array (the physical container) from the list (the occupied portion of the array. The array may thus contain both a list of meaningful data plus “logical garbage.” It is important to distinguish the 2 so we only do list processing, i.e. we should only total/search/sort etc. values in the list, ignoring the garbage values. E.g. consider a DailySales array. See diagram.

  13. Loading methods (cont). Our algorithm needs to prevent going beyond the array (a bounds error), but must also allow the user to enter a list smaller than the array. The algorithm must also count the size of the list, so that after the load is completed, we only process the list, not the whole array (except when they happen to coincide).

  14. Loading methods (cont). Console.Write (“Enter a sale (-1 to quit)); Sale = Convert.ToDouble (Console.ReadLine()); Index = 0; while ((Sale != -1) && (Index < MAX)) { DailySales[Index] = Sale; Index++; Console.Write (“Enter a sale (-1 to quit)); Sale = Convert.ToDouble (Console.ReadLine()); }// end while;

  15. Loading methods (cont). Notice that the loop has 2 LCVs hence: Both have to be initialized; Both are tested in the loop condition; Both are updated. What important information does Index contain after the load terminates?

  16. Loading methods (cont). Index will be equal to the size of the list = the number of elements loaded. Because the first array location is zero, this means that the last element will be at location….? See diagram.

  17. Loading methods (cont). From now on, one can do list processing, only processing the meaningful data. E.g. for unloading the list, one can code: for (int i = 0; i < ListSize; i++) Console.Write (DailySales[i]);

  18. 5. Parallel and multi-dimensional arrays. At first sight, arrays present a problem: since the elements must be homogeneous (of the same data type), how can one handle a case where there is related data of different data types? E.g. suppose one wants a list of car part descriptions and their price. The car part description is a character string, the price is a float. How can we handle this?

  19. Parallel and multi-dimensional arrays (cont.). In fact, there are 2 main ways of handling this problem. One is to have an array of records (see later). The other is to set up 2 (or more) parallel arrays. Parallel arrays store data that refers to a common entity, and share a common subscript.

  20. Parallel and multi-dimensional arrays (cont.). See diagram. This makes it easy to load/unload and do other uniform operations on all of the arrays. On the other hand, it might be that all the data is of the same type (e.g. float), but you need a table of rows and columns (which we can imagine as rather like a spreadsheet).

  21. Parallel and multi-dimensional arrays (cont.). E.g. DailyWeather. Suppose that for each day of the week from Sunday to Saturday we want 4 readings: the High temperature, Low temperature, the High humidity and Low humidity. See diagram.

  22. Parallel and multi-dimensional arrays (cont.). To create this 2-dimensional array, we need to establish 2 subscripts. Note: C# is a “row-major” language, meaning we list rows before columns. Thus we can code: float [,] DailyWeather = new float [7, 4]; // This creates an array with 7 rows and 4 columns.

  23. Parallel and multi-dimensional arrays (cont.). To set the 3rd column (High humidity) of the 5th row (Thursday) to 75, we would code: DailyWeather [ 4, 2 ] = 75; Why? Next time: Sorting and searching arrays.

More Related