lect09 array

Upload: cheng-shiang

Post on 07-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Lect09 Array

    1/37

    --

    CSIS1117 Com uter Pro rammin

    1c1117 lecture 9

  • 8/6/2019 Lect09 Array

    2/37

    Arraysrrays Initialization

    Pass index variables

    - - Call-by-reference

    Multi-dimensional array

    c1117 lecture 9 2

  • 8/6/2019 Lect09 Array

    3/37

    ,

    many data items in the program for further.

    E.g. Write a program to read ten numbers, then

    them

    i bl variables numbers in the program. It is rather troublesome.

    -

    . .

    c1117 lecture 9 3

  • 8/6/2019 Lect09 Array

    4/37

    int x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;

    cin >> x1 >> x2 >> x3 >> x4 >> x5

    double mean = (x1 + x2 + x3 + x4 + x5 +

    x6 + x7 + x8 + x9 + x10 / 10.0

    cout

  • 8/6/2019 Lect09 Array

    5/37

    variables variables type.

    array - array purpose to form containers.

    o ec are an array, we ave o spec y e ypeof the data and the maximum no. of data to bes ore .

    _

    char myword[31]; // storing 31 char

    c1117 lecture 9 5

  • 8/6/2019 Lect09 Array

    6/37

    array variablesarray variables,can be accessed using the subscript operator [].

    const int max_no_input = 10;

    = > x[i];

    total += x[i];

    }

    double mean = total / max_no_input;cou

  • 8/6/2019 Lect09 Array

    7/37

    ,elements storing in an array with size n are

    -

    4

    8 1

    x1 x2 x3 x4 x5

    variables

    An array of

    elements

    98 4 1 0 -3 -8 6 3 -2

    [0] [9][0] x[9]

    c1117 lecture 9 7

  • 8/6/2019 Lect09 Array

    8/37

    To make ro rams more readable we can use delcare

    constants:

    const int SIZE=10;int x[SIZE];

    for (int i=0; i

  • 8/6/2019 Lect09 Array

    9/37

    ,

    elements of an array can be initialized by a list of.

    char choice[5];

    choice[0] = 'A';

    c o ce = 'B';

    choice[2] = 'C';

    choice[3] = 'D';

    choice[4] = 'E';

    = ' ' ' ' ' ' ' ' ' '

    The size can be omitted if it is equal to the no. of

    c1117 lecture 9 9

    ems n e s .

  • 8/6/2019 Lect09 Array

    10/37

    char choice 5 = 'A' 'B' 'C' 'D' 'E'

    cout

  • 8/6/2019 Lect09 Array

    11/37

    sizeof(variable)izeof(variable) used to store the

    variable.si eof(names) sizeof(names) array names.

    sizeof(names[0])izeof(names[0]) string.sizeof(names)/size(names[0]) stringizeof(names)/size(names[0]) stringelements in the array names.

    It can hel to make the ro ram more d namic.

    c1117 lecture 9 11

  • 8/6/2019 Lect09 Array

    12/37

    ,

    int N = sizeof(names) / sizeof(names[0]);

    for(int i = 0; i < N; ++i){ ... }

    The code of the for loop doesn't need to change

    if the size of the array names is changed.

    r e a program o rea n num ers an repor

    the minimum of them. We can declare an array with size 10 to store the 10

    numbers.

    c1117 lecture 9 12

  • 8/6/2019 Lect09 Array

    13/37

    Use a loo to read the 10 numbers from user

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

    Traverse the array sequentially and keep the smallest

    value seen so far.int min = a[0];

    for(int i = 1; i < 10; ++i)

    if(min > a[i]) min = a[i];

    The minimum value is kept in the variablemin after thefor loop.

    c1117 lecture 9 13

  • 8/6/2019 Lect09 Array

    14/37

    accessing a non-existence element of an array.

    - -

    int a[5] = {2, 3, 5, 7, 11};

    or n = ; ;

    cout

  • 8/6/2019 Lect09 Array

    15/37

    int p[5] = { -2, -3, -5, -7, -11 };

    int q[5] = { 100, 101, 102, 103, 104 };int a[5] = { 2, 3, 5, 7, 11 };

    int total = 0;

    for (int i = 0; i < x; ++i){total += a[i];

    cout

  • 8/6/2019 Lect09 Array

    16/37

    array array as a simple variable in passing to a function.

    ,

    if(x > y) return y;

    return x;

    You can treat it asan integer variable

    }int main(){

    int a 10

    and pass it to thefunction smaller.

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

    cin >> a[i];

    n m n = a ;

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

    min = smaller(min, a[i]);

    c1117 lecture 9 16}

  • 8/6/2019 Lect09 Array

    17/37

    the numbers in ascending order.

    array array,the elements in the array with the smallest at the frontand the largest at the end.

    How to sort the elements? Find the smallest element and ut it in the 1st entr of

    the array.

    Then find the 2nd

    smallest element and put it in the 2nd

    entry

    Until the end, the largest element is put in the last entry.

    c1117 lecture 9 17

  • 8/6/2019 Lect09 Array

    18/37

    Take the 10 numbers from input and store them in an array

    98 4 1 7 2 0 6 5 3

    a[0] a[9][0] a[9]Scan the array one time and find the smallest

    a[0] a[9]98 4 1 7 2 0 6 5 3

    Swap this smallest element with the first element

    a[0] a[9]

    90 4 1 7 2 8 6 5 3

    a[0] a[9]c1117 lecture 9 18

    a[0] a[9]

  • 8/6/2019 Lect09 Array

    19/37

    Similarly, find the 2nd smallest element byscanning the array once

    a[0] a[9]

    Swap it with the element in the 2nd entry of the array

    90 2 1 7 4 8 6 5 3

    [0] [9][0] a[9]

    c1117 lecture 9 19

  • 8/6/2019 Lect09 Array

    20/37

    Until the lar est element is ut at the end of the arra

    30 1 2 4 5 6 7 8 9

    The elements in the array are sorted, and we only

    a[0] a[9].

    How we can sort the elements in ro rams? It can be done by using nested loops.

    Outer loop: scan from i=0 to 9, the i-th iteration locatethe i-th smallest element and put it in the i-th entry.

    Inner loop: scan the array to locate the i-th smallestc1117 lecture 9 20

    .

  • 8/6/2019 Lect09 Array

    21/37

    index uses to store the index of the i-th smallest

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

    element in the i-th iteration.

    int index = i;

    for(int j = i + 1; j < 10; ++j)

    a n ex a

    index = j;

    int temp = a[index];Scan the array from the(i+1)-th elements to the last

    a[index] = a[i];

    a[i] = temp;

    one in the i-th iteration andupdate the variable index if

    Swap the i-th smallest elementvalue among them.

    c1117 lecture 9 21

    with the element in i-th entry

  • 8/6/2019 Lect09 Array

    22/37

    function function used in the previous program

    exchange their values in the array.

    Exchange thea[0] a[9]98 4 1 7 2 0 6 5 3

    e ement inposition 0 withswap(a[0], a[6])

    a[0] a[9]

    position 6.

    a[0] a[9]90 4 1 7 2 8 6 5 3

    c1117 lecture 9 22

    a[0] a[9]

  • 8/6/2019 Lect09 Array

    23/37

    void swap(int p, int q){

    p = q;

    q = temp;

    }

    int x = 10; int y = 20;

    cout

  • 8/6/2019 Lect09 Array

    24/37

    - -

    The variables x and are called value arameters.

    For value parameter, the argument is copied into the

    memor that belon s to the arameter. An chan esof the value are only valid to the parameter variablebut not the argument.

    void swap(int x, int y){

    int tem = x

    x10

    x = y;

    y = temp;

    swap(x, y);

    c1117 lecture 9 24Main memory

  • 8/6/2019 Lect09 Array

    25/37

    void swap(int p, int q){

    int tem =

    x10

    p = q;

    q = temp;

    swa x

    20 q

    Main memory

    When the swap function is called, the values ofvariables x and y are copied to the local variables

    c1117 lecture 9 25

    .

  • 8/6/2019 Lect09 Array

    26/37

    void swap(int p, int q){

    int tem =

    x10

    p = q;

    q = temp;

    swa x

    10

    tem

    q10

    Main memory

    Inside the swap function, only the local variables areupdated but not x and y. The values ofx and y are not

    c1117 lecture 9 26

    .

  • 8/6/2019 Lect09 Array

    27/37

    - -

    For reference parameter, the argument is the memory,.

    For referencing the memory address of a variable, we add

    void swap(int&p, int& q){

    =

    .

    p = q;

    q = temp;

    An argument for a referent parameter should be a

    c1117 lecture 9 27

    var a e, see roots.cc as an examp e.

  • 8/6/2019 Lect09 Array

    28/37

    void swap(int& p, int& q){

    int tem = x p = q;

    q = temp;y20 , q

    swa x

    i blMain memory

    en t e swap unct on s ca e , t e oca variablespand q are pointing to the same memory locations ofx

    c1117 lecture 9 28

    . .

  • 8/6/2019 Lect09 Array

    29/37

    void swap(int& p, int& q){

    p = q;

    q = temp;

    }

    int x = 10; int y = 20;

    cout

  • 8/6/2019 Lect09 Array

    30/37

    array array functions? Array in ++ n nl functionsrray functions

    reference. Usually the size of an array is also passed explicitly by

    caller. We do not need to add & before the array variable.void print_array(int a[], int size){

    for(int i = 0; i < size; ++i)cout

  • 8/6/2019 Lect09 Array

    31/37

    void sorting(int x[], int size){

    or nt = ; < s ze; ++

    int index = i;

    for(int j = i + 1; j < size; ++j)if(x[index] > x[j])

    index = j;

    ,

    }print_array(x,10);int main(){

    } int a[10];

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

    >>sorting(a, 10);

    return 0;

    as the argument

    c1117 lecture 9 31

  • 8/6/2019 Lect09 Array

    32/37

    int main

    a3

    int a[10];

    for(int i = 0; i < 10; ++i)8

    21c n >> a ;

    sorting(a, 10);return 0;

    -10

    }

    -32

    10 3 4 8 21 1 0 9 32 2

    c1117 lecture 9 32Main memory

  • 8/6/2019 Lect09 Array

    33/37

    void sorting(int x[],int size){

    for(int i=0; i

  • 8/6/2019 Lect09 Array

    34/37

    void sorting(int x[],int size){

    for(int i=0; i

  • 8/6/2019 Lect09 Array

    35/37

    -

    array array , , indexes. We call these kinds of arrays Multi-

    .

    A2D array is just an array of array.

    int mytable[3][8];// A table with 3 rows and 8 columns

    2D arrays are useful to implement table-like data

    , . . .

    c1117 lecture 9 35

  • 8/6/2019 Lect09 Array

    36/37

    Column

    Mytable[0][0]my a e

    Row

    Mytable[1][4]ytable[1][4]Mytable[2][7]

    c1117 lecture 9 36

  • 8/6/2019 Lect09 Array

    37/37

    - ,have bounds for all dimensions except the first one.

    int mytable[][3] = { {1, 0, 4}, {-1, 1, 2}};

    Same rule apply to pass argument in functions.vo pr nt_mu t array nt a , nt s ze

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

    =