$40
ENGG1340 Computer Programming II
Module 6 Self-Review Exercise
1. Write a single statement to accomplish each of following tasks:
(a) Use a stream manipulator to ensure that floating-point values print in scientific notation for when using cout .
(b) Use a stream manipulator to set the fill character to '*' for printing in field widths larger than the values being output using cout .
(c) Print 6789 right justified in an 8-digit field.
2. Write a C++ statement that uses the manipulator setfill to output a line containing 40
pound signs, i.e., “######################################## ”.
3. Identify error(s), if any, in the following array declarations. If a statement is incorrect, provide the correct statement.
(a) double weights[100];
(b) int age[0..80];
(c) int100 list[];
(d) double[50] salaries;
4. Correct the following code so that it correctly sets the value of each element of myList to the index of the element.
int myList[10];
for (int i = 1; i <= 10; i--) myList[i] = [i];
5. What is stored in list after the following C++ code executes?
int list[10];
list[0] = 2; list[1] = 3;
for (int i = 2; i < 10; i++)
{ list[i] = list[i - 1] + list[i - 2]; if (i 7) list[i] = 2 * list[i] - list[i - 2];
}
6. Determine whether the following array declarations are valid. If a declaration is valid, determine the size of the array.
(a) int list[] = {18, 13, 14, 16};
(b) int x[10] = {1,7,5,3,2,8};
(c) double y[4] = { 2.0, 5.0, 8.0, 11.0, 14.0} ;
(d) int list[7] = {12, 13, , 14, 16, , 8};
7. Write a single statement for each of the following one-dimensional array operations:
(a) Initialize the 10 elements of integer array counts to zero.
(b) Add 1 to each of the 15 elements of the integer array bonus .
(c) Read 12 values for double array scores from the keyboard.
Self-Review Exercise Module 6 p. 1/2
8. Write a code segment that finds the minimum and maximum values contained in a
99-element double array w .
9. Write a code segment that finds the minimum and maximum values contained in a 4-by-6 int array t . (The declaration for t is int t[4][6]; )
10.Consider the following C++ code:
string str1; string str2; char ch; int index; cin str1; cin str2; cin index; ch = str1[index]; str1[index] = str2[index]; str2[index] = ch;
cout << str1 << " " << str2 << endl;
Answer the following questions:
(a) What is the output if the input is Hello There 2 ?
(b) What is the output if the input is Diamond Gold 0 ?
(c) What is the output if the input is C++ Java 1 ?
11.What is the output of the following C++ code?
string str1 = "Trip to Hawaii"; string str2 = "Summer or Fall"; string newStr;
newStr = str2 + ' ' + str1;
cout << newStr << endl;
cout << str1 + " in " + str2 << endl; cout << newStr.length() << endl; cout << str1.find('H') << endl; cout << str2.find("or") << endl; cout << newStr.substr(10, 19) << endl;
cout << newStr.replace(23, 6, "******") << endl;
string str = "C++ Programming"; cout << str << endl;
cout << str.length() << endl;
str[0] = 'J';
str[2] = '$';
cout << str << endl;
12.Find the error(s) in each of the following, and explain how to correct it (them):
(a) string string1( 28 ); // construct string1
(b) string string2( 'z' ); // construct string2
Self-Review Exercise Module 6 p. 2/2