ch. 8 "Arrays" --- Gaddis Starting Out W/ C++ Early Objects

Allows you to store and work with multiple values of the same data type
What is an array?
It depends on the array’s data type and the number of elements
What does the amount of memory used by an array depend on?
We will write a custom essay sample on
ch. 8 "Arrays" — Gaddis Starting Out W/ C++ Early Objects
or any similar topic only for you
Order now
By multiplying the number of bytes needed to store an individual element by the number of elements in the array
How can the size of an array be calculated?
The individual elements of an array are assigned unique subscripts. These subscripts are used to access the elemts
What are subscripts and what are they used for?
“hours sub zero”
How is the expression hours [0] pronounced?
The values have not been assigned to other elements of the array, question marks are used to indicate that the contents of those elemts are unknown. If an array holding numeric values is define globally, all of its elemts are initialized to zero by default
Why would there be question marks in the elements for arrays?
Do local arrays have a default initialization value?
The number inside the brackets in an array definition is the size declarator—it specifies how many elements the array holds. The number inside the brackets in an assignment statement or any statement that works with the contents of an array is a subscript, which specifies which element is being accessed
What is the difference between the array size declarator and a subscript?
The fact that subscript numbers can be stored in variables
What makes it possible to use a loop to “cycle through” an entire an array, performing the same operation on each element?
1) open the file
2) Use a loop to read each item from the file
3) Store it in an array element
How can you read data from a file and store it in an array?
1) open an output file pointed to by an ofstream object
2) use a loop to step through each element of the array
3) Direct the output to the file instead of to the computer screen
How do you write the contents of an array to a file?
C++ does not perform array bounds checking, which means that you could write a program that accidentally allows an array’s subscript to go beyond its boundaries
What does it mean when C++ does not check bounds?
Array subscripts start at 0 rather than 1
What is the “off-by-one error”?
When they are defined
When can arrays be initialized?
Some instructors prefer you to begin storing the actual data in element 1 when you are modeling something in the real world that logically begins with 1
How can you start with array element 1?
When an array is being initialized, does C++ require a value for every element?
int numbers[7] = {1, 2, 4, 8}
How is it possible to initialize only part of an array?
its elements will contain “garbage” just like other local variables
What happens if a local array is completely uninitialized?
It is not necessary to offset array subscripts by 1 to locate a particular piece of data
What happens if the actual data stored at the beginning with element 1?
C++ does not require a value for every element—it is possible to only initialize part of an array
int numbers[7] = {1, 2, 4, 8}
The other uninitialized elements will be set to zero
What happens if an array is being initialized?
Can an array have more values than an array can hold?
Can you skip elements in an array?
Yes; by providing an initialization list that includes a value for every element; C++ counts the number of items in the initialization list and gives the array that many elemts
Can you define an array without specifying its size?
int value = 5;
int value (5);
int value {5}; //Only works with C++ 11 or higher and checks //to make sure the value you are initializing matches the //data type of the variable
intvalue1 = 4.9;
intvalue2 = doubleVal;
//Both cases the fractional part of the value will be //truncated before it is assigned to the variable being //defined
What are different ways to initialize variables?
It is a loop that iterates once for each element in an array. Each time the loop iterates, it copies an elements from the array to a variable
What is the range-based loop?
Each time the range-based loop iterates, it copies the next array element to the range variable
What is the range variable?
for(dataType rangeVariable : array)
What is the general format for a rang=based for loop?
the data type of the range variable; it must be the same as the data type of the array elements, or a type that the elements can automatically be converted to
What is dataType?
It is the name of the range variable; receives a value of a different array element during each loop iteration; during the first loop iteration, it receives the value of the first element and so on
What is the rangeVariable?
The name of the array on which you wish the loop to operate. the loop will iterate once for every element in the array
What is the “array”?
A statement that executes during each loop iteration; to execute more than one statement in the loop, enclose the statements in a set of curly braces
What is a “statement”?
Declares val as a reference variable, so as the loop executes the val variable will contain a copy of the array element and it will be an alias for the element itself
What is an ampersand (&) used for?
You must assign each element of the first array, one at a time, to the corresponding element of the second array

const int SIZE = 6;
int arrayA[SIZE] = {10, 20, 30, 40, 50, 60};
int arrayB [SIZE] = { 2, 4, 6, 8, 10, 12};

for {int index = 0; index < SIZE; index++} arrayA[index] = arrayB [index];

How can you copy one array to another?
When you use the == operator with array names, the operator compares the beginning memory addresses of the arrays, not the contents of the arrays because they have two different memory addresses;
to do this correctly, you must compare their individual elements
How do you compare two arrays?
Strings are internally stored as arrays off characters; different from other arrays in that the elements can either be treated as a set of individual characters
What are strings?
As single entities
How are strings are normally treated and processed?
Stored as characters in contiguous bytes of memory
How are string objects and C-strings stored?
By placing a ‘/0’ which represents the null terminator in the byte of memory following the last character of the string; no guarantee on how string objects will be implemented
How are string literals and c-strings terminated?
By using the same subscripts
How can you build relationships between data stored in two or more arrays?
When data items stored in two or more arrays related in this fashion
What are parallel arrays?
It allows an alias, or synonym, to be associated with a simple or structured data type
What is the typedef statement?
The arrays and entire arrays can both be passed as arguments to functions
What are the individual elements of arrays?
Reference variables; give the function direct access to the original array
What are array parameters work like?
The function is not allowed to make changes to the array’s contents
What happens when an array parameter is declared as a const?
Good idea to always use a const array parameter in any function that is not intended to modify its array element
What should you do to a const array parameter as a precaution?
Like several identical arrays put together; useful for storing multiple sets of data; two dimensional arrays can be initialized when they are created
What is a two-dimensional array?
It needs to know how many bytes separate the memory; the number of columns is a critical factor in this calculation
What happens when the compiler generates code to access element of two dimensional array?
Number of columns; outer loop iterates once for each row in the array and the inner loop iterates once for each column in the row
C++ permits arrays to have multiple dimension
Arrays with three or more dimensions?
You must explicitly state all but the first dimension in the parameter list
What must you do when writing functions that accept multidimensional arrays as arguments?
It is similar to a one-dimensional array but has some advantages compared to a standard array
What is a vector?
It is a collection of programmer-defined data types and algorithms that are available for you to use in C++ programs; data types and algorithms are not part of the C++ language but were created in addition to the built-in data types
What is the Standard Template Library (STL)?
The data types that are defined in the STL; they are called containers because they store and organize data
What are containers?
Organizes data in a sequential fashion, similar to an array
What does a sequence container do?
Organizes data with keys which allow for rapid, random access to elements stored in the contaner
What does an associative container do?
It is a sequence container that is like a one-dimensional array:
-A vector holds a sequence of values, or elements
-A vector stores its elements in contiguous memory locations
-You can use the array subscript operator [] to access individual elements in the vector
What kind of container is a vector data type?
-you do not have to declare the number of elements that a vector will have
-if you add a value to a vector that is already full, the vector will automatically increase its size to accommodate the new value
-Vectors can report the number of elements they contain
What are the advantages of vectors when compared to arrays?
What header do you have to include when you use vectors?
Vector numbers; you can also declare a starting size if you prefer:
Vector numbers (10);
The size will expand if you add more than 10 values to it
What is an example of a statement when you create a vector object?
Vector set2 (set1);
How can you initialize a vector with the values in another vector?
This defines names as an empty vector of string objects
Vector names;
This defines scores as a vector of 15 ints
Vector scores (15);
This defines letters as a vector of 25 characters. Each element is initialized with ‘A’
Vector letters (25, ‘A’);
This define values2 as a vector of doubles. All the elements of values1, which is also a vector of doubles, are copied to values2
Vector values2 (values1);
In C++ 11 this defines length as a vector of 3 ints, holding the values 12, 10, and 6
Vector length {12, 10, 6}
The array subscript operator []
What operator can you use to store a value in a vector element that already exists or to access the data stored in a vectoe element?
Use the psuh_back function
How would you access a vector element that does not exist?
This function accepts a value as an argument and stores it in a new element placed at the end of the vector—pushes the value at the “back” of the vector
What does the psuh_back function do?
Accomplished with the size member of a function
How do you report the number of elements of a vector for a vector?
You can use the pop_back member function
How do you remove the last element from a vector?
Removes the last element from a vector named collection
Use the clear member function
Numbers.clear ()
How do you completely clear the contents of a vector?
Use the empty member function…the function returns true if the vector is empty and false if the vector has elements stored in it
How do you determine if a vector is empty?
Returns the value of the element located at position in the vector
Returns the maximum number of elements that may be stored in the vector w/o additional memory being allocated. (Not the same value as returned by the size member function)
Capacity ()
Clears a vector of all its elements
Clear ()
Returns true if the vector is empty. Otherwise, it returns false
Empty ()
Removes the last element from the vector
Stores a value in the last element of the vector. If the vector is full or empty, a new element is created
Push_back (value)
Reverse the order of the elements in the vector (the last element becomes the first element, and the first element becomes the lsst element)
Reverse ()
Resizes a vector to have n elements, where n is greater than the vector’s current size. If the optional value argument is included, each of the new elements will be initialized with that value
Resize (n)
Example where vect has four elements
Resize (n, value)
Returns the number in the vector
Size ()
Swaps the contents of the vector with the contents of vector2
Swap (vector2)
_____ of arrays can be class objects

Hi there, would you like to get such a paper? How about receiving a customized one? Check it out