Ch.7 Flashcards
Which of the following will correctly assign all the values in one array to the other array? (Assume both arrays are of the same type and have SIZE elements)
A) array1[] = array2;
B) for(i = 0;i < SIZE;i ++)array1[i] = array2[i];
C) for(i = 0;i < SIZE;i ++) array1[] = array2[];
D) array1 = array2;
for(i = 0;i < SIZE;i ++)array1[i] = array2[i];
Given the following function definition, what modifications need to be made to the search function so that it finds all occurrences of target in the array? int search(const int array[], int target, int numElements) { int index = 0; bool found = false;
while((!found) && (index < numElements))
{
if(array[index] == target)
found = true;
else
index ++;
}
if(found == true)
return index;
else
return -1;
}
Add another parameter to indicate where to start searching
If you need a function that will handle multi-dimensional arrays, you must specify the following sizes inside the square brackets.
all the sizes
Which of the following function declarations correctly expect an array as the first argument?
void f1(int array[100], int size); void f1(float array[], int size);
Given an array of integers of size 5, how does the computer know where the 3rd indexed variable is located?
It adds space for 3 integers to the base address of the array.
Which of the following function declarations could be used to input data from the keyboard into the array?
void input(int array[], int &numElements, int MAX_SIZE);
Why should you use a named constant for the size of an array?
A) makes changes to the program easier
B) helps reduce logic errors
C) readability of code
D) all of the above
all of the above
Which of the following array declarations are legal?
A) int array[] = {0,0,0};
B) int size;
cin >> size;
int array[size];
C) const int size = 9;
int array [size];
D) int array[10];
E) all but B
all but B
What is wrong with the following code fragment?
const int SIZE = 5;
float scores[SIZE];
for(int i = 0; i <= SIZE;i ++)
{
cout << “Enter a score\n”;
cin >> scores[i];
}
Array indexes must be less than the size of the array.
Which of the following statements are true?
A) Array elements may be user-defined types (structs or classes).
B) If a function is expecting a variable of the base type of the array, then you can pass an element of the array to the function.
C) all of the above
D) none of the above
all of the above
Which of the following function declarations correctly guarantee that the function will not change any values in the array argument?
A) void f1(int array[], const int size);
B) void f1(int array[], int size);
C) void f1(int &array, int size);
D) void f1(const int array[], int size);
E) void f1(int array[], int size) const;
void f1(const int array[], int size);
What is wrong with the following code?
float scores[10], total;
A) nothing
B) Cannot declare regular and array variables together.
C) The 10 should be replaced with a variable name, whose value is input from the user.
D) Arrays must be integers.
nothing
Given an array named scores with 25 elements, what is the correct way to access the 25th element?
scores[24]
What is the output of the following code fragment?
int array[4][4], index1, index2;
for(index1 = 0;index1 < 4;index1 ++)
for(index2 = 0;index2 < 4;index2 ++)
array[index1][index2]=index1 + index2;
for(index1 = 0;index1 < 4;index1 ++)
{
for(index2 = 0;index2 < 4;index2 ++)
cout << array[index1][index2] << “ “;
cout << endl;
}
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
If you declare and initialize an integer array of size 10, but only list 5 values, what values are stored in the remaining 5 indexed variables?
0