Đang chuẩn bị liên kết để tải về tài liệu:
Chapter 5 Array list
Không đóng trình duyệt đến khi xuất hiện nút TẢI XUỐNG
Tải xuống
If fewer values than size supplied Fills from beginning Fills "rest" with zero of array base type If array-size is left out Declares array with size required based on number of initialization values. | Chapter 5 Arrays Learning Objectives Introduction to Arrays Declaring and referencing arrays For-loops and arrays Arrays in memory Arrays in Functions Arrays as function arguments, return values Programming with Arrays Partially Filled Arrays, searching, sorting Multidimensional Arrays Introduction to Arrays Array definition: A collection of data of same type First "aggregate" data type Means "grouping" int, float, double, char are simple data types Used for lists of like items Test scores, temperatures, names, etc. Avoids declaring multiple simple variables Can manipulate "list" as one entity Declaring Arrays Declare the array allocates memory int score[5]; Declares array of 5 integers named "score" Similar to declaring five variables: int score[0], score[1], score[2], score[3], score[4] Individual parts called many things: Indexed or subscripted variables "Elements" of the array Value in brackets called index or subscript Numbered from 0 to size - 1 Accessing Arrays Access using | Chapter 5 Arrays Learning Objectives Introduction to Arrays Declaring and referencing arrays For-loops and arrays Arrays in memory Arrays in Functions Arrays as function arguments, return values Programming with Arrays Partially Filled Arrays, searching, sorting Multidimensional Arrays Introduction to Arrays Array definition: A collection of data of same type First "aggregate" data type Means "grouping" int, float, double, char are simple data types Used for lists of like items Test scores, temperatures, names, etc. Avoids declaring multiple simple variables Can manipulate "list" as one entity Declaring Arrays Declare the array allocates memory int score[5]; Declares array of 5 integers named "score" Similar to declaring five variables: int score[0], score[1], score[2], score[3], score[4] Individual parts called many things: Indexed or subscripted variables "Elements" of the array Value in brackets called index or subscript Numbered from 0 to size - 1 Accessing Arrays Access using index/subscript cout << score[3]; Note two uses of brackets: In declaration, specifies SIZE of array Anywhere else, specifies a subscript Size, subscript need not be literal int score[MAX_SCORES]; score[n+1] = 99; If n is 2, identical to: score[3] Array Usage Powerful storage mechanism Can issue command like: "Do this to ith indexed variable" where i is computed by program "Display all elements of array score" "Fill elements of array score from user input" "Find highest value in array score" "Find lowest value in array score" Array Program Example: Display 5.1 Program Using an Array (1 of 2) Array Program Example: Display 5.1 Program Using an Array (2 of 2) for-loops with Arrays Natural counting loop Naturally works well "counting thru" elements of an array Example: for (idx = 0; idx<5; idx++) { cout << score[idx] << "off by " << max – score[idx] << endl; } Loop control variable (idx) counts from 0 – 5 Major Array Pitfall Array indexes always start with zero! Zero is "first" number