Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.

Similar presentations


Presentation on theme: "1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to."— Presentation transcript:

1 1 Arrays Chapter 9

2 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to array elements (Section 9.2)  Strings and arrays of characters  Inputting and outputting arrays example

3 3 Array Structure  An array of values is stored in consecutive memory locations  Each value in an array is called an array element  Array elements have a common name  The array as a whole is referenced through the common name  Array elements are of the same type

4 4 Array Structure (cont’d)  Individual elements of the array are referenced by sub-scripting the array name  element’s relative position used, beginning with 0  Subscripts (indexes) are denoted as integers (or expressions) within brackets: [ ]  The index range must be 0... n-1, for array of size n

5 5 Array Declaration int grades[10]; element-type array-name [array-size]; Type of all the values in the array Name of the entire collection of values Integer expression indicating number of elements in the array

6 6 Declartion Examples const int NUM_EMP = 10; bool onVacation[NUM_EMP]; int vacationDays[NUM_EMP]; float plantHours[7];

7 7 Array Initialization  List of initial values enclosed in braces ({ }) following assignment operator (=)  Values from initialization list are assigned in order to array elements  Length of initialization list cannot exceed size of the array  If too few values, only values given will be assigned  Size of array can be automatically set to number of initializing values using empty brackets ([ ])

8 8 Initialization Example element-type array-name [array-size] = {initialization- list}; float x[8] = {16.0, 12.0, 6.0, 8.0, 2.5, 12.0, 14.0, -54.5}; or float x[ ] = {16.0, 12.0, 6.0, 8.0, 2.5, 12.0, 14.0, -54.5};

9 9 Array Subscripts  Enclosed in brackets ([ ])  Indicates which element is referenced by position  Array subscript value is different than array element value  Subscript can be an expression of any integral type  To be valid, subscript must be a value between 0 and one less than the array size

10 10 Using Array Elements Examples cout << x[0]; x[3] = 25.0; sum = x[0] + x[1]; sum += x[2]; x[3] += 1.0; x[2] = x[0] + x[1];

11 Sequential Access to Array Elements  Random Access  Access elements is any order  Sequential Access  Process elements in sequential order starting with the first

12 12 Example of Sequential Access int cube[10]; for (int i = 0; i < 10; i++) cube[i] = i * i * i;

13 13 Strings and Arrays of Characters  string object uses an array of char  Can reference individual character of a string object in different ways  name[ i ]  name.at( i )  Other member functions of string class  message.length( i )

14 14 Strings and Arrays of Characters (cont’d)  Declaring and initializing char name[ ] = “Jackson”; // array size 8 or char name[8] = “Jackson”; // 7 characters  Last character stored is ‘\0’ to denote the end of the string in a character array

15 15 Reading and Writing Character Arrays  Can read or write entire character array at once cin >> name; cout << name << endl;  Can read or write each character of array  must account for null character at end of array

16 16 Inputting and Outputting Array Elements Example Write a complete C++ program to read in 10 integer values in an array, and then print the array in reverse order each array element on a separate line.

17 17 #include using namespace std; void main() { int values[10]; cout << “Please enter 10 integers”; for (int i = 0; i < 10; i++) cin >> values[i]; for (int i = 9; i >= 0; i--) cout << values[i] << endl; }


Download ppt "1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to."

Similar presentations


Ads by Google