My Programming Lab - Ch7

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Declare a 8x8 two-dimensional array of strings named chessboard.

const int SIZE = 8; string chessboard[SIZE][SIZE];

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];

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[9];

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};

Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens?

Another variable or array will very likely be unexpectedly modified.

Given the array a, write an expression that refers to the first element of the array.

a [0]

Given that an array of int named a has been declared, assign 3 to its first element

a[0] = 3;

Given that an array of int named a with 30 elements has been declared, assign 5 to its last element

a[29] = 5;

Given an array a, declared to contain 34 elements, write an expression that refers to the last element of the array.

a[33]

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];

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;

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;

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<size; k++){ if (one[k] == two[size-k-1]){ match++; } } if (match==size){ return true; }else{ return false; } }

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.

bool testPIN(int custPIN[], int databasePIN[], int size) { if (countMatches(custPIN, databasePIN, size)== size) return true; else return false; }

Write a statement that declares an array of char named streetAddress that contains exactly eighty elements.

char streetAddress[80];

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; } }

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<n/2; k++){ temp=a[k]; a[k]=a[n-k-1]; a[n-k-1]=temp; }

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;

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.

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; }

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};

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[0] > annualCost[1]) best2 = healthOption[1]; else best2 = healthOption[0]; }

Declare an array named scores of twenty-five elements of type int.

int scores[25];

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; }

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.

int westboundHollandTunnelTraffic[10][52][7][24];

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.

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]; } }

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);

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[4] = 160000;

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; }

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);

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] ;

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[2][3] + x[4][0]

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[4][3]

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]


Set pelajaran terkait

CLPS 0450 Final Exam Questions + Concepts

View Set

CompTIA Security+ Final Assessment (real)

View Set

college board questions + other things to know for bio

View Set

Psychology: Unit 4; Quiz 1: Learning; Quiz 2: Memory; Quiz 3: Thinking, Language, Consciousness, and Sleeping; Test

View Set

InterComm Ch. 1-4 - Quiz Questions

View Set