COSC 181 Foundations of Computer Programming Class 30 1 Next Test Monday April 20th 2 OBJECTIVES Chapter 7 In this chapter you will learn To use the array data structure to represent a set of related data items To use arrays to store sort and search lists and tables of values To declare arrays initialize arrays and refer to the individual elements of arrays To pass arrays to functions Basic searching and sorting techniques To declare and manipulate multidimensional arrays To use C Standard Library class template vector 3 7 1 Introduction 7 2 Arrays 7 3 Declaring Arrays 7 4 Examples Using Arrays 7 5 Passing Arrays to Functions 7 6 Case Study Class GradeBook Using an Array to Store Grades 7 7 Searching Arrays with Linear Search 7 8 Sorting Arrays with Insertion Sort 7 9 Multidimensional Arrays 7 10 Case Study Class GradeBook Using a Two Dimensional Array 7 11 Introduction to C Standard Library Class Template vector 7 12 Optional Software Engineering Case Study Collaboration Among Objects in the ATM System 7 13 Wrap Up 4 7 1 Introduction Arrays Data structures containing related data items of same type Always remain the same size once created Are static entities Character arrays can also represent strings C style pointer based arrays vs vectors object based Vectors are safer and more versatile 5 7 2 Arrays Array Consecutive group of memory locations All of which have the same type Index Position number used to refer to a specific location element Also called subscript Place in square brackets Must be positive integer or integer expression First element has index zero Example assume a 5 and b c a b 2 Adds 2 to array element c 6 11 6 Fig 7 1 Array of 12 elements 7 7 2 Arrays Cont Examine array c in Fig 7 1 c is the array name c has 12 elements c 0 c 1 c 11 The value of c 0 is 45 Brackets used to enclose an array subscript are actually an operator in C 8 Common Programming Error 7 1 It is important to note the difference between the seventh element of the array and array element 7 Array subscripts begin at 0 so the seventh element of the array has a subscript of 6 while array element 7 has a subscript of 7 and is actually the eighth element of the array Unfortunately this distinction frequently is a source of off by one errors To avoid such errors we refer to specific array elements explicitly by their array name and subscript number e g c 6 or c 7 9 Fig 7 2 Operator precedence and associativity Operators Associativity Type left to right highest static cast type operand left to right unary postfix right to left unary prefix left to right multiplicative left to right additive left to right insertion extraction left to right relational left to right equality left to right logical AND left to right logical OR right to left conditional right to left assignment left to right comma 10 7 3 Declaring Arrays Declaring an array Arrays occupy space in memory Programmer specifies type and number of elements Example int c 12 c is an array of 12 ints Array s size must be an integer constant greater than zero Arrays can be declared to contain values of any non reference data type Multiple arrays of the same type can be declared in a single declaration Use a comma separated list of names and sizes 11 Good Programming Practice 7 1 We prefer to declare one array per declaration for readability modifiability and ease of commenting 12 7 4 Examples Using Arrays Using a loop to initialize the array s elements Declare array specify number of elements Use repetition statement to loop for each element Use body of repetition statement to initialize each individual array element 13 1 Fig 7 3 f ig07 03 cpp 2 3 inc lude iostream 4 us ing std cout 5 us ing std endl In i t ia l i z i n g Outline an array fig07 0 6 7 inc lude iomanip 8 us ing std setw 9 10 int main 11 12 int n 10 n is an array of 10 integers 13 14 initialize elements of array n to 0 15 for int i 0 i 10 i 16 3 cpp Declare n as an array of ints with 10 elements 1 Each int initialized is to 0 n i 0 set element at location i to 0 17 18 cout Element setw 13 Value endl 14 of 2 19 20 output each array element s value 21 for int j 0 j 10 j 22 cout setw 7 j setw 13 n j endl Outline 23 24 return 0 indicates successful termination fig07 0 25 end main Element 0 1 2 3 4 5 6 7 8 9 Value 0 0 0 0 0 0 0 0 0 0 n j returns int associated with index j in array n 3 cpp 2 Each int has been initialized to 0 15 of 2 7 4 Examples Using Arrays Using a loop to initialize the array s elements Declare array specify number of elements Use repetition statement to loop for each element Use body of repetition statement to initialize each individual array element 16 1 Fig 7 3 f ig07 03 cpp 2 3 inc lude iostream 4 us ing std cout 5 us ing std endl In i t ia l i z i n g Outline an array fig07 0 6 7 inc lude iomanip 8 us ing std setw 9 10 int main 11 12 int n 10 n is an array of 10 integers 13 14 initialize elements of array n to 0 15 for int i 0 i 10 i 16 3 cpp Declare n as an array of ints with 10 elements 1 Each int initialized is to 0 n i 0 set element at location i to 0 17 18 cout Element setw 13 Value endl 17 of 2 19 20 output each array element s value 21 for int j 0 j 10 j 22 cout setw 7 j setw 13 n j endl Outline 23 24 return 0 indicates successful termination fig07 0 25 end main Element 0 1 2 3 4 5 6 7 8 9 Value 0 0 0 0 0 0 0 0 0 0 n j returns int associated with index j in array n 3 cpp 2 Each int has been initialized to 0 18 of 2 7 4 Examples Using Arrays Cont Initializing an array in a declaration with an initializer list Initializer list Items enclosed in braces Items in list separated by commas Example int n 10 20 30 40 50 Because array size is omitted in the declaration the compiler determines the size of the array based on the size of the initializer list Creates a five element array Index values are 0 1 2 3 4 Initialized to values 10 20 30 40 50 respectively 19 7 4 Examples Using Arrays Cont Initializing an array in a declaration …
View Full Document