Chapter 8: Arrays
One-dimension array
Components are arranged in a list form
Statement : int list [10] = {0};
Declares an array of 10 components and initializes all of them to zero
Statements: in list [10] = {0};
Declares an array of 10 components and initializes all of them to zero
Arrays that are created using pointers during program execution are called________ arrays
Dynamic
Values are placed between curly braces
Example 1: double sales [5] = { 12.25, 32.50, 16.90, 23, 45.68}
A function can return a value of type array. (6)
False
Arrays can be passed as parameters to a function either by value or by reference. (6)
False
As parameters, two-dimensional arrays are passed either by value or by reference. (15, 16)
False
C++ does not allow functions to return a value of type array
Refer to Example 8-6 in the text function sumArray and indexLargestElement
Ex. int myList [5] = {0, 4, 8, 12, 16}; //Line 1 int yourList [5] ; //Line 2 yourList = myList; //illegal
Solution for (int index = 0; index < 5; index++ ) yourList [index] = myList [index];
list [5] = 34;
Stores 34 in list [5], sixth component of the array list
int list [10]
The statement (an example of partial initialization of an array during declaration):
int list [10];
This Statement declares an array of 10 components
In C++, there is no guard against indices that are out of bounds
This check is solely the programmers responsibility
A one-dimensional array is an example of a structured data type. (1)
True
Arrays can be initialized during declaration
True
In C++, some aggregate operations are allowed for strings. (11, 12, 13)
True
The number in parentheses at the end of an exercise refers to the learning objective listed at the beginning of the chapter.
True
The size of an array is determined at compile time. (1, 6)
True
Given the declaration int list [100] ; // array size of 100 int i;
Use a for loop to access array elements: for (i = 0; i < 100; i++) //Line 1 cin >> list [ i ]; //Line 2
Which of the following statements is FALSE?
When declaring a two-dimensional array as a formal parameter, you can omit the size of the first dimension or the second.
n dimensi
a collection of a fixed number of elements attanges in n dimensions (n>1=)
Structured data type
a data type in which each data item is a collection of other data items
Functions for C-string manipulation are found in the ____ header file.
cstring
Two dimensional array syntax
dataType arrayName [inExp1] [intExp2];
Syntax for declaring a one dimensional array
dataType arrayName[intExp]; example: int numbers[5];
Which of the following date types cannot be used as an array index
double
Example 2: the array is determined by the number of initial values in the braces of the array is declared without size specified
double sales [ ] = { 12.25, 32.50, 16.90, 23, 45.68}
C-strings are ____.
enclosed in double quotation marks
char name [16];
example of a c-string declaration; largest string it can store has 15 characters
Assuming arrayName is the name of an array and identifier is a name of a variable that has the same data type as the array elements, which of the follow is the correct syntax for the range-based for loop?
for (dataType identifier : arrayName) statements
Arrays are passed by ________________
reference only
When storing a two-dimensional array in computer memory, C++ uses the ____.
relative address format
A ____ sort sorts the list by finding the smallest (or equivalently largest) element in the list and moving it to the beginning (or end) of the list.
selection
The _____ search algorithm searches a list for a given item, starting with the first element and continues to compare the item with the other elements in the list until either the item is found or the list has no more elements.
sequential
The size of array can be omitted if__________.
the array is initialized during declaration. ex: char name [ ] = "John"; declares an array of length 5 and stores the C-String "John" in the array
when an array is passed as a parameter, ______________________
the base address of the actual array is passed to the formal parameter
If you store a string whose length is less than the array size,__________.
the last components are unused
When you initialize arrays during declaration, ____.
the size is determined by the number of initial values in the braces
Ex: int arr[3][3] = {{1,2,3,}, {4,5,6} };
this examples creates a 2-dimensional array with each index having three elements. represents a table of two rows and three columns the first row or index contains number 1-3 and the second row, or index, contains numbers 4-6 *notice the arrangement of the braces to assign initial value to each element of the indices in the array declaration
Arrays are not limited
to the the int data type only, but can be created for any C++ data type, E.g.: int, float, double, bool, char.
Parallel Arrays
two or omore arrays are called parallel if their corresponding components hold related information
A ____ array is a collection of a fixed number of components arranged in rows and columns, wherein all components are of the same data type.
two-dimensional
C-strings are compared character by character___________
using the collating sequence of the system using the function strcmp
Simple data Types
variables of these types can store only one value at a time
Simple data type
variables of these types can store only one value at a time
If using the ASCII character set:
"Air" < "Boat" "Air" < "An" "Bill" < "Billy" "Hello" < "hello"
useful string manipulation function include:
-strcpy -strncpy -strcmp -strlen
The function strcmp (s1, s2) returns ____ if s1 and s2 are the same.
0
array
A collection of fixed number of components, all of the same data type
Structured data type:
A data type in which each data item is a collection of other data items
The index of an array is in bounds if the index is between 0 and
ARRAY_Size - 1 Otherwise, the index is out of bounds
Multidimensional Arrays
An array can have more than one index to represent multiple dimensions rather than the single dimension of a regular array
indexExp called the index
An expression with a nonnegative integer value
C-Strings
Are null-terminated ('\0')
[ ]: array subscripting operator
Array index always starts 0
Which of the following statements is TRUE?
C++ functions cannot return a value of type array.
If an array index goes out of bounds, the program always terminates in an error. (3)
False
The only aggregate operations allowable on int arrays are the increment and decrement operations. (5)
False
he declaration:char name[16] = "John K. Miller";declares name to be an array of 15 characters because the string "John K. Miller" has only 14 characters. (11)
False
he declaration:char str = "Sunny Day";declares str to be a string of an unspecified length. (11)
False
iven the declaration:int list[10];the statement:list[5] = list[3] + list[2];updates the content of the fifth component of the array list. (2)
False
Value of the index
Is the position of the item in the array
A(n) ______ operation is any operation that manipulates the entire array as a single unit
aggregate
Special Null Character
allows the entire string to be referenced from just the array name
Most rules for arrays _____
also apply to C-Strings
intExp:
any constant expression that evaluates to a positive integer
Aggregate operation
any operation that manipulates the entire array as a single unit **not allowed on arrays in C++
Aggregate operations, such as assignment and comparison,
are not allowed on arrays
In C++, arrays ____.
are passed by reference only
Multidimensional array of three indices or more:
are uncommon
General Syntax for Array
arrayName [indexExp]
When you pass an array as a parameter, the ____ of the actual array is passed to the formal parameter.
base address
the ___________ of an array is the address (memory location) of the the first array
base address
two dimensional arrays
can be initialized when they are declared
Exanple of string input
cin >> name; store the next input C-string name
TO read strings with blanks, use the function get:
cin.get(str, m+1); when executed, the statement stores the next n characters into str, but the newline character is not stored in str **If input string has fewer than m characters, reading stops at the newline character
2 dimensional arrays are ______
common and they are useful to store grid-based information such as table or coordinates
User can specify the name of an input and/or output file at execution time
cout << "Enter the input file name: "; cin >> fileName; infile.open(fileName); //open the input file * * * cout << "Enter the output file name: "; cin >> fileName; outfile.open(fileName); //open the outputfile
The index of an array is _______
in bounds if the index is between ) and ARRAY_SIZE - 1 **otherwise, the index is out of bounds***
Example of two-dimensional array initialiation
int board [4][43]
The statement (an example of partial initialization of an array during declaration):
int list [10] = {8, 5, 12}; Declares an array of 10 components and liabilities list [0] to 8, list [1] to 5, list[2] to 12 All other components are initialization to 0
If list is one-dimensional array.......
its base address is the address of list [0]
All the elements (components)of an array________
must be of the same type.
The last character in a C-string is always the ____.
null character
"\0"
null character: it can be used to store a string of text if the array's final element Ex: char fnames[5] = { 'm', 'i', 'k', 'e', '\0' };
Two (or more) arrays are called _____ if their corresponding components hold related information.
parallel