LOGO ObjectOriented Programming Using C Understanding Arrays Strings
LOGO Object-Oriented Programming Using C++ Understanding Arrays, Strings, and Pointers
Objectives v. Learn about memory addresses v. Learn about arrays v. Store values in an array v. Access and use array values § Avoid common errors with arrays v. Learn techniques to access part of an array 2
Objectives (continued) v. Use parallel arrays v. Learn how to use strings v. Discover special string-handling problems v. Learn about pointers v. Use a pointer in place of an array name 3
Understanding Memory Addresses v. Each location where a piece of data can be stored is identified by a memory address v. When you declare a variable with a statement such as int my. Age; § The computer chooses an available memory location § Associates the name my. Age with the memory address of that location 4
Understanding Memory Addresses (continued) Address operator Address appears as a hexadecimal number 5
Understanding Arrays v. Array: list of items that all have the same type v. Subscript: number that indicates the position of the particular array element being used v. Element: single object in an array vdouble money. Collected[5]; 6
Understanding Arrays (continued) int some. Numbers[7]; cout<<some. Numbers produces the same output as cout<<&some. Numbers[0] v. If you access some. Numbers[7], you may get a warning or a garbage value 7
Storing Values in an Array int rent[4]; rent[0] = 250; rent[1] = 375; rent[2] = 460; rent[3] = 600; int int int rent[4] = {250, 375, 460, 600}; rent[2] and rent[4] = {250, 375}; rent[3] are set to 0 rent[3] = {250, 375, 460, 600}; Syntax error rent[4] = {0}; Sets all array elements to 0 8
Accessing and Using Array Values 9
Accessing and Using Array Values (continued) v. You can also use a variable as a subscript 10
Accessing and Using Array Values (continued) 11
Accessing and Using Array Values (continued) 12
Avoiding Common Array Errors v. When working with arrays, common errors include: § Forgetting that arrays are zero based § Accessing locations beyond the array 13
Forgetting that Arrays are Zero-Based 14
Accessing Locations Beyond the Array Element is out of bounds 15
Using Part of an Array Sentinel 16
Using Part of an Array (continued) v. Tip: You cannot use a variable to declare an array’s size; you must use a constant 17
Using Parallel Arrays v Parallel arrays are corresponding arrays in which values in the same relative locations are logically related 18
Using Parallel Arrays (continued) 19
Using Parallel Arrays (continued) 20
Using Parallel Arrays (continued) Flag could also be of type bool 21
Creating Arrays of Structure Objects 22
Using Strings v String: value expressed within double quotes v You can type two characters within single quotes when they represent a single character: ‘n’ v “Hello” is a string constant v To store a value such as “Hello” -> must create a string variable in one of two ways: § Create a string as an array of characters § Create a string using the string class defined in the C++ standard library 23
Strings Created as Arrays of Characters char char first. Name[] = “Mary”; Null character. You could first. Name[] = {“Mary”}; also use the constant NULL, defined in iostream first. Name[5] = “Mary”; first. Name[5] = {“Mary”}; first. Name[5] = {'M', 'a', 'r', 'y', '