# My Programming Lab - Ch7

## Unlock all answers in this set

question
Declare an array named scores of twenty-five elements of type int.
int scores;
question
Write a statement that declares an array of char named streetAddress that contains exactly eighty elements.
question
Given the array a, write an expression that refers to the first element of the array.
a 
question
Given an array a, declared to contain 34 elements, write an expression that refers to the last element of the array.
a
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 ).
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 .
cout << monthSales;
question
Given that an array of int named a has been declared, assign 3 to its first element
a = 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.
salarySteps = 160000;
question
Given that an array of int named a with 30 elements has been declared, assign 5 to its last element
a = 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
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]
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!
a[j] = 2 * a[j+1];
question
Suppose v is an array with 100 int elements. If 100 is assigned to v, what happens?
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.
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.)
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.
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
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.)
int i; for(i = 0; i < 10; i++){ if(annualCost > annualCost) best2 = healthOption; else best2 = healthOption; }
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
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.
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.
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.
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.)
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.
const int SIZE = 8; string chessboard[SIZE][SIZE];
question
Declare a two-dimensional array of integers named arr with 3 rows and 10 columns.
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
x + x
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).
x
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.
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.
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.