Chapter 9 Margin Notes
General Note: In this chapter you learn about a Data Structure called an Array--essentially a variable that has family members. Each family member has the same name, but a unique subscript or index. Read on!
Page 386. Yes, there are one-dimensional arrays and also multi-dimensional arrays. Concentate, for now, on the one-dimensional arrays.
Page 387, Figure 9.2. Unfortunately, this isn't a great illustration, in that it doesn't convey one of the essential facts about the nature of an array--that is, the name of each element is identical. The index (sometimes called element number or subscript) is, however, unique.
Page 388. Arrays are NOT a Data type, but a Data Structure of Data Types. As a result, you can have Integer Arrays, Float Arrays, String Arrays, etc. What you can't do is have an array that mixes Data Types.
Page 388, Indices. In C++, Array Indices are numbered beginning with 0.
Page 389. To declare an Array, we use this syntax
The number in brackets is the number of elements in the array. This Array contains 10 elements, with indices ranging from 0 through 9. Notice that by convention, the Array name ends in the letter 's'.
Page 390, Initializing Arrays. Check out the syntax for initializing an array with 3 elements. Notice the second example on the page which includes a 'partial' initialization of the array. Notice that the compiler initializes the elements of the array for you, based on the Data Type.
Page 390, Initializing Arrays (more). Notice what happens if you attempt to initialize more elements of an array than are declared. heck out the syntax for initializing an array with 3 elements. Notice the second example on the page which includes a 'partial' initialization of the array. Notice that the compiler initializes the elements of the array for you, based on the Data Type.
Page 391, Initializing Character Arrays. Notice how character arrays are initialized with apostrophes.
Page 392, 'null terminator'. This is the default for a character data type.
Page 393, Placing Elements into One-Dimensional Arrays. Observe how the assignment of a value to an Array element requires references its index within brackets.
Page 394, Debugging Tip. This is great to know, but please don't concern yourself with it.
Page 394, Using loops to insert values into an Array. The heading doesn't suggest it, but this is an extremely important skill. Read this section carefully.
Page 395, Code at the top of
the page. Notice how the Array size is designated using the value of the
Page 395, Code at the top of the page (more). Notice how the For loop test expression is 'i < SIZE'--this is because the last index number in an Array is one less than the size of the array.
Page 396, Copying Elements from One-Deimensional Arrays. Bit of an overkill here---copying values from an array element is no different than copying values from a variable.
Page 399, Arrays of Characters: C-Strings. Unfortunately, this section is a bit confusing. The author is telling you that in the 'C' language, there is no String data type, the kind that we see in C++. In C, Strings are really just an array of characters. That's what this whole section is about, and quite honestly, it's something that you can skip if you want. We're dealing with C++ here---not C. For that reason, I'm skipping right to page 408.
Page 408, Passing Arrays and Array Elements to Functions. I know you just learned about custom functions last week, and how to pass values and variables to them. Now you learn you can also pass Arrays and Array elements to them. For instance, you might write a function called Sort(), pass it an Array with unsorted elements, and have the function return the Array sorted. Or, if you pass the Array by Reference (remember that from last week), you can have it directly sort the Array instead of having to pass it back. However, in C++, an Array is passed by Reference simply by designating the Array name itself (with no brackets). For instance, if I have an array called myArray, passing a reference to the sort() function would look like this...
Page 408, Code. Notice how the size of the array is included in the function prototype (although the code still works without it).
Page 432, the Vector Class. The Vector class is a 'newer' version of the Array structure. Vectors have a lot more 'built in' functionality than Arrays (they have a bunch of functions). Check out the example.