Professional Documents
Culture Documents
Arrays
7.1
Arrays Hold Multiple Values
first element
second element
third element
fourth element
fifth element
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 of elements( ) 'si&e of each element(
Array #erminolo y
#he si&e of an array is:
* the total number of bytes allocated for it * 'number of elements( ) 'number of bytes for each element(
+,amples:
int tests[5] is an array of -. bytes/ assumin 0 bytes for an int long double measures[10]is an array of 1. bytes/ assumin 1 bytes for a long double
Copyright 2012 Pearson Education, Inc.
2i&e Declarators
3amed constants are commonly used as si&e declarators. const int SIZE = 5; int tests[SIZE]; #his eases pro ram maintenance $hen the si&e of the array needs to be chan ed.
subscripts 1 :0
(Program Continues)
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:
Default %nitiali&ation
:lobal array all elements initiali&ed to 0 by default "ocal array all elements uninitialized by default
7.;
3o <ounds 6hec9in in 6==
Cff!<y!Cne +rrors
An off!by!one error happens $hen you use array subscripts that are off by one. #his can happen $hen you start subscripts at 1 rather than .:
&& /'is code 'as an o,,0b10one error2 const int SIZE = 100; int numbers[SIZE]; ,or -int count = 1; count #= SIZE; count%%. numbers[count] = 0;
7.0
Array Initialization
7.0
Array %nitiali&ation
Array %nitiali&ation
Arrays can be initiali&ed $ith an initiali&ation list:
const int SIZE = 5; int tests[SIZE] = 3!",42,"1,!!,4 5;
#he values are stored in the array in the order in $hich they appear in the list. #he initiali&ation list cannot e,ceed the array si&e.
Copyright 2012 Pearson Education, Inc.
7!-D
Must use either array si&e declarator or initiali&ation list at array definition
7.A
@rocessin Array 6ontents
>hen this code is finished/ the 'ig'est variable $ill contains the hi hest value in the numbers array.
>hen this code is finished/ the lo9est variable $ill contains the lo$est value in the numbers array.
@artially!?illed Arrays
%f it is un9no$n ho$ much data an array $ill be holdin :
* Ma9e the array lar e enou h to hold the lar est e,pected number of elements. * 7se a counter variable to 9eep trac9 of the number of items stored in the array.
6omparin Arrays
#o compare t$o arrays/ you must compare element!by!element:
const int SIZE = 5; int ,irst(rra1[SIZE] = 3 5, 10, 15, 20, 25 5; int second(rra1[SIZE] = 3 5, 10, 15, 20, 25 5; bool arra1sE7ual = true; && Alag @ariable int count = 0; && BooC counter @ariable && DomCare t'e t9o arra1s2 9'ile -arra1sE7ual EE count # SIZE. 3 i, -,irst(rra1[count] F= second(rra1[count]. arra1sE7ual = ,alse; count%%; 5 i, -arra1sE7ual. cout ## >/'e arra1s are e7ual2Gn>; else cout ## >/'e arra1s are not e7ual2Gn>;
Copyright 2012 Pearson Education, Inc.
7.D
7sin @arallel Arrays
(Program Continues)
Copyright 2012 Pearson Education, Inc.
#he 'ours and Ca1)ate arrays are related throu h their subscripts:
7.7
Arrays as ?unction Ar uments
7!01
Copyright 2012 Pearson Education, Inc.
(Program Continues)
Copyright 2012 Pearson Education, Inc.
7.1
#$o!Dimensional Arrays
#$o!Dimensional Arrays
6an define one array for multiple sets of data "i9e a table in a spreadsheet 7se t$o si&e declarators in definition:
const int )I:S = , DIBS = 3; int eJams[)I:S][DIBS];
r o $ s
eJams[1][0] e
7!A7
Copyright 2012 Pearson Education, Inc.
-D Array %nitiali&ation
#$o!dimensional arrays are initiali&ed ro$!by!ro$:
const int )I:S = 2, DIBS = 2; int eJams[)I:S][DIBS] = 3 34 , !45, 3"2, "!5 5;
4 !4 "! "2
6an omit inner 3 5/ some initial values in a ro$ * array elements $ithout initial values $ill be set to 0 or =LBB
Copyright 2012 Pearson Education, Inc.
7se empty [] for ro$/ si&e declarator for column in prototype/ header: const int DIBS = 2; && Mrotot1Ce @oid getEJams-int [][DIBS], int.; && ?eader @oid getEJams-int eJams[][DIBS], int ro9s.
2ummin the Go$s of a #$o!Dimensional Array && Oet eac' student;s a@erage score2
,or -int ro9 = 0; ro9 # =LN+S/LHE=/S; ro9%%. 3 && Set t'e accumulator2 total = 0; && Sum a ro92 ,or -int col = 0; col # =LN+SDI)ES; col%%. total %= scores[ro9][col]; && Oet t'e a@erage a@erage = total & =LN+SDI)ES; && HisCla1 t'e a@erage2 cout ## >Score a@erage ,or student > ## -ro9 % 1. ## > is > ## a@erage ##endl; 5
Copyright 2012 Pearson Education, Inc.
7.8
Arrays $ith #hree or More Dimensions
>hen used as parameter/ specify all but 1st dimension in prototype/ headin :
@oid get)ectSolid-s'ort [][3][5].;
7.11
%ntroduction to the 2#" @ector
Automatically adds space as more is needed * no need to determine si&e at definition 6an use [] to access elements
Copyright 2012 Pearson Education, Inc.
Declarin Vectors
Eou must Pinclude#@ector$ Declare a vector to hold int element:
@ector#int$ scores;
Description
Geturns the value of the element at position elt in the vector
+,ample
cout ## @ec12at-i.;
maJelts = Geturns the ma,imum number of elements a vector can store $ithout @ec12caCacit1-.; allocatin more memory Geverse the order of the elements @ec12re@erse-.; in a vector Add elements to a vector/ optionally initiali&es them +,chan e the contents of t$o vectors @ec12resi8e-5,0.; @ec12s9aC-@ec2.;