Self-test 6:
Array
-
Consider the following program. Which of the following is true?
#include <iostream> using namespace std; int main(void) { int n = 0; int i = n+1; int z[10] = {1,2,3,4,5,6,7,8,9,10}; for (i = 0; i <= 5; i++) { cout << z[2*i+1] << " " << 2*i+1 << endl; } return 0; }- The program fails to compile.
- The output of the program is:
2 1 4 3 6 5 8 7 10 9
- We cannot determine the output of the program definitely.
- The program outputs the 10 elements in array z.
SolutionC
While the for loop iterates from i = 0 to 5, the values of elements z[1], z[3], z[5], z[7], z[9], z[11] should be displayed. However, since the size of the array z is only 10, the index 11 is out of bound. We cannot guarantee what happens when the compute tries to display the value of z[11]; if the location of z[11] does not belong to the user, it will give the runtime error of "segmentation fault".
-
What is the output of the following program segment?
char a[7] = "ABC8900"; int i = 0; while (i*2 < 8) { cout << a[i]; i++; }- AC90
- B80
- Segmentation error
- Cannot even compile
SolutionD.
The character string "ABC8900" is equivalent to {'A','B','C','8','9','0','0','\0'}. Thus, "ABC8900" actually consists of 8 characters, and the a array, which has a size of 7, is not big enough to store "ABC8900". Initializing an array with more elements than its size is a compilation error.
-
What is the output of the following program segment?
char a[6][4] = {"ABC", "abc", "123", "789", "689", "123"}; for (int i = 2; i >= 0; i--) { for (int j = 0; j < 3; j++) cout << a[2*i][j]; cout << endl; }-
689 123 ABC
-
abc 789 123
-
ABC abc 123
- Segmentation error
SolutionA.
The declaration: char a[6][4] represents a list of 6 words, each with a maximum length of 3-character long. The outer i loop picks the words to print, while the inner j picks the characters to print in each word. The expression: 2*i picks the words with index = 4, 2, 0 since i iterates from 2 to 0. Since j iterates from 0 to 2, all the 3 characters of each words are printed.
-
-
Which of the function(s) below will never produce segmentation fault at runtime?
void boo1 (const char x[3][20]) { for (int i = 0; i < 3; i++) for (int j = 0; j < 20; j++) cout << x[j][i]; } void boo2 (const char x[3][20]) { for (int i = 0; i < 20; i++) for (int j = 0; j < 3; j++) cout << x[j][i]; } void boo3 (const char x[3][20]) { for (int i = 1; i <= 6; i++) for (int j = 0; j < 3; j++) cout << x[j][i*2-1]; }- boo1 & boo2
- boo2 & boo3
- boo1 & boo3
- None of the above
SolutionB.
In the output statement of boo1, the first index of x (which is j) iterates from 0 to 19, but the size of the first dimension of the x array is only 3. So the index j will soon get out of bound, causing the segmentation fault runtime error.
-
Which of the function(s) cannot be compiled?
void foo1 (const char x[10][10]) { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) cout << x[i][j]; } void foo2 (const char x[][10]) { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) cout << x[i][j]; } void foo3 (const char x[10][]) { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) cout << x[i][j]; }- foo2 only
- foo3 only
- foo2 and foo3 only
- All of the functions
- None of the functions
SolutionB.
In the function declaration of multidimensional arrays, the size of all dimensions except the first one (which is optional) must be specified.
-
Which of the function(s) below can successfully be compiled and add 1 to every element of the 10x10 array?
void cfoo1 (char arr[10][10]) { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) arr[i][j]++; } void cfoo2 (const char arr[10][10]) { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) arr[i][j]++; } void cfoo3 (char& arr[10][10]) { for (int i = 0; i <10; i++) for (int j = 0; j < 10; j++) arr[i][j]++; }- cfoo1 only
- cfoo3 only
- cfoo1 and cfoo2 only
- cfoo2 and cfoo3 only
- All of functions
SolutionA. In function cfoo2, the char array is declared as read only (const), and its elements cannot be modified using the increment operator. In function cfoo3, x is declared as an array of character references (char&), but array of references is not allowed in C++.
-
Which of the following(s) show(s) the correct input and corresponding output of the program?
int main(void) { char x[20]; cout << "enter a C string:"; cin >> x; cout << x << endl; return 0; }-
enter a C string: hkust comp2011 hkust
-
enter a C string: hkust comp2011 hkust comp2011
-
enter a C string: hkust comp2011 hkust comp2011 -
enter a C string: hkust comp2011 c++ programming hkust comp2011 c+
SolutionA.
cin will skip all the leading white spaces when reading data of the required type until it sees the next white space. So if one enters a sentence with spaces between words, only the first word is read into x.
-
-
Which of the following is the correct way to assign values to EACH element of an array?
-
char x[][] = {"a", "b", "c", "d", "e" }; -
int x[] = {{1}, {2}, {3}, {4}, {5}}; -
int x[]; for (int j = 0; j < 5; ++j) x[j] += 2;
-
float x[6]; for (int j = 5; j > -1; j--) x[j] = 8;
-
float x[1] = 3.4;
SolutionD.
Others are wrong:
Answer A: All dimenision, except the first MUST be specified when initializing an array.
Answer B: The initializer is a 2D array initializer.
Answer C: the size of the x array is missing.
Answer E: x is not a single integer variable! To initialize it, you need something like "float x[1] = {3.4};"
-