Deck 5: Arrays
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/32
Play
Full screen (f)
Deck 5: Arrays
1
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;
int a[10],b[10];
You can successfully compute one array,say a,then assign b to a:
a = b;
False
2
Arrays in C++ may have several different types stored in them.
False
3
Distinguish the use of the square brackets [] in a definition of an array and their use for access to some indexed variable of the array.
In the definition of an array,for example,int array[10];the [] are used to define the array,the number between the brackets specifies the declared size,that is,it specifies how many indexed variables are being defined.This could be any number.
In the access to an indexed variable,for example,array[5] = 14;the number between the brackets specifies which of the indexed variables is being accessed.This must be between 0 and one less than the declared size of the array.Technically,the [] are called the subscripting operator.
In the access to an indexed variable,for example,array[5] = 14;the number between the brackets specifies which of the indexed variables is being accessed.This must be between 0 and one less than the declared size of the array.Technically,the [] are called the subscripting operator.
4
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.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
5
Indexed variables for an array are stored wherever the computer finds memory for the first indexed variable,then the next one is stored next to the first if there is space,and someplace else if not.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
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. )
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
7
In the sequential search algorithm,items are examined alternately,odd then evens,in order to find whether the target value is in the array (and if the target is present,to the index of the target. )
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
8
The programmer should always use a defined constant in an array declaration.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
9
C++ arrays check for out-of-range index values.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
10
A for-loop is a convenient way to step through an array.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
11
With arrays,indices start at any number the programmer chooses to indicate in the definition.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
12
Consider the array declaration,int x[20];.There is no memory allocated for x[20].
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
13
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.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
14
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.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
15
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.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
16
If we want to find the median of 100 scores,we need 100 separate variables to hold the data..
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
17
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]
for all j and k,if j < k,then array[j]<=array[k]
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
18
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;
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
19
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]?
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
20
Give the syntax of an array declaration.Mention the base type and declared size.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
21
What is the output of the following code?
#include
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;
}
#include
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;
}
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
22
Explain the error in the following code.You may give the warning message,or error message,your compiler might give for the following error,or you may describe the error.However you present the error,you must explain clearly what is wrong.
#include
//Test question
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);
//...
}
#include
//Test question
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);
//...
}
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
23
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
|------|------|------|------|------|------|------|-
0 1 2 3 4 5 6 7
|------|------|------|------|------|------|------|-
0 1 2 3 4 5 6 7
|------|------|------|------|------|------|------|-
0 1 2 3 4 5 6 7
|------|------|------|------|------|------|------|-
0 1 2 3 4 5 6 7
|------|------|------|------|------|------|------|-
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
|------|------|------|------|------|------|------|-
0 1 2 3 4 5 6 7
|------|------|------|------|------|------|------|-
0 1 2 3 4 5 6 7
|------|------|------|------|------|------|------|-
0 1 2 3 4 5 6 7
|------|------|------|------|------|------|------|-
0 1 2 3 4 5 6 7
|------|------|------|------|------|------|------|-
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
24
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 a -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.
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 a -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.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
25
Given the definition and code fragment: int matrix[2][3];
Int k = 0;
For(int i =0;i < 3;i++)
For (int j=0,j < 4;j++)
Matrix[i][j] = k++;
The value of matrix[0][0] is
A)0
B)1
C)2
D)3
E)4
Int k = 0;
For(int i =0;i < 3;i++)
For (int j=0,j < 4;j++)
Matrix[i][j] = k++;
The value of matrix[0][0] is
A)0
B)1
C)2
D)3
E)4
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
26
Given the array declaration,int a[20];The first element is written as:
A)a[1]
B)a[0]
C)a
D)a[20]
E)a[19]
A)a[1]
B)a[0]
C)a
D)a[20]
E)a[19]
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
27
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 of this condition
#include
using namespace std;
int main()
{
double array[10] = { 1,2,3,4,5,6,7,8,9,10 };
// assume the array is filled somehow.
for(int i=0;i < 10;i++)
if (array[i] > array[i+1])
cout << "Array elements " << i << " and "
<< i + 1 << " are out of order.\n";
}
When this is run,we sometimes get the following puzzling output:
Array elements 9 and 10 are out of order.
Even more puzzling,sometimes we don't get this output.
Why?
a[0] <= a[1] <= a[2] <= ...
And suppose this code is written to implement a test of this condition
#include
using namespace std;
int main()
{
double array[10] = { 1,2,3,4,5,6,7,8,9,10 };
// assume the array is filled somehow.
for(int i=0;i < 10;i++)
if (array[i] > array[i+1])
cout << "Array elements " << i << " and "
<< i + 1 << " are out of order.\n";
}
When this is run,we sometimes get the following puzzling output:
Array elements 9 and 10 are out of order.
Even more puzzling,sometimes we don't get this output.
Why?
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
28
Insert const before .any of the following array parameters that can be changed to const.
void output(double a[],int size);
//Pre: a[0] through a[size-1] have values set.
//Post: a[0] through a[size-1] have been displayed on the screen.
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.
void output(double a[],int size);
//Pre: a[0] through a[size-1] have values set.
//Post: a[0] through a[size-1] have been displayed on the screen.
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.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
29
What is the output of the following code (assuming it is embedded in a correct and complete program)? char letter[5] = {'o','k','c','e','g'};
For(int i = 4;i >= 0;i-- )
Cout << letter[i];
Cout << endl;
Choices:
A)okceg
B)gecko
C)ecko followed by a character from an out of bounds access.
D)kceg followed by a character from an out of bounds access.
E)Correct answer not listed.Specify what the output is.
For(int i = 4;i >= 0;i-- )
Cout << letter[i];
Cout << endl;
Choices:
A)okceg
B)gecko
C)ecko followed by a character from an out of bounds access.
D)kceg followed by a character from an out of bounds access.
E)Correct answer not listed.Specify what the output is.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
30
Write the selection sort algorithm for an array of int in C++.You are to assume that there are predefined functions swapValues and indexOfSmallest,that is do not write these functions.However,you must write declarations with pre and post conditions for these functions.
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
31
What is the problem with this code?
int .array[5];
>>Delete the dot
for (int index = 1;index <=5;index++)
array[index] = index /2;
int .array[5];
>>Delete the dot
for (int index = 1;index <=5;index++)
array[index] = index /2;
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck
32
Assume you have a swap routine in place for doubles.Write a routine calls swap that reverses the entries in an array of doubles.Use this function declaration.
void reverse( double a,int size);
Carry out your reversal in place,do not make a local copy of the array
void reverse( double a,int size);
Carry out your reversal in place,do not make a local copy of the array
Unlock Deck
Unlock for access to all 32 flashcards in this deck.
Unlock Deck
k this deck