Chapter 4 - Arrays

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. | Chapter 4 - Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays Case Study: Computing Mean, Median and Mode Using Arrays Searching Arrays: Linear Search and Binary Search Multiple-Subscripted Arrays Introduction Arrays Structures of related data items Static entity (same size throughout program) A few types Pointer-based arrays (C-like) Arrays as objects (C++) Arrays Array Consecutive group of memory locations Same name and type (int, char, etc.) To refer to an element Specify array name and position number (index) Format: arrayname[ position number ] First element at position 0 N-element array c c[ 0 ], c[ 1 ] c[ n - 1 ] Nth element as position N-1 Arrays Array elements like other variables Assignment, printing for an integer array c c[ 0 ] = 3; cout Arrays c[6] -45 6 0 72 1543 | Chapter 4 - Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays Case Study: Computing Mean, Median and Mode Using Arrays Searching Arrays: Linear Search and Binary Search Multiple-Subscripted Arrays Introduction Arrays Structures of related data items Static entity (same size throughout program) A few types Pointer-based arrays (C-like) Arrays as objects (C++) Arrays Array Consecutive group of memory locations Same name and type (int, char, etc.) To refer to an element Specify array name and position number (index) Format: arrayname[ position number ] First element at position 0 N-element array c c[ 0 ], c[ 1 ] c[ n - 1 ] Nth element as position N-1 Arrays Array elements like other variables Assignment, printing for an integer array c c[ 0 ] = 3; cout Arrays c[6] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c Declaring Arrays When declaring arrays, specify Name Type of array Any data type Number of elements type arrayName[ arraySize ]; int c[ 10 ]; // array of 10 integers float d[ 3284 ]; // array of 3284 floats Declaring multiple arrays of same type Use comma separated list, like regular variables int b[ 100 ], x[ 27 ]; Examples Using Arrays Initializing arrays For loop Set each element Initializer list Specify each element when array declared int n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements 0 If too many syntax error To set every element to same value int n[ 5 ] = { 0 }; If array size omitted, initializers determine size int n[] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array (1 of 2) 1 // Fig. : .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
34    439    5    22-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.