Download presentation
Presentation is loading. Please wait.
Published byLewis Henry Modified over 9 years ago
2
1 One Dimensional Arrays Chapter 11
3
2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : 85 79 92 57 68 80... 0 1 2 3 4 5 98 99 Inspecting arrays Stepping through arrays Passing arrays as parameters
4
3 Design Problem l Consider a program to calculate class average Why?? ?
5
4 Add to Design Problem l Now your client says, I need to ALSO calculate and display “deviations” from the average Describe why this will or will NOT work
6
5 l Enter in the scores again l Use 100 separate variables » and cout and cin commands l Read (then re-read) from a file l The real answer … Possible Solutions Use arrays !!
7
6 Simple vs Structured Data Types l Simple data type => data element contains a single value l Structured data type => a data element contains a collection of data values x : 15 avg : 84.35 ch : ‘A’ scores : 85 79 92 57 68 80 name : ‘C’ ‘L’ ‘Y’ ‘D’ ‘E’
8
7 Arrays l Arrays are Structured Data Types l They have a means of accessing individual components l Values can be retrieved from and stored in the structure scores : 85 79 92 57 68 80 0 1 2 3 4 5 cout << scores[2]; scores[0] = 100;
9
8 One Dimensional Array l Structured collection of components » All of the same type l Structure given a single name l Individual elements accessed by index indicating relative position in collection l Type of elements stored in an array can be “just about” anything l Index of an array must be an integer
10
9 Use of Array for Our Problem l Store elements in array as read in l Go back and access for deviations Note declaration
11
10 Declaring Arrays l Syntax: Data_type Array_name [constant]; l Note declaration from our example Tells how many elements set aside
12
11 Declaring Arrays l Example specifies an array… » each element is an integer » there is space for 100 elements » the are numbered 0 through 99 scores : 85 79 92 57 68 80... 0 1 2 3 4 5 98 99
13
12 Accessing Individual Components l Use the name of the array l Followed by an integer expression inside the square brackets [ ] scores : 85 79 92 57 68 80... 0 1 2 3 4 5 98 99 max = scores[0]; for (x = 0; x max) max = scores[x]; Index can be: - constant - variable - expression MUST be an integer
14
13 Out of Bounds Index l What happens if … l C++ does NOT check for index out of range l Possible to walk off into “far reaches” of memory -- clobbers... » other variable locations ».exe code » the operating system (??) float f_list [50]; f_list [100] = 123.456; float f_list [50]; f_list [100] = 123.456;
15
14 Initializing Arrays in Declarations l Possible to declare the size & initialize l Possible to omit size at declaration » Compiler figures out size of array int results [5] = {14, 6, 23, 8, 12 } float prices [ ] = { 2.41, 85.06, 19.95, 3.91 }
16
15 Aggregate Operations l Defn => an operation on the data structure as a whole » as opposed to operation on a SINGLE element within the structure l Example » would be nice to read in a WHOLE array
17
16 Lack of Aggregate Operations l Would be nice but... C++ does NOT have... l Assignment operator for whole array l Arithmetic operations for whole array (think matrix) l Comparisons for arrays (not even = =) l Return of an array type by a function
18
17 How to Accomplish Aggregate Operations? l Most such tasks (assignment, read, write) can be performed some other way » CS II course will write “classes” to provide these functions l Otherwise » these operations must be performed by the programmer » element by element in a loop
19
18 Arrays as Parameters l This is one task that CAN be done to the WHOLE array l C++ always passes arrays by reference
20
19 Arrays as Parameters l The name of the array is a pointer constant l The address of the array is passed to the function l Size of the array also passed to control loop
21
20 Arrays as Parameters l Note the empty brackets in parameter list » A number can be placed here but it will be ignored
22
21 Sub-array Processing l Note we specified an array size of 100 » but we don’t anticipate that many scores l Array always declared larger than needed l Must keep track of how many have been used » this is our limit when doing other things to the array
23
22 Design Problem l Consider the task of keeping track of data about parts for manufacture » part number, description, qty needed, unit price
24
23 Design Problem l Use “Parallel” arrays l One array each for part num, descrip, qty, price l n th item in any one of the arrays associated with same n th item of all the arrays part #descripqtyprice A100xxx5 1.23 B25yyy 23 8.95 0 1 2
25
24 Testing and Debugging Hints l Range of legal index values is 0 to array_size - 1 l Individual elements of the array are of the component type l No aggregate operations in arrays » you must write the code to do this l If array parameter is incoming, specify formal parameter as const » prevents function from modifying
26
25 Testing and Debugging Hints l Omitting array size in declaration » when array declared formal parameter » when array initialized at declaration l Don’t pass component when function expects entire array l Declare array size as max ever needed » process only part of array which is used l Pass array name and length to functions which process array or sub array
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.