Uploaded by wonkplomery

sheet5

advertisement
Arrays– Test Questions
True False:
An explanation is required.
1. If we want to find the median of 100 scores, we
need 100 separate variables to hold the data.
2. An array behaves like a list of variables each of
which is of the same type and for which there is a
uniform, convenient naming convention that can be
declared in a single line of code. In the explanation,
give an example of declaration and access.
3. With arrays, indices start at any number the
programmer chooses to indicate in the definition.
4. The programmer should always use a defined
constant in an array declaration.
5. When using an array, it is perfectly legal to access
indexed variables with index values less than 0 or
greater than or equal to the declared size of the
array.
6. To call a function with an array parameter, write the
array argument as the array name followed by
empty square brackets, as in f(a[], 7); (The first
argument is an array a, the second is the size.)
7. Consider the array declaration, int x[20];.
There is no memory allocated for x[20].
8. Arrays in C++ may have several different types
stored in them.
9. Given the two C++ array declarations:
int a[10], b[10];
You can successfully compute one array, say a,
then assign b to a:
a = b;
10. In a sorting an array, the items in the array are
rearranged so that
for all j and k, if j < k, then
array[j]<=array[k]
11. In the definition, double d[10] = {0.0};
only d[0] is initialized to zero, the rest are
uninitialized, just like x in the definition
double x;
12. If you need an array with more than one index, you
can use a multidimensional array, which is actually
an array of arrays. In the explanation, declare an
array of doubles with 2 rows and 5 columns.
13. C++ arrays check for out-of-range index values.
14. A for-loop is a convenient way to step through an
array.
2. Describe the difference in the meaning of the 5 in
int x[5]; and the meaning of the 4 in x[4].
What are the meanings of the int, the [5] and the
[4]?
3. Give the syntax of an array declaration. Mention
the base type and declared size.
4. In the array declaration
double score[5];
identify the following:
a) The array name
b) The base type
c) The declared size of the array
d) The smallest and largest index values this array
can have.
e) Give one of the indexed variables (a.k.a.
elements) of this array.
5. Write a C++ code fragment that is assumed to be
embedded in an otherwise complete and correct
program. You are to assume the user has been
prompted (so you don’t have to) for (exactly) 20
values of type int to be read from the keyboard,
You are to use this input to fill an array. Do not
write a full program, just the code fragment to do
this. Do give declarations of the array and any
variables you use.
6. Consider the declaration
double a[10] = {1.2, 2.1, 3.3,
3.5, 4.5,7.9, 5.4, 8.7, 9.9,1.0};
Write a function named out_of_order that will
test this array for the condition
a[0] <= a[1] <= a[2] <= ...
The function returns -1 if the elements are not out of
order, otherwise it returns the index of the first element
that is out of order.
Explain what you do to avoid out of bounds array
access.
7. Consider the following function definition:
void tripler(int& n)
{
n = 3*n;
}
Given this definition, which of the following are
acceptable function calls?
int a[3] = {3,4,5}, number = 2;
a) tripler(a[2]);
b) tripler(a[3]);
Free Form Questions:
c) tripler(a[number]);
1. Distinguish the use of the square brackets [] in a
d) tripler(a);
definition of an array and their use for access to
e) tripler(number);
some indexed variable of the array.
8. Consider the following function definition:
void tripler(int& n)
{
0
1
2
3
4
5
6
7
|------|------|------|------|------|------|------|n = 3*n;
}
0
1
2
3
4
5
6
7
Given these definitions what (if anything) is wrong
|------|------|------|------|------|------|------|with the following?
int b[5] = {3,4,5,6,7};
0
1
2
3
4
5
6
7
for (int j = 1; j <= 5; j++)
|------|------|------|------|------|------|------|tripler(b[j]);
0
1
2
3
4
5
6
7
a) Nothing is wrong with either bit of code.
|------|------|------|------|------|------|------|
b) There are not enough initializers for the array.
c) There is an illegal index in the loop.
d) There are too many initializers in for the array. 11. *Write the selection sort algorithm for an array of
int in C++. You are to assume that there are
e) The call to the function requires different
predefined
functions
swapValues
and
syntax.
9. Explain the error in the following code. You may
indexOfSmallest, that is do not write these
give the warning message, or error message, your
functions. However, you must write declarations
compiler might give for the following error, or you
with pre and post conditions for these functions.
may describe the error. However, you present the 12. *Insert const before .any of the following array
error, you must explain clearly what is wrong.
parameters that can be changed to const.
#include <iostream>
//Test question
void output(double a[], int size);
void show_array(int ar[], int size)
{
using namespace std;
for(int i = 0; i < size; i++)
cout << ar[i] << " ";
cout << endl;
}
int main()
{
const int a[6] = {2, 4, 2, 3, 5};
show_array(a, 6);
//...
}
10. *Here is a list of 8 numbers. Use the selection sort
algorithm to sort this list. Fill in this table with each
iteration of the loop in the selection sort algorithm.
Mark the place from which you are looking for the
'next smallest element'. In this display, the upper
numbers are the indices, the lower numbers are in
the corresponding positions. Use the several rows
provided to show the sequence of steps.
0
1
2
3
4
5
6
7
|------|------|------|------|------|------|------|8
6
10
2
16
4
18
14
0
1
2
3
4
5
6
7
|------|------|------|------|------|------|------|0
1
2
3
4
5
6
7
|------|------|------|------|------|------|------|0
1
2
3
4
5
6
7
|------|------|------|------|------|------|------|-
//Pre: a[0] through a[size-1]
values set.
//Post: a[0] through a[size-1]
been displayed on the screen.
have
have
void dropOdd(int a[], int size);
//Pre: a[0] through a[size-1] have
values set.
//Post: All odd numbers in a[0] through
a[size-1] have //been changed to 0.
13. What is the output of the following code?
#include<iostream>
int main()
{
using namespace std;
double a[3] = {1.1, 3.3, 2.2};
cout << a[0] << " " << a[1] << " "
<< a[2] << endl;
a[1] = a[2];
cout << a[0] << " " << a[1] << " "
<< a[2] << endl;
}
14. What is the problem with this code?
int array[5];
for (int index = 1; index <=5;
index++)
array[index] = index /2;
15. Suppose we want an array to satisfy the condition,
a[0] <= a[1] <= a[2] <= ...
and suppose this code is written to implement a test 2. Given the array declaration, int a[20]; The
of this condition
first element is written as:
a) a[1]
#include <iostream>
b) a[0]
using namespace std;
c) a
d) a[20]
int main()
e) a[19]
{
double array[10] = { 1, 2, 3, 4, 5, 3. Given the array declaration, int a[20]; The
6, 7, 8, 9, 10 };
last (legal) element is written as:
//
assume
the
array
is
filled
a) a[2]
somehow.
b) a[0]
for(int i=0; i < 10; i++)
c) a
if (array[i] > array[i+1])
d) a[20]
cout << "Array elements " << i <<
" and "
e) a[19]
<< i + 1 << " are out of 4. Are the following array initializations correct? If
order.\n";
not, why not?
}
a) int x[4] = {8, 7, 6, 5, 4};
When this is run, we sometimes get the following
b) int x[] = {8, 7, 6, 5, 4};
puzzling output:
c) int x[4] = {8, 7, 6};
Array elements 9 and 10 are out
d) const int SIZE =4;
of order.
int x[SIZE];
Even more puzzling, sometimes we
e)
const int SIZE =4;
don’t get this output.
int x[SIZE-4];
Why?
5. Which of the following are correct? Why are the
others incorrect?
16. *Assume you have a swap routine in place for
When
a function having an array formal parameter
doubles. Write a routine calls swap that reverses the
is
called,
the formal array parameter …
entries in an array of doubles. Use this function
a) names a copy of the array argument.
declaration.
b) refers to exactly the same array as the calling
void reverse (double a, int size);
program
Carry out your reversal in place, do not make a
c) is passed the address of the argument, and the
local copy of the array
function needs further information about the
17) Rewrite the code below using the for each loop
array size to safely use an array parameter
construct provided in C++11.
d)
refers to the array using a name that is always different
int b[5] = {3,4,5,6,7};
from the calling program's argument.
int sum = 0;
6. Suppose you have the following array declaration
for (int j = 0; j < 5; j++)
in a program.
sum+=b[j];
int yourArray[5];
Further suppose that in the implementation of C++
Multiple Choice
you are using an int that requires 4 bytes.
There may be more than one correct answer. You must
give all the correct answers for full credit. An
i) When your program runs, how much memory is
explanation is required.
required for this array?
1. In C++ array indices, that is subscript values, must
ii) Suppose further that your array starts at memory
be
location decimal 100. What will be the address of
a) An integer type
yourArray[3]?
b) Non-negative
iii)If you wrote to the (illegal) index 7 position in
c) Positive
yourArray to what address would this clobber?
d) Less than or equal to the declared size of the
a) i) The array takes 5 bytes, ii) yourArray[3]
array
will be an int located at Address 103. iii)
e) None of these is correct
writing to yourArray[7] will clobber an
int
starting
at
location
107.
c) "Hey,
make_2,
come
array1. Its size is 20."
change
b) i) The array takes 10 bytes, ii)
//A
declaration
for
parts
e)
yourArray[3] will be an int located at
through h).
Address 106. iii) writing to yourArray[7]
int array2[50];
will clobber an int starting at location 114
d) make_2( array2, 50 );
c) i) The array takes 20 bytes, ii)
e) make_2( array2[5],50 );
yourArray[3] will be an int located at
9. Given the definition and code fragment:
Address 112 iii) writing to yourArray[7]
int matrix[2][3];
will clobber an int starting at location 128
int k = 0;
d) The purpose of a high-level language is to
for(int i =0; i < 3; i++)
insulate the programmer from these details. It
for (int j=0, j < 4; j++)
isn’t possible to know this without probing the
matrix[i][j] = k++;
source to the operating system and the compiler,
The value of matrix[0][0] is
or extensive debugging.
a) 0
7. What is the output of the following code (assuming
b) 1
it is embedded in a correct and complete program)?
c) 2
d) 3
char letter[5] = {'o', 'k', 'c',
e) 4
'e', 'g'};
10. Which of these array definitions will set all the
for(int i = 4; i >= 0; i-- )
indexed variables to 0?
cout << letter[i];
a) int array[5];
cout << endl;
b) int array[5] = {0};
Choices:
c) int array[5] = {0,1,2,3,4};
a) okceg
d) int
array[5]
=
{0,0,0};x
b) gecko
>>What is the x?
c) ecko followed by a character from an out of
e) int array[5] = {0,0,0,0,0};x
bounds access.
>>What is the x?
d) kceg followed by a character from an out of
bounds access.
e) Correct answer not listed. Specify what the
output is.
8. Consider the function definition and array
declarations. Which are incorrect calls to the
function make_2? Why?
(In considering this code, you are to ignore the a) b),
and so on, at the start of the lines, and consider that
this code is to be embedded in a complete and
correct program.)
void make_2 ( int a[], int size )
{
for (int i = 0; i < size; i++ )
a[i] = 2;
}
int array1[20];
a) make_2( array, 30 );
b) make_2(
array,
10
);
>>>>>replace array with array1
two places
Download