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
Which of the following function declarations can be passed the following array?
char myArray[6][8];
A) void f1(char a[][], int sizeOfFirst);
B) void f1(char a[][8], int sizeOfFirst);
C) void f1(char& a, int sizeOfFirst);
D) void f1(char a[6][8], int sizeOfFirst);
E) B and D
void f1(char a[6][8], int sizeOfFirst);
Which of the following declare an array of 5 characters, and initializes them to some known values?
A) char array[5] = {‘a’,’b’,’c’,’d’,’e’};
B) char array[4] = {‘a’,’b’,’c’,’d’,’e’};
C) char array[5] = {‘’};
D) char array[] = {‘a’,’b’,’d’,’e’};
E) A and C
char array[5] = {‘a’,’b’,’c’,’d’,’e’};
Which of the following correctly declare an array that can hold up to 3 rows of 5 columns of doubles?
A) int array[3],[5];
B) float array[3,5];
C) int array[3][5];
D) float array[3][5];
float array[3][5]; or double array[[3][5]
Which of the following will read values from the keyboard into the array?
(Assume the size of the array is SIZE).
A) cin >> array[];
B) cin >> array;
C) cin >> array[SIZE];
D) for(i = 0;i < SIZE;i ++)
cin >> array[i];
for(i = 0;i < SIZE;i ++)
cin >> array[i];
What are the valid indexes for the array shown below?
int myArray[25];
0-24
How many indexed variables does the following array have?
int myArray[12] = {1,2,3,6,5,4,7,1,2};
9
If your index used to access the indexed variables of the array has the value of a non-existent index, this is called ________.
out of bounds
An ________ is used to process a collection of data all of which is the same type
array
Write the code to declare a two dimension array of integers with 10 rows and 20 columns
int array[10][20]