ch07

7.1 Introduction to Arrays

  • Definition: An array stores a collection of data of the same type.

    • Examples:

      • List of names

      • List of temperatures

  • Need for Arrays:

    • Simplifies the management of multiple variables (e.g., tracking multiple test scores).

    • Prevents the complexity of naming multiple variables (e.g., score1, score2,...).

7.2 Declaring an Array

  • Syntax: To declare an array in C++:

    • int score[5];

    • This creates an array of 5 integers: score[0], score[1], ..., score[4].

  • Key Terms:

    • Subscript: The number in brackets (e.g., [5]).

    • Index: Refers to the position of each element (0 to size-1).

7.3 Properties of Arrays

  • Indexed Variables: Each array variable is indexed (e.g., score[0] is the first element).

  • Base Type: All elements in an array share the same type.

  • Size: Defined at the moment of declaration.

7.4 Array Declaration Syntax

  • General Syntax:

    • Type_Name Array_Name[Declared_Size];

    • After declaring, an array will have indices from Array_Name[0] to Array_Name[Declared_Size - 1].

7.5 Memory Considerations

  • Memory Allocation: Each variable of the array is stored in consecutive memory locations.

  • Address: The address of the first indexed variable is retained; others are calculated based on the size.

7.6 Array Indexes

  • Valid Indexes: If an array is int a[6], valid indexes are 0 to 5.

  • Out of Range Indexes: Accessing indexes outside defined limits may lead to unintended behaviors without errors.

7.7 Initializing Arrays

  • Initialization: On declaration, values can be assigned as follows:

    • int children[3] = {2, 12, 1};

    • If fewer values are provided, remaining elements are set to zero.

  • Uninitialized Arrays: If no values are listed, the result is compiler-dependent, and it's advised against relying on default values.

7.8 Using Arrays with Functions

  • Function Arguments:

    • Arrays can be passed into functions similar to primitive data types, using an array parameter:

      • void myFunction(int a[]);

    • The function does not know the size of the array, so include a size parameter as well.

  • Const Modifier: To prevent array modification within functions, declare parameters as const.

7.9 Multidimensional Arrays

  • Definition: C++ supports arrays with two or more indices (e.g., char page[30][100]).

  • Accessing Elements: Elements accessed with separate brackets (e.g., page[0][1]).

  • Declaration Example:

    • For grades of students across quizzes: int grade[4][3]; (4 students, 3 quizzes).

7.10 Programming with Partially Filled Arrays

  • Dynamic Sizes: Use the maximum needed size but keep track of filled elements.

  • Function Parameters: Functions interacting with these arrays should know how many values are actually used, not just the declared size.

7.11 Searching and Sorting Arrays

  • Searching: Sequential search method for finding values in an array.

    • Returns index or -1.

  • Sorting methods:

    • Selection Sort and Bubble Sort prominently discussed as examples to arrange elements in order.

7.12 Conclusion

  • Understand:

    • The basic syntax and uses of arrays in programming.

    • How to declare, initialize, and utilize arrays effectively, including their functions and memory considerations.

  • Practical Applications:

    • Developed examples reinforce knowledge through practical coding solutions.