Title: Arrays
1Chapter 4
21. Introduction
- Arrays are data structures consisting of related
items of the same type. - This chapter covers C-style pointer-based arrays.
- Arrays remain the same size throughout program
execution.
32. Arrays
- An array is a consecutive group of memory
locations that all have the same name and the
same type. - To refer to a particular location or element in
the array, we specify the name of the array and
the position number of the element. - The first element in every array is the zeroth
element. (c0) The second is referred to as c1
42. Arrays (cont.)
- The position number contained within square
brackets is more formally called a subscript. - A subscript must be an integer or an integer
expression. - If a program uses an expression as a subscript,
then the expression is evaluated to determine the
subscript. - x c6/2
53. Declaring Arrays
- To tell the compiler to reserve 12 elements for
an integer array named c, use the declaration
int c12 - Memory may be reserved for several arrays with a
single declaration. char b100, x27
64. Examples Using Arrays
- Using a for loop to initialize an array int
i, n10 for(i0 ilt10 i)
ni0 - for output in tabular format for(i0 ilt10
i) cout ltlt setw(7) ltlt i ltlt
setw(13) ltlt nI ltlt endl
74. Examples Using Arrays (cont.)
- Using initializer lists.
- int n10 0
- if there are fewer initializers than elements,
the rest are automatically initialized to zero. - int n 1, 2, 3, 4, 5
- If the array size is omitted from the
declaration, the number of elements in the array
will be the number in the initializer list.
84. Examples Using Arrays (cont.)
- Using initializer lists. (cont.)
- int n5 32, 27, 64, 18, 95, 14
- providing more initializers than there are
elements in an array is a syntax error.
94. Examples Using Arrays (cont.)
- Bounds Checking
- Referring to an element outside of the array
bounds is an execution-time logic error. It is
not a syntax error. - The (normally serious) effects of referencing
elements outside the array bounds are system
dependent.
104. Examples Using Arrays (cont.)
- Character Arrays
- A character array can be initialized using a
string literal - char string1 first
- Note first contains 5 characters plus the
special NULL terminator. - Input
- char string220
- cin gtgt string2
115. Passing Arrays to Functions
- To pass an array argument to a function, specify
the name of the array without any brackets. - int hourlyTemperature24 modifyArray(hourlyTempe
rature, 24) - Arrays are not passed by value.
125. Passing Arrays to Functions (cont.)
- For a function to receive an array, the
functions parameters must specify that an array
will be received. - The function header would be void
modifyArray(int b , int arraysize) - The function prototype would be void
modifyArray(int , int ) - Note variable names are ignored in prototypes.
135. Passing Arrays to Functions (cont.)
- The const qualifier.
- There may be situations in your program in which
a function should not be allowed to modify array
elements. - C provides the type qualifier const that can be
used to prevent modification of array values. - void tryToModify( const int )
146. Sorting Arrays
- Sorting data is one of the most important
computing applications. - In this chapter we discuss a simple sorting
scheme. In Chapter 15, we investigate more
complex schemes that yield superior performance.
156. Sorting Arrays (cont.)
- Swapping two array elements.
- Declare a temporary variable. int hold
- hold ai ai ai1 ai1
hold
166. Sorting Arrays (cont.)
- The code for(pass0 pass lt arraySize -1
pass) for(i0 iltarraySize-1 i)
if(aigtai1) hold ai
ai ai1 ai1 hold
177. Computing Mean, Median, and Mode Using Arrays
- The mean is the arithmetic average.
- add up the numbers in the array and divide by the
number of elements. - The median is the middle value.
- sort the array a.
- return asize/2
- The mode is the value that occurs most frequently.
188. Linear Search and Binary Search.
- Often, a programmer will be working with large
amounts of data stored in arrays. It may be
necessary to determine whether an array contains
a value that matches a certain key. - The process of finding a particular element in an
array is called searching. - In this section we discuss two searching
techniques linear search and binary search.
198. Linear Search and Binary Search. (cont.)
- Linear Search
- works well for small arrays or unsorted arrays.
- Compares each element of the array with the
search key. - Since the array is not in any particular order,
it is just as likely that the value will be found
in the first element as the last.
20- Linear Search (cont.) int linSearch(const int
array , int key, int size)
int i for(i0 i lt size
i) if(arrayi key) return i
return -1
218. Linear Search and Binary Search. (cont.)
- Binary Search
- If the array is sorted the Binary Search
technique can be used. - Locate the middle element and compare it to the
key. - if the key is less than the middle, search the
first half. - if the key is greater than the middle, search the
second half. - If the two are equal you are done.
22- Binary Search (cont.) int binSearch(int b ,
int key, int low, int high, int size) int
middle while(low lt high) middle
(low high)/2 if (key bmiddle)
return middle else if (key lt
bmiddle) high middle -1 else low
middle 1 return -1
238. Linear Search and Binary Search. (cont.)
- Binary Search Performance
- In a worst case scenario, searching an array of
1024 elements will take only 10 comparisons. - Repeatedly dividing 1024 by 2 (because after each
comparison we are able to eliminate half of the
array.) yields 512, 256, 128, 64, 32, 16, 8, 4,
2, and 1
249. Multiple-Subscripted Arrays
- A common use of multiple-subscripted arrays is to
represent tables of values consisting of
information arranged in rows and columns. - C compilers support at least 12 array
subscripts.
259. Multiple-Subscripted Arrays (cont.)
- Declaration
- int a23
- Declaration and Initialization.
- int b22 1,2, 3,4
- int array123 1,2,3, 4,5,6
- int array2231,2,3,4,5
- int array323 1,2, 4