Chapter 5 ABSOLUTE C++ Arrays WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 5 Arrays
Learning Objectives Introduction to Arrays Declaring and referencing arrays ◆For-loops and arrays ◆Arrays in memory ◆Arrays in Functions Arrays as function arguments,return values Programming with Arrays Partially Filled Arrays,searching,sorting Multidimensional Arrays Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives ¨ Introduction to Arrays ¨ Declaring and referencing arrays ¨ For-loops and arrays ¨ Arrays in memory ¨ Arrays in Functions ¨ Arrays as function arguments, return values ¨ Programming with Arrays ¨ Partially Filled Arrays, searching, sorting ¨ Multidimensional Arrays
Introduction to Arrays ◆Array definition: A collection of data of same type First "aggregate"data type ◆Means"grouping" int,float,double,char are simple data types Used for lists of like items Test scores,temperatures,names,etc. Avoids declaring multiple simple variables Can manipulate "list"as one entity Copyright006 Pearson Addison-Wesley.All rights reserved. 5-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-3 Introduction to Arrays ¨ Array definition: ¨ A collection of data of same type ¨ First "aggregate" data type ¨ Means "grouping" ¨ int, float, double, char are simple data types ¨ Used for lists of like items ¨ Test scores, temperatures, names, etc. ¨ Avoids declaring multiple simple variables ¨ Can manipulate "list" as one entity
Declaring Arrays ◆ Declare the array allocates memory int score[5]; Declares array of 5 integers named "score" Similar to declaring five variables: int score[0],score[1],score[2],score[3],score[4] Individual parts called many things: Indexed or subscripted variables "Elements"of the array Value in brackets called index or subscript Numbered from 0 to size-1 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-4 Declaring Arrays ¨ Declare the array allocates memory int score[5]; ¨ Declares array of 5 integers named "score" ¨ Similar to declaring five variables: int score[0], score[1], score[2], score[3], score[4] ¨ Individual parts called many things: ¨ Indexed or subscripted variables ¨ "Elements" of the array ¨ Value in brackets called index or subscript ¨ Numbered from 0 to size - 1
Accessing Arrays Access using index/subscript ◆cout<<score[3]; Note two uses of brackets: In declaration,specifies SIZE of array Anywhere else,specifies a subscript Size,subscript need not be literal int score[MAX SCORES]; ◆score[n+1]=99; If n is 2,identical to:score[3] Copyright006 Pearson Addison-Wesley.All rights reserved. 5-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-5 Accessing Arrays ¨ Access using index/subscript ¨ cout << score[3]; ¨ Note two uses of brackets: ¨ In declaration, specifies SIZE of array ¨ Anywhere else, specifies a subscript ¨ Size, subscript need not be literal ¨ int score[MAX_SCORES]; ¨ score[n+1] = 99; ¨ If n is 2, identical to: score[3]