My Programming Lab - Ch7

24 July 2022
4.7 (114 reviews)
33 test answers

Unlock all answers in this set

Unlock answers (29)
question
Declare an array named scores of twenty-five elements of type int.
answer
int scores[25];
question
Write a statement that declares an array of char named streetAddress that contains exactly eighty elements.
answer
char streetAddress[80];
question
Given the array a, write an expression that refers to the first element of the array.
answer
a [0]
question
Given an array a, declared to contain 34 elements, write an expression that refers to the last element of the array.
answer
a[33]
question
Assume that the array arr has been declared. In addition, assume that ARR_SIZE has been defined to be an integer that equals the number of elements in arr . Write a statement that assigns to x the value of the next to last element of the array ( x has already been declared ).
answer
x = arr[ARR_SIZE-2] ;
question
Assume that the array monthSales of integers has already been declared and that its elements contain sales data for the 12 months of the year in order (i.e., January, February, etc.). Write a statement that writes to standard output the element corresponding to October. Do not write anything else out to standard output .
answer
cout << monthSales[9];
question
Given that an array of int named a has been declared, assign 3 to its first element
answer
a[0] = 3;
question
Assume that an array of integers named salarySteps that contains exactly five elements has been declared . Write a statement that assigns the value 160000 to the last element of the array salarySteps.
answer
salarySteps[4] = 160000;
question
Given that an array of int named a with 30 elements has been declared, assign 5 to its last element
answer
a[29] = 5;
question
Assume that an array of int named a has been declared with 12 elements and that the integer variable k holds a value between 0 and 6. Assign 15 to the array element whose index is k
answer
a[k] = 15;
question
Given that an array of int named a has been declared with 12 elements and that the integer variable k holds a value between 2 and 8. Assign 22 to the element just before a[k]
answer
a[k-1] = 22;
question
An array of int named a that contains exactly five elements has already been declared and initialized. In addition, an int variable j has also been declared and initialized to a value somewhere between 0 and 3. Write a single statement that assigns a new value to the element of the array indexed by j. This new value should be equal to twice the value stored in the next element of the array (i.e. the element after the element indexed by j ). Do not modify any other elements of the array!
answer
a[j] = 2 * a[j+1];
question
Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens?
answer
Another variable or array will very likely be unexpectedly modified.
question
Declare an array named taxRates of 5 elements of type double and initialize the elements (starting with the first) to the values 0.10, 0.15, 0.21, 0.28, 0.31, respectively.
answer
double taxRates [] = {0.10, 0.15, 0.21, 0.28, 0.31};
question
Write a statement to declare and initialize an array of int named denominations that contains exactly six elements . Your declaration statement should initialize the elements of the array to the following values: 1, 5, 10, 25, 50, 100. (The value 1 goes into the first element; the value 100 to the last.)
answer
int denominations[] = {1, 5, 10, 25, 50, 100};
question
Given an array temps of double, containing temperature data, and an int variable n that contains the number of elements in temps : Compute the average temperature and store it in a variable called avgTemp . Besides temps, n, and avgTemp, you may use only two other variables -- an int variable k and a double variable total, which have been declared.
answer
for(total = 0, k = 0; k < n; k++) total += temps[k]; avgTemp = total / n;
question
Reversing the elements of an array involves swapping the corresponding elements of the array : the first with the last, the second with the next to the last, and so on, all the way to the middle of the array. Given an array a, an int variable n containing the number of elements in a, and two other int variables, k and temp, write a loop that reverses the elements of the array . Do not use any other variables besides a , n , k , and temp
answer
for (int k=0; k
question
Assume that two parallel arrays have been declared and initialized: healthOption an array of type char that contains letter codes for different healthcare options and annualCost an array of type int. The i-th element of annualCost indicates the annual cost of the i-th element of healthOption. In addition, there is an char variable, best2.Write the code necessary to assign to best2 the health option with the lower annual cost, considering only the first two healthcare options. Thus, if the values of healthOption are 'B', 'Q', 'W', 'Z' and the values of annualCost are 8430, 9400, 7050, 6400 your code would assign 'B' to best2 because 8430 is less than 9400 and is associated with 'B' in the parallel array. (We ignore 'W' and 'Z' because we are considering only the first two options.)
answer
int i; for(i = 0; i < 10; i++){ if(annualCost[0] > annualCost[1]) best2 = healthOption[1]; else best2 = healthOption[0]; }
question
printArray is a function that has two parameters. The first parameter is an array of element type int and the second is an int, the number of elements in the array. The function prints the contents of the array parameter; it does not return a value. inventory is an array of int s that has been already declared and filled with values. n is an int variable that holds the number of elements in the array . Write a statement that prints the contents of the array inventory by calling the function printArray
answer
printArray(inventory, n);
question
Write a statement that declares a prototype for a function printArray, which has two parameters. The first parameter is an array of element type int and the second is an int, the number of elements in the array. The function does not return a value.
answer
void printArray(int[], int);
question
Write the definition of a function printArray , which has two parameters. The first parameter is an array of int s and the second is an int, the number of elements in the array. The function does not return a value. The function prints out each element of the array, on a line by itself, in the order the elements appear in the array, and does not print anything else.
answer
void printArray(int s[], int m){ int n; for(n = 0; n< m; n++) cout << s[n] << endl; }
question
Write the definition of a function named sumArray that receives two parameters: an array of element type int and an int that contains the number of elements of the array. The function returns the sum of the elements of the array as an int.
answer
int sumArray(int a[], int n){ int index; int sum = 0; for(index = 0; index < n; index++) sum += a[index]; return sum; }
question
Write the definition of a function, isReverse, whose first two parameters are arrays of integers of equal size, and whose third parameter is an integer indicating the size of each array. The function returns true if and only if one array is the reverse of the other. ("Reverse" here means same elements but in reverse order.)
answer
bool isReverse (int one[], int two[], int size){ int match=0; for (int k=0; k
question
Declare a 8x8 two-dimensional array of strings named chessboard.
answer
const int SIZE = 8; string chessboard[SIZE][SIZE];
question
Declare a two-dimensional array of integers named arr with 3 rows and 10 columns.
answer
const int row = 3; const int column = 10; int arr[row][column];
question
Given a two-dimensional array x of element type int, write an expression whose value is the sum of the element in the 3rd row/4th column and the element in the 5th row/1st column
answer
x[2][3] + x[4][0]
question
Given a two-dimensional array x of element type int with 5 rows and 4 columns, write an expression whose value the last element in the array (the last column of the last row).
answer
x[4][3]
question
Given a two-dimensional array x of element type double, and two integer variables i and j, write an expression whose value is the i-th element in the j-th row.
answer
x[i][j]
question
Given a two-dimensional array of integers named q, with 2 rows and 4 columns, write some code that puts a zero in every element of q. Declare any variables needed to help you.
answer
for (int i = 0; i < 2; i++){ for (int k = 0; k <4; k++) { q[i][k] = 0; } }
question
You are given a 6x8 (6 rows, 8 columns) array of integers, x, already initialized and three integer variables: max, i and j. Write the necessary code so that max will have the largest value in the array x.
answer
max = x[0][0]; for(int i = 0; i < 6; i++){ for (int j = 0; j < 8; j++){ if(x[i][j] > max) max = x[i][j]; } }
question
Declare an array of integers, westboundHollandTunnelTraffic that can store the number of vehicles going westbound through the Holland Tunnel on a particular hour (numbered 0 through 23) on a particular day (numbered 0 through 6) on a particular week numbered (0 through 51) over the last ten years (numbered 0 through 9). The innermost dimension should be years, with the next being weeks, and so on.
answer
int westboundHollandTunnelTraffic[10][52][7][24];
question
Consider the testPIN function used in Program 7-21. For convenience, we have reproduced the code for you below. Modify this function as follows:change its type to int change its name to countMATCHES make it return the number of corresponding parallel elements that are equal.
answer
int countMatches(int custPIN[], int databasePIN[], int size) { int matches = 0; for (int index = 0; index < size; index++) { if (custPIN[index] == databasePIN[index]) matches++; } return matches; }
question
Again, consider the testPIN function used in Program 7-21. For convenience, we have reproduced the code for you below.Rewrite the function body of this function so that instead of using iterating through the elements of the parallel arrays, it returns true or false by calling countMatches the function defined in the previous exercise (10988), and checking its return value.
answer
bool testPIN(int custPIN[], int databasePIN[], int size) { if (countMatches(custPIN, databasePIN, size)== size) return true; else return false; }