Chapter 6 Arrays Liang Introduction to Programming with




![Declaring Array Variables datatype array. Name[array. Size]; Example: double my. List[10]; C++ requires that Declaring Array Variables datatype array. Name[array. Size]; Example: double my. List[10]; C++ requires that](https://slidetodoc.com/presentation_image_h/f7f49085a8d790e69ccaf6e50564e87e/image-5.jpg)




![Array Initializers Declaring, creating, initializing in one step: data. Type array. Name[array. Size] = Array Initializers Declaring, creating, initializing in one step: data. Type array. Name[array. Size] =](https://slidetodoc.com/presentation_image_h/f7f49085a8d790e69ccaf6e50564e87e/image-10.jpg)
![Declaring, creating, initializing Using the Shorthand Notation double my. List[4] = {1. 9, 2. Declaring, creating, initializing Using the Shorthand Notation double my. List[4] = {1. 9, 2.](https://slidetodoc.com/presentation_image_h/f7f49085a8d790e69ccaf6e50564e87e/image-11.jpg)



![Initializing Character Arrays char city[] = {'D', 'a', 'l', 'a', 's'}; char city[] = Initializing Character Arrays char city[] = {'D', 'a', 'l', 'a', 's'}; char city[] =](https://slidetodoc.com/presentation_image_h/f7f49085a8d790e69ccaf6e50564e87e/image-15.jpg)







![Test. Array. cpp // Find the largest int max = numbers[0]; for (int i Test. Array. cpp // Find the largest int max = numbers[0]; for (int i](https://slidetodoc.com/presentation_image_h/f7f49085a8d790e69ccaf6e50564e87e/image-23.jpg)




- Slides: 27

Chapter 6 Arrays Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1

Motivations Often you will have to store a large number of values during the execution of a program. Suppose, for instance, that you need to read one hundred numbers, compute their average, and find out how many numbers are above the average. Your program first reads the numbers and computes their average, and then compares each number with the average to determine whether it is above the average. The numbers must all be stored in variables in order to accomplish this task. You have to declare one hundred variables and repeatedly write almost identical code one hundred times. From the standpoint of practicality, it is impossible to write a program this way. So, how do you solve this problem? Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 2

Objectives F F F To describe why an array is necessary in programming (§ 7. 1). To declare arrays (§ 7. 2. 1). To access array elements using indexed variables (§ 7. 2. 2). To initialize the values in an array (§ 7. 2. 3). To program common array operations (displaying arrays, summing all elements, finding min and max elements, random shuffling, shifting elements) (§ 7. 2. 4). To apply arrays in the Lotto. Numbers and Deck. Of. Cards problems (§§ 7. 3 -7. 4). To develop and invoke functions with array arguments (§§ 7. 5 -7. 6). To develop functions involving array parameters in the Count. Letters. In. Array problem (§ 7. 7). To search elements using the linear (§ 7. 8. 1) or binary search algorithm (§ 7. 8. 2). To sort an array using the selection sort (§ 7. 9. 1) To sort an array using the insertion sort (§ 7. 9. 2). To process strings using C-strings (§ 7. 10). Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 3

Introducing Arrays Array is a data structure that represents a collection of the same types of data. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 4
![Declaring Array Variables datatype array Namearray Size Example double my List10 C requires that Declaring Array Variables datatype array. Name[array. Size]; Example: double my. List[10]; C++ requires that](https://slidetodoc.com/presentation_image_h/f7f49085a8d790e69ccaf6e50564e87e/image-5.jpg)
Declaring Array Variables datatype array. Name[array. Size]; Example: double my. List[10]; C++ requires that the array size used to declare an array must be a constant expression. For example, the following code is illegal: int size = 4; double my. List[size]; // Wrong But it would be OK, if size is a constant as follow: const int size = 4; double my. List[size]; // Correct Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 5

Arbitrary Initial Values When an array is created, its elements are assigned with arbitrary values. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 6

Indexed Variables The array elements are accessed through the index. Array indices are 0 -based; that is, they start from 0 to array. Size-1. In the example in Figure 6. 1, my. List holds ten double values and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: array. Name[index]; For example, my. List[9] represents the last element in the array my. List. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 7

Using Indexed Variables FAfter an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in my. List[0] and my. List[1] to my. List[2] = my. List[0] + my. List[1]; F The following loop assigns 0 to my. List[0], 1 to my. List[1], …, and 9 to my. List[9]. for (int i=0; i<10; i++) { my. List[i] =i; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 8

No Bound Checking C++ does not check array’s boundary. So, accessing array elements using subscripts beyond the boundary (e. g. , my. List[-1] and my. List[11]) does not does cause syntax errors, but the operating system might report a memory access violation. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 9
![Array Initializers Declaring creating initializing in one step data Type array Namearray Size Array Initializers Declaring, creating, initializing in one step: data. Type array. Name[array. Size] =](https://slidetodoc.com/presentation_image_h/f7f49085a8d790e69ccaf6e50564e87e/image-10.jpg)
Array Initializers Declaring, creating, initializing in one step: data. Type array. Name[array. Size] = {value 0, value 1, . . . , valuek}; double my. List[4] = {1. 9, 2. 9, 3. 4, 3. 5}; Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 10
![Declaring creating initializing Using the Shorthand Notation double my List4 1 9 2 Declaring, creating, initializing Using the Shorthand Notation double my. List[4] = {1. 9, 2.](https://slidetodoc.com/presentation_image_h/f7f49085a8d790e69ccaf6e50564e87e/image-11.jpg)
Declaring, creating, initializing Using the Shorthand Notation double my. List[4] = {1. 9, 2. 9, 3. 4, 3. 5}; This shorthand notation is equivalent to the following statements: double my. List[4]; my. List[0] = 1. 9; my. List[1] = 2. 9; my. List[2] = 3. 4; my. List[3] = 3. 5; Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 11

CAUTION Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double my. List[4]; my. List = {1. 9, 2. 9, 3. 4, 3. 5}; Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 12

Implicit Size C++ allows you to omit the array size when declaring and creating an array using an initilizer. For example, the following declaration is fine: double my. List[] = {1. 9, 2. 9, 3. 4, 3. 5}; C++ automatically figures out how many elements are in the array. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 13

Partial Initialization C++ allows you to initialize a part of the array. For example, the following statement assigns values 1. 9, 2. 9 to the first two elements of the array. The other two elements will be set to zero. Note that if an array is declared, but not initialized, all its elements will contain “garbage”, like all other local variables. double my. List[4] = {1. 9, 2. 9}; Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 14
![Initializing Character Arrays char city D a l a s char city Initializing Character Arrays char city[] = {'D', 'a', 'l', 'a', 's'}; char city[] =](https://slidetodoc.com/presentation_image_h/f7f49085a8d790e69ccaf6e50564e87e/image-15.jpg)
Initializing Character Arrays char city[] = {'D', 'a', 'l', 'a', 's'}; char city[] = "Dallas"; This statement is equivalent to the preceding statement, except that C++ adds the character '