1. Consider the following declaration: double words[60]; In this declaration, id
ID: 3868222 • Letter: 1
Question
1. Consider the following declaration:
double words[60];
In this declaration, identify the following:
a. The array name:
b. The array size:
c. The data type of each array component:
d. The range of valid indices for the array: to
2. Explain why the following declarations are invalid.
a. people[82];
Answer:
b. int myArray[50;
Answer:
c. int SIZE;
double list[SIZE];
Answer:
d. const int X = 50;
double list[X – 55];
Answer:
e. int ids[-20];
Answer:
f. name string[10];
Answer:
3. Write C++ statements to do the following:
a. Declare an array beta of 10 components of type double with all values initialize to zero.
b. Output the value of the first component of the array beta. Assume iostream has been included.
d. Set the value of the ninth component to 0.5 times the second component.
e. Set the value of the sixth component to the seventh component minus the third component.
f. Output the value of the last component of beta. Assume iostream has been included.
4. What is stored in myList after the following C++ code executes?
double myList[5];
myList[0] = 3.0;
myList[1] = 4.0;
for (int i = 2; i < 5; i++)
{
myList[i] = myList[i - 1] * myList[i - 2];
if (i > 3)
myList[i] = myList[i] / 2;
}
Answer:
5. Correct the following code so that it correctly sets the value of each element of myList to 3 times the element’s index.
int myList[10];
for (int i = 1; i <= 10; i--)
myList[i] = 3i;
Answer:
6. Write C++ statements to define and initialize the following arrays.
a. Array heights of 10 components of type double. Initialize this array to the following values:
6.3, 1.8, 4.9, 1.2, 1.7, 6.7, 6.1, 1.10, 6.0, 1.2
b. Array weights of 7 components of type int. Initialize this array to the following values:
320, 325, 317, 340, 350, 380, 230
c. Array mySymbols of type char. Initialize this array to the following values:
'#', '^', '!', '%', '@', '&', '$'
d. Array seasons of 4 components of type string. Initialize this array to the following values:
"winter", "spring", "summer", "fall"
7. What is the output of the following C++ code?
Image
Output:
8. Suppose that you have the following function definition:
void sum(int x, int y, int & z)
{
z = x + y;
}
Consider the following declarations:
Which of the following function calls is valid?
a. sum(a, b);
Answer(valid/invalid):
b. sum(list1[0], list3[0], b);
Answer(valid/invalid):
c. sum(list1, list3, c);
Answer(valid/invalid):
d. for(int i = 0; i < 10; i++)
sum(list1[i], list2[i], list3[i]);
Answer(valid/invalid):
9. When an array is passed as an argument (or actual parameter) to a function, is it passed by value or reference?
Answer:
10. Suppose that you have the following declaration:
int list[7] = { 6, 10, 14, 18, 22 };
What is stored in each component of list?
Answer:
Which of the following function calls is valid?
a. sum(a, b);
Answer(valid/invalid):
b. sum(list1[0], list3[0], b);
Answer(valid/invalid):
c. sum(list1, list3, c);
Answer(valid/invalid):
d. for(int i = 0; i < 10; i++)
sum(list1[i], list2[i], list3[i]);
Answer(valid/invalid):
9. When an array is passed as an argument (or actual parameter) to a function, is it passed by value or reference?
Answer:
10. Suppose that you have the following declaration:
int list[7] = { 6, 10, 14, 18, 22 };
What is stored in each component of list?
Answer:
Which of the following function calls is valid?
a. sum(a, b);
Answer(valid/invalid):
b. sum(list1[0], list3[0], b);
Answer(valid/invalid):
c. sum(list1, list3, c);
Answer(valid/invalid):
d. for(int i = 0; i < 10; i++)
sum(list1[i], list2[i], list3[i]);
Answer(valid/invalid):
9. When an array is passed as an argument (or actual parameter) to a function, is it passed by value or reference?
Answer:
10. Suppose that you have the following declaration:
int list[7] = { 6, 10, 14, 18, 22 };
What is stored in each component of list?
Answer:
11. Suppose that you have the following declaration:
int list[10] = { 35, 6, 10, 14, 17, 22, 34, 33, 34, 36 };
Using the selection sort algorithm, show all of the values in the list array after each iteration of the outer loop. You do not need to repeat the values of the array before the first iteration because it is stated above.
{ _ }
{ _ }
{ _ }
{ _ }
{ _ }
{ _ }
{ _ }
{ _ }
{ _ }
12. Mark the following statements as correct or incorrect (incorrect can be either a failure to compile or some other problem with the operation). If a statement is incorrect for some reason, explain why.
a. char str[16] = "kiwi";
strcpy(str, "apples or bananas");
Answer:
b. char str2[16] = "kiwi";
cout << strlen(str2);
Answer:
c. char str3[16];
str3[16] = "kiwi";
Answer:
d. char str4[16];
str4 = "kiwi";
Answer:
e. char str5[16] = "kiwi";
str5[3] = '';
Answer:
Explanation / Answer
1. Consider the following declaration:
double words[60];
In this declaration, identify the following:
a. The array name: words
b. The array size: 60
c. The data type of each array component: double
d. The range of valid indices for the array: 0 to 59
______________
2. Explain why the following declarations are invalid.
a. people[82];
Answer: here we didn’t declared the type of array
b. int myArray[50;
Answer: closing braces in Array
c. int SIZE;
double list[SIZE];
Answer:didn’t mentioned the size value.
d. const int X = 50;
double list[X – 55];
Answer: array size should not be negative(50-55=-5)
e. int ids[-20];
Answer: array size should not be negative(-20)
f. name string[10];
Answer:we didn’t declared type of array
______________
3. Write C++ statements to do the following:
a. Declare an array beta of 10 components of type double with all values initialize to zero.
Ans)double beta[10]={0};
b. Output the value of the first component of the array beta. Assume iostream has been included.
Ans) cout<<beta[0]<<endl;
d. Set the value of the ninth component to 0.5 times the second component.
Ans)beta[8]=0.5*beta[1];
e. Set the value of the sixth component to the seventh component minus the third component.
Ans) beta[5]=beta[6]-beta[2];
f. Output the value of the last component of beta. Assume iostream has been included.
Ans) cout<<beta[8]<<endl;
______________
4. What is stored in myList after the following C++ code executes?
double myList[5];
myList[0] = 3.0;
myList[1] = 4.0;
for (int i = 2; i < 5; i++)
{
myList[i] = myList[i - 1] * myList[i - 2];
if (i > 3)
myList[i] = myList[i] / 2;
}
Answer: 3 4 12 48 288
______________
5. Correct the following code so that it correctly sets the value of each element of myList to 3 times the element’s index.
int myList[10];
for (int i = 1; i <= 10; i--)
myList[i] = 3i;
Answer:
int myList[10];
for (int i = 1; i <= 10; i++)
myList[i] = 3*i;
______________
6. Write C++ statements to define and initialize the following arrays.
a. Array heights of 10 components of type double. Initialize this array to the following values:
6.3, 1.8, 4.9, 1.2, 1.7, 6.7, 6.1, 1.10, 6.0, 1.2
Ans) double heights[]={6.3, 1.8, 4.9, 1.2, 1.7, 6.7, 6.1, 1.10, 6.0, 1.2};
b. Array weights of 7 components of type int. Initialize this array to the following values:
320, 325, 317, 340, 350, 380, 230
Ans) int weights={320, 325, 317, 340, 350, 380, 230};
c. Array mySymbols of type char. Initialize this array to the following values:
'#', '^', '!', '%', '@', '&', '$'
Ans)char mySymbols={'#', '^', '!', '%', '@', '&', '$'};
d. Array seasons of 4 components of type string. Initialize this array to the following values:
"winter", "spring", "summer", "fall"
Ans) string seasons={"winter", "spring", "summer", "fall"};
______________
8. Suppose that you have the following function definition:
void sum(int x, int y, int & z)
{
z = x + y;
}
Consider the following declarations:
Which of the following function calls is valid?
a. sum(a, b);
Answer(valid/invalid): Invalid
b. sum(list1[0], list3[0], b);
Answer(valid/invalid): valid
c. sum(list1, list3, c);
Answer(valid/invalid): Invalid
d. for(int i = 0; i < 10; i++)
sum(list1[i], list2[i], list3[i]);
Answer(valid/invalid): invalid
______________
9. When an array is passed as an argument (or actual parameter) to a function, is it passed by value or reference?
Answer: reference
______________
10. Suppose that you have the following declaration:
int list[7] = { 6, 10, 14, 18, 22 };
What is stored in each component of list?
Answer:
List[0]= 6;
List[1]= 10;
List[2]= 14;
List[3]= 18;
List[4]= 22;
List[5]= 0;
List[6]= 0;
______________
11. Suppose that you have the following declaration:
int list[10] = { 35, 6, 10, 14, 17, 22, 34, 33, 34, 36 };
Using the selection sort algorithm, show all of the values in the list array after each iteration of the outer loop. You do not need to repeat the values of the array before the first iteration because it is stated above.
{6, 35 , 10, 14, 17, 22, 34, 33, 34, 36 }
{6, 10 , 35, 14, 17, 22, 34, 33, 34, 36 }
{ 6, 10 , 14, 35 , 17, 22, 34, 33, 34, 36 }
{ 6, 10 , 14, 17 , 35, 22, 34, 33, 34, 36 }
{ 6, 10 , 14, 17 , 22, 35, 34, 33, 34, 36 }
{ 6, 10 , 14, 17 , 22, 34, 35, 33, 34, 36 }
{ 6, 10 , 14, 17 , 22, 33, 34, 35, 34, 36 }
{ 6, 10 , 14, 17 , 22, 33, 34, 34, 35, 36 }
{ 6, 10 , 14, 17 , 22, 33, 34, 34, 35, 36 }
______________
12. Mark the following statements as correct or incorrect (incorrect can be either a failure to compile or some other problem with the operation). If a statement is incorrect for some reason, explain why.
a. char str[16] = "kiwi";
strcpy(str, "apples or bananas");
Answer: this statement is correct
b. char str2[16] = "kiwi";
cout << strlen(str2);
Answer: this statement is correct
c. char str3[16];
str3[16] = "kiwi";
Answer: here we are trying to assign the string “Kiwi” to the char array element.And also the size of an array is 16 whose index ranging from 0 to 15.But we are trying to assign to 16 element which is also wrong.
d. char str4[16];
str4 = "kiwi";
Answer: here we are trying to assign the String to char array reference .which is incompatable.
e. char str5[16] = "kiwi";
str5[3] = '';
Answer: correct
______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.