Arrays Par Ti

download Arrays Par Ti

of 22

Transcript of Arrays Par Ti

  • 8/8/2019 Arrays Par Ti

    1/22

    C++ Arrays

  • 8/8/2019 Arrays Par Ti

    2/22

    Array Basics An array is a block of contiguous

    memory locations to hold values ofa single specified type.

    Arrays can hold values of ANY type

    In C++, arrays are 0-based. Thismeans that the first element hasindex 0.

  • 8/8/2019 Arrays Par Ti

    3/22

    Declaring an Array

    To declare an array, use thefollowing structure

    type VariableName [ SIZE ];

    Example:

    int myArray[5];declares an array named myArraythat can hold 5 integers with

    indices 0, 1, 2, 3, and 4.

  • 8/8/2019 Arrays Par Ti

    4/22

    Declaring an Array

    Note: We are using static arrays.

    IE the size must be set when thearray is declared, and can notchange during the course of theprogram.

    Use constant variables for thearrays size so that modifications tothe size can be made in one place.

  • 8/8/2019 Arrays Par Ti

    5/22

    Using an Array In C++, the name of the array

    refers to the address of the firstelement in the array.

    In order to access an element inthe array, follow the name of the

    array with square bracketscontaining the elements index.

  • 8/8/2019 Arrays Par Ti

    6/22

    Using an ArrayIn our example, int myArray[5];declares an array of 5 integers.

    M32 M36 M40 M44 M48

    myArray[0] myArray[1] myArray[2] myArray[3] myArray[4]

  • 8/8/2019 Arrays Par Ti

    7/22

    Using an Array Declaring an array does not

    automatically initialize the values.

    We can initialize our array in thedeclaration statement by assigningit a list of values. The list must be

    held in braces, {}. The values inthe list must be separated bycommas.

  • 8/8/2019 Arrays Par Ti

    8/22

    Using an ArrayExample:

    int myArray[5] = {14,3,7,0,1};

    Note: You can only initialize the

    array with a list in the arraydeclaration.

  • 8/8/2019 Arrays Par Ti

    9/22

    Using an Array If we are initializing an array of

    numeric values, any values left outof the list will be set to 0.

    Example: int Array[5] = {14};

    14 0 0 0 0

  • 8/8/2019 Arrays Par Ti

    10/22

    Using an Array You can leave out the size of the

    array when using a list to initializethe array in the declaration. Thearrays size will be the number ofvalues in the initialization list.

    Example:

    int Array[] = {14, 3, 6, 9, 2};

    14 3 6 9 2

  • 8/8/2019 Arrays Par Ti

    11/22

    Using an Array

    To read values into a non-characterarray, use a counter controlled loop

    to move through the array indices.Example:

    for( int i = 0; i < 5; ++i )

    {cout > myArray[ i ];

    }

  • 8/8/2019 Arrays Par Ti

    12/22

    Using an Array

    To output the values in a non-character array, use a counter

    controlled loop to move throughthe array indices.

    Example:

    for ( int i = 0; i < 5; ++i){

    cout

  • 8/8/2019 Arrays Par Ti

    13/22

    Using an Array Common Error: Overstepping the

    bounds of an array.

    Your compiler will not produce anerror if an index is used outside ofthe 0 to size 1 range.

    Invalid indices are used as anyother, moving the specified offsetfrom the first element in the array.

  • 8/8/2019 Arrays Par Ti

    14/22

    Using an Array Overstepping an arrays bounds

    can attempt to modify values in

    memory locations being used byother variables or programs.

  • 8/8/2019 Arrays Par Ti

    15/22

    Using an Array

    Example:

    int myArray[] = {1,2,3,4,5};

    for ( int i = 0; i

  • 8/8/2019 Arrays Par Ti

    16/22

    Passing Arrays to Functions

    To specify an array argument,place a set of empty bracketsbeside the arrays data type in thefunctions parameter list (forsingle-subscripted arrays).

    Example:

    void doubleValues( int [], int );

  • 8/8/2019 Arrays Par Ti

    17/22

    Passing Arrays to Functions

    When calling the function, use thename of the array (no brackets) asan argument of the function.

    Example:

    int myArray[8]={1,2,3,4,5,6,7,8};

    doubleValues( Array, 8 );

  • 8/8/2019 Arrays Par Ti

    18/22

    Passing Arrays to Functions

    When passing an array argumentto a function were sending theaddress of the first element in thearray, allowing the function directaccess to the array elements.

    Remember the Principle of LeastPrivilege! You can make array andreference parameters constant.

    void printArray( const int [], int );

  • 8/8/2019 Arrays Par Ti

    19/22

    Searching Arrays

    Unsorted arrays linear search.

    Linear Search Method

    Start at one end of the array.

    If the current element contains thesearch value, return its array index

    If not, move to the next element.

    Continue until the element is foundor there are no more elements to

    examine.

  • 8/8/2019 Arrays Par Ti

    20/22

    Searching Arrays

    Sorted arrays binary search.

    Binary Search

    If the middle element containsthe value youre searching forreturn its array index.

    Else, if the middle element islarger than the value youresearching for, search the leftsub-array.

    Else search the right sub-array.

  • 8/8/2019 Arrays Par Ti

    21/22

    Sorting Arrays

    Bubble Sort - Informal Algorithm

    Start at the end of the array

    swapping all pairs of unorderedelements until you reach thebeginning of the array.

    Now the smallest element is at thefront.

    Repeat the process until all of theelements are in order.

  • 8/8/2019 Arrays Par Ti

    22/22

    Sorting Arrays

    Selection Sort - Informal Algorithm

    front = position 0

    Find the smallest element and swap itto the front. Increment the front

    Repeat until front = size - 1