Chapter 07 Arrays

download Chapter 07 Arrays

of 75

Transcript of Chapter 07 Arrays

  • 8/13/2019 Chapter 07 Arrays

    1/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Chapter 7:

    Arrays

  • 8/13/2019 Chapter 07 Arrays

    2/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    7.1 Arrays Hold Multiple Values

  • 8/13/2019 Chapter 07 Arrays

    3/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Arrays Hold Multiple Values

    Array: variable that can store multiplevalues of the same type

    Values are stored in adjacent memorylocations

    Declared usin [] operator:

    int tests[5];

  • 8/13/2019 Chapter 07 Arrays

    4/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Array ! Memory "ayout

    #he definition: int tests[5];

    allocates the follo$in memory:

    firstelement

    secondelement

    thirdelement

    fourthelement

    fifthelement

  • 8/13/2019 Chapter 07 Arrays

    5/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Array #erminolo y

    %n the definition int tests[5]; int is the data type of the array elements

    tests is the name of the array 5, in [5], is the si&e declarator. %t sho$s

    the number of elements in the array.

    #he si&e of an array is 'number ofelements( ) 'si&e of each element(

  • 8/13/2019 Chapter 07 Arrays

    6/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Array #erminolo y

    #he si&e of an array is: * the total number of bytes allocated for it

    * 'number of elements( ) 'number of bytes foreach element(

    +,amples:

    int tests[5] is an array of - bytes/

    assumin 0 bytes for an intlong double measures[10] is an array of bytes/ assumin bytes for a long double

  • 8/13/2019 Chapter 07 Arrays

    7/75Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    2i&e Declarators

    3amed constants are commonly used assi&e declarators.

    const int SIZE = 5;int tests[SIZE];

    #his eases pro ram maintenance $henthe si&e of the array needs to be chan ed.

  • 8/13/2019 Chapter 07 Arrays

    8/75Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    7.- Accessin Array +lements

  • 8/13/2019 Chapter 07 Arrays

    9/75Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Accessin Array +lements

    +ach element in an array is assi ned auni4ue subscript .

    2ubscripts start at

    0 1 2 3

    subscripts

    :

  • 8/13/2019 Chapter 07 Arrays

    10/75Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Accessin Array +lements

    #he last element5s subscript is n!1 $here n is the number of elements in the array.

    0 1 2 3subscripts:

  • 8/13/2019 Chapter 07 Arrays

    11/75Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Accessin Array +lements

    Array elements can be used as re ular variables:

    tests[0] = !";cout ## tests[0];cin $$ tests[1];tests[ ] = tests[0] % tests[1];

    Arrays must be accessed via individualelements:cout ## tests; && not legal

  • 8/13/2019 Chapter 07 Arrays

    12/75Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    (Program Continues)

  • 8/13/2019 Chapter 07 Arrays

    13/75Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Here are the contents of the 'ours array/ $ith the values

    entered by the user in the e,ample output:

  • 8/13/2019 Chapter 07 Arrays

    14/75Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Accessin Array 6ontents

    6an access element $ith a constant orliteral subscript:

    cout ## tests[3] ## endl;

    6an use inte er e,pression as subscript:

    int i = 5;cout ## tests[i] ## endl;

  • 8/13/2019 Chapter 07 Arrays

    15/75Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    sin a "oop to 2tep #hrou han Array +,ample * #he follo$in code defines an

    array/ numbers / and assi ns 88 to eachelement:

    const int ())(*+SIZE = 5;int numbers[())(*+SIZE];

    or -int count = 0; count # ())(*+SIZE; count%%. numbers[count] = "";

  • 8/13/2019 Chapter 07 Arrays

    16/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    A 6loser "oo9 At the "oop

  • 8/13/2019 Chapter 07 Arrays

    17/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Default %nitiali&ation

    lobal array all elements initiali&ed to 0 by default

    "ocal array all elements uninitialized bydefault

  • 8/13/2019 Chapter 07 Arrays

    18/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    7.;3o

  • 8/13/2019 Chapter 07 Arrays

    19/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    3o hen you use a value as an arraysubscript/ 6== does not chec9 it to ma9e

    sure it is a valid subscript.

    %n other $ords/ you can use subscripts

    that are beyond the bounds of the array.

  • 8/13/2019 Chapter 07 Arrays

    20/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    6ode ?rom @ro ram 7!

    #he follo$in code defines a three!element array/ and then $rites five valuesto itB

  • 8/13/2019 Chapter 07 Arrays

    21/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    >hat the 6ode Does

  • 8/13/2019 Chapter 07 Arrays

    22/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    3o

  • 8/13/2019 Chapter 07 Arrays

    23/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Cff!

  • 8/13/2019 Chapter 07 Arrays

    24/75

    Array Initialization7.0

  • 8/13/2019 Chapter 07 Arrays

    25/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    7.0 Array %nitiali&ation

  • 8/13/2019 Chapter 07 Arrays

    26/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Array %nitiali&ation

    Arrays can be initiali&ed $ith aninitiali&ation list:

    const int SIZE = 5;int tests[SIZE] = !",42,"1,!!,4 ;

    #he values are stored in the array in theorder in $hich they appear in the list.

    #he initiali&ation list cannot e,ceed thearray si&e.

    7!-

  • 8/13/2019 Chapter 07 Arrays

    27/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    6ode ?rom @ro ram 7!

  • 8/13/2019 Chapter 07 Arrays

    28/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    @artial Array %nitiali&ation

    %f array is initiali&ed $ith fe$er initialvalues than the si&e declarator/ theremainin elements $ill be set to 06

  • 8/13/2019 Chapter 07 Arrays

    29/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    %mplicit Array 2i&in

    6an determine array si&e by the si&e ofthe initiali&ation list:

    int 7ui88es[]= 12,1!,15,11 ;

    Must use either array si&e declarator orinitiali&ation list at array definition

    12 1! 15 11

  • 8/13/2019 Chapter 07 Arrays

    30/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    7.@rocessin Array 6ontents

  • 8/13/2019 Chapter 07 Arrays

    31/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    @rocessin Array 6ontents

    Array elements can be treated as ordinaryvariables of the same type as the array

    >hen usin %%/ operators/ don5tconfuse the element $ith the subscript:

    tests[i]%%; && add 1 to tests[i]

    tests[i%%]; && increment i, no && e ect on tests

  • 8/13/2019 Chapter 07 Arrays

    32/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Array Assi nment

    #o copy one array to another/ Don5t try to assi n one array to the other:

    ne9/ests = tests; && :on t 9or;else cout ## >/'e arra s are not e7ual Gn>;

  • 8/13/2019 Chapter 07 Arrays

    40/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    7.sin @arallel Arrays

  • 8/13/2019 Chapter 07 Arrays

    41/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    sin @arallel Arrays

    @arallel arrays: t$o or more arrays thatcontain related data

    A subscript is used to relate arrays:elements at same subscript are related

    Arrays may be of different types

  • 8/13/2019 Chapter 07 Arrays

    42/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    @arallel Array +,ample

    const int SIZE = 5; && (rra si8eint id[SIZE]; && student IHdouble a@erage[SIZE]; && course a@eragec'ar grade[SIZE]; && course grade

    or-int i = 0; i # SIZE; i%%.

    cout ## >Student IH6 > ## id[i]

    ## > a@erage6 > ## a@erage[i]## > grade6 > ## grade[i]## endl;

  • 8/13/2019 Chapter 07 Arrays

    43/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    (Program Continues)

  • 8/13/2019 Chapter 07 Arrays

    44/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    @ro ram 7!1- (Continued)

  • 8/13/2019 Chapter 07 Arrays

    45/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    #he 'ours and Ca )ate arrays are related throu h their subscripts :

  • 8/13/2019 Chapter 07 Arrays

    46/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    7.7 Arrays as ?unction Ar uments

  • 8/13/2019 Chapter 07 Arrays

    47/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Arrays as ?unction Ar uments

    #o pass an array to a function/ just use the arrayname:

    s'o9Scores-tests.;

    #o define a function that ta9es an arrayparameter/ use empty [] for array ar ument:@oid s'o9Scores-int [].;

    && unction Crotot Ce@oid s'o9Scores-int tests[].

    && unction 'eader

  • 8/13/2019 Chapter 07 Arrays

    48/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Arrays as ?unction Ar uments

    >hen passin an array to a function/ it is commonto pass array si&e so that function 9no$s ho$ manyelements to process:

    s'o9Scores-tests, ())(*+SIZE.; Array si&e must also be reflected in prototype/

    header:@oid s'o9Scores-int [], int.;

    && unction Crotot Ce

    @oid s'o9Scores-int tests[], int si8e.&& unction 'eader

    7!0

  • 8/13/2019 Chapter 07 Arrays

    49/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    (Program Continues)

  • 8/13/2019 Chapter 07 Arrays

    50/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    @ro ram 7!10 (Continued)

  • 8/13/2019 Chapter 07 Arrays

    51/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Modifyin Arrays in ?unctions

    Array names in functions are li9ereference variables * chan es made toarray in a function are reflected in actualarray in callin function

    3eed to e,ercise caution that array is notinadvertently chan ed by a function

  • 8/13/2019 Chapter 07 Arrays

    52/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    7.#$o!Dimensional Arrays

  • 8/13/2019 Chapter 07 Arrays

    53/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    #$o!Dimensional Arrays

    6an define one array for multiple sets ofdata

    "i9e a table in a spreadsheet

    se t$o si&e declarators in definition:

    const int ) :S = , D BS = 3;

    int eJams[) :S][D BS]; ?irst declarator is number of ro$sF

    second is number of columns

    #$o!Dimensional Array

  • 8/13/2019 Chapter 07 Arrays

    54/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    #$o!Dimensional ArrayGepresentation

    const int ) :S = , D BS = 3; inteJams[) :S][D BS];

    se t$o subscripts to access element:eJams[2][2] = 4K;

    eJams[0][0] eJams[0][1] eJams[0][2]

    eJams[1][0] eJams[1][1] eJams[1][2]

    e eJams[2][1] eJams[2][2]

    columns

    r o$s

  • 8/13/2019 Chapter 07 Arrays

    55/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

  • 8/13/2019 Chapter 07 Arrays

    56/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

  • 8/13/2019 Chapter 07 Arrays

    57/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    7! 7

  • 8/13/2019 Chapter 07 Arrays

    58/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    -D Array %nitiali&ation

    #$o!dimensional arrays are initiali&ed ro$!by!ro$:const int ) :S = 2, D BS = 2;int eJams[) :S][D BS] = 4 , !4 ,

    "2, "! ;

    6an omit inner / some initial values in a ro$ *array elements $ithout initial values $ill be set to 0 or LBB

    4 !4

    "2 "!

    #$o!Dimensional Array as

  • 8/13/2019 Chapter 07 Arrays

    59/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    #$o!Dimensional Array as@arameter/ Ar ument

    se array name as ar ument in function call:getEJams-eJams, 2.;

    se empty [] for ro$/ si&e declarator for column inprototype/ header:const int D BS = 2;&& Mrotot Ce@oid getEJams-int [][D BS], int.;

    && ?eader@oid getEJams-int eJams[][D BS], int ro9s.

    l * #h ' 9(

  • 8/13/2019 Chapter 07 Arrays

    60/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    +,ample * #he s'o9(rra ?unction from @ro ram 7!18

  • 8/13/2019 Chapter 07 Arrays

    61/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Ho$ s'o9(rra is 6alled

    2 i All th +l t i

  • 8/13/2019 Chapter 07 Arrays

    62/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    2ummin All the +lements in a#$o!Dimensional Array

    iven the follo$in definitions:const int LN+) :S = 5; && umber o ro9sconst int LN+D BS = 5; && umber o columnsint total = 0; && (ccumulatorint numbers[ LN+) :S][ LN+D BS] =

    2, !, ", K, , K, 1, 4, ", , , 3, !, 2, " , ", ", 0, 3, 1 , K, 2, !, , 1 ;

    2 mmin All the +lements in a

  • 8/13/2019 Chapter 07 Arrays

    63/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    2ummin All the +lements in a#$o!Dimensional Array

    && Sum t'e arra elementsor -int ro9 = 0; ro9 # LN+) :S; ro9%%.

    or -int col = 0; col # LN+D BS; col%%.

    total %= numbers[ro9][col];

    && HisCla t'e sumcout ## >/'e total is > ## total ## endl;

    2ummin the Go$s of a

  • 8/13/2019 Chapter 07 Arrays

    64/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    2ummin the Go$s of a#$o!Dimensional Array

    iven the follo$in definitions:const int LN+S/LHE /S = 3;const int LN+SD )ES = 5;double total; && (ccumulatordouble a@erage; && /o 'old a@erage scoresdouble scores[ LN+S/LHE /S][ LN+SD )ES] = 44, "!, !", 4K, " , 4K, "1, !4, !", 4 , 42, !3, !!, 42, 4" ;

    umm n t e o$s o a

  • 8/13/2019 Chapter 07 Arrays

    65/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    umm n t e o$s o a#$o!Dimensional

    Array&& Oet eac' student s a@erage scoreor -int ro9 = 0; ro9 # LN+S/LHE /S; ro9%%. && Set t'e accumulator total = 0; && Sum a ro9 or -int col = 0; col # LN+SD )ES; col%%. total %= scores[ro9][col]; && Oet t'e a@erage a@erage = total & LN+SD )ES; && HisCla t'e a@erage cout ## >Score a@erage or student > ## -ro9 % 1. ## > is > ## a@erage ##endl;

    2ummin the 6olumns of a

  • 8/13/2019 Chapter 07 Arrays

    66/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    2ummin the 6olumns of a#$o!Dimensional Array

    iven the follo$in definitions:const int LN+S/LHE /S = 3;const int LN+SD )ES = 5;double total; && (ccumulatordouble a@erage; && /o 'old a@erage scoresdouble scores[ LN+S/LHE /S][ LN+SD )ES] = 44, "!, !", 4K, " , 4K, "1, !4, !", 4 , 42, !3, !!, 42, 4" ;

    2ummin the 6olumns of a

  • 8/13/2019 Chapter 07 Arrays

    67/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    2ummin the 6olumns of a#$o!Dimensional Array

    && Oet t'e class a@erage or eac' scoreor -int col = 0; col # LN+SD )ES; col%%.

    && )eset t'e accumulator total = 0; && Sum a column or -int ro9 = 0; ro9 # LN+S/LHE /S; ro9%%. total %= scores[ro9][col]; && Oet t'e a@erage a@erage = total & LN+S/LHE /S; && HisCla t'e class a@erage cout ## >Dlass a@erage or test > ## -col % 1. ## > is > ## a@erage ## endl;

  • 8/13/2019 Chapter 07 Arrays

    68/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    7.8 Arrays $ith #hree or More

    Dimensions

    Arrays $ith #hree or More

  • 8/13/2019 Chapter 07 Arrays

    69/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Arrays $ith #hree or MoreDimensions

    6an define arrays $ith any number ofdimensions:

    s'ort rectSolid[2][3][5];double timeOrid[3][ ][3][ ];

    >hen used as parameter/ specify all but

    1st

    dimension in prototype/ headin :@oid get)ectSolid-s'ort [][3][5].;

  • 8/13/2019 Chapter 07 Arrays

    70/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    7.11%ntroduction to the 2#" @ector

  • 8/13/2019 Chapter 07 Arrays

    71/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    %ntroduction to the 2#" @ector

    A data type defined in the 2tandard#emplate "ibrary 'covered more in6hapter 1 (

    6an hold values of any type:@ector#int$ scores;

    Automatically adds space as more isneeded * no need to determine si&e atdefinition

    6an use [] to access elements

  • 8/13/2019 Chapter 07 Arrays

    72/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Declarin Vectors

    Eou must Pinclude#@ector$ Declare a vector to hold int element:

    @ector#int$ scores;

    Declare a vector $ith initial si&e ; :@ector#int$ scores-30.;

    Declare a vector and initiali&e all elements to :@ector#int$ scores-30, 0.;

    Declare a vector initiali&ed to si&e and contentsof another vector:

    @ector#int$ inals-scores.;

  • 8/13/2019 Chapter 07 Arrays

    73/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Addin +lements to a Vector

    se Cus'+bac< member function to addelement to a full array or to an array thathad no defined si&e:

    scores Cus'+bac

  • 8/13/2019 Chapter 07 Arrays

    74/75

    Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.

    Gemovin Vector +lements

    se CoC+bac< member function to remove lastelement from vector:

    scores CoC+bac

  • 8/13/2019 Chapter 07 Arrays

    75/75

    Cther seful Member ?unctions

    Member?unction

    Description +,ample

    at-elt. Geturns the value of the element atposition elt in the vector

    cout ##@ec1 at-i.;

    caCacit -. Geturns the ma,imum number ofelements a vector can store $ithoutallocatin more memory

    maJelts =@ec1 caCacit -.;

    re@erse-. Geverse the order of the elementsin a vector

    @ec1 re@erse-.;

    resi8e-elts,@al.

    Add elements to a vector/optionally initiali&es them

    @ec1 resi8e-5,0.;

    s9aC-@ec2. +,chan e the contents of t$ovectors

    @ec1 s9aC-@ec2.;