C++ Chapters 9

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

zeroIt(&x);

zeroIt is a function that takes one argument and returns no value. The argument is a pointer to int . The function stores the value 0 back into the variable pointed to by the argument. x is an int variable that has been declared. Write a statement that sets the value stored in x to zero by invoking the function zeroIt .

counterPointer = &counter;

Assume that an int variable counter has already been declared. Assume further a variable counterPointer of type "pointer to int " has also already been declared. Write a statement that makes counterPointer "point" to counter .

diffPointer = &diff;

Assume that an int variable diff has already been declared. Assume further a variable diffPointer of type "pointer to int " has also already been declared. Write a statement that assigns the address of diff to diffPointer .

tp = jp; jp = ip; ip = tp;

Assume that ip , jp , and tp have all been declared to be pointers to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array and that jp has been initialized to point to an element in the second half of the array.

jp = ip + 5;

Assume that ip and jp have both been declared to be pointers to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array. Write a statement that makes jp point to the element in the array 5 positions after the one that ip points to.

jp = ip + 1;

Assume that ip and jp have both been declared to be pointers to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array. Write a statement that makes jp point to the element in the array just after the one that ip points to.

ip = enrollment;

Assume that ip has been declared to be a pointer to int and that enrollment has been declared to be an array of 20 elements. Write a statement that makes ip point to the first element in the array.

ip = &enrollment[19];

Assume that ip has been declared to be a pointer to int and that enrollment has been declared to be an array of 20 elements. Write a statement that makes ip point to the last element in the array.

ip = &enrollment[section];

Assume that ip has been declared to be a pointer to int and that enrollment has been declared to be an array of 20 elements. Assume also that section has been declared to be an int and has been initialized to some value between 5 and 10. Write a statement that makes ip point to the element in the array indexed by section .

ip ++;

Assume that ip has been declared to be a pointer to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array. Write a statement that makes ip point to the next element in the array.

*(ip + 1)

Assume that ip has been declared to be a pointer to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array. Write an expression whose value is the element in the array after the element that ip points to.

*ip + (*ip + 1) + (*ip + 2)

Assume that ip has been declared to be a pointer to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array. Write an expression whose value is the sum of the element that ip points to plus the next two elements.

*ip3 = *ip1 + *ip2;

Assume that ip1 , ip2 , and ip3 have already been declared to be of type "pointer to int ". Assume further that each of these pointer variables have been initialized -- each points to some int variable. Write a statement that computes the sum of the variables that ip1 and ip2 point to, and assigns that value (the sum) to the variable that ip3 points to.

*strikeCounter += 22;

Assume that strikeCounter has already been declared to be a "pointer to int ". Assume further that strikeCounter has been initialized -- its value is the address of some int variable. Write a statement that adds 22 to the value of the variable that strikeCounter is pointing to.

*strikeCounter = 27;

Assume that strikeCounter has already been declared to be a "pointer to int ". Assume further that strikeCounter has been initialized -- its value is the address of some int variable. Write a statement that assigns the value 27 to the variable that strikeCounter is pointing to.

*strikeCounter * 4

Assume that strikeCounter has already been declared to be a "pointer to int ". Assume further that strikeCounter has been initialized -- its value is the address of some int variable. Write an expression whose value is four (4) times the value of the variable that strikeCounter is pointing to.

&diff

Assume the variable diff has already been declared. Write an expression whose value is the address of diff .

char *cp;

Declare a variable cp that can be assigned the address of an char variable. In other words, declare cp to be of type "pointer to char ".

double *dp;

Declare a variable dp that can be assigned the address of a double variable. In other words, declare dp to be of type "pointer to double ".

int *ip;

Declare a variable ip that can be assigned the address of an int variable. In other words, declare ip to be of type "pointer to int ".

bool* bp; bp = new bool; *bp = true;

Declare a variable, bp, as a pointer to bool, dynamically allocate memory for a single boolean value, assign the resulting pointer to bp and initialize the value to true.

ip = new int; *ip = 27;

Given the variable ip, already declared as a pointer to an integer, write the code to dynamically allocate memory for a single integer value, assign the resulting pointer to ip, and initialize the integer value to 27.

int *squares; int n = 0; for (int i = first; i <= last; i++) { n++; } squares = new int[n]; for (int j = first; j <= last; j++) squares[j - first] = j*j;

The integer variables first and last have been declared and assigned values (with last >= first ). Allocate an integer array that can hold the squares of the numbers between first and last (inclusively), assign the resulting pointer to the variable squares (which you must declare), and initialize the array to the squares of the numbers from first to last (in that order).

for(int i = 0; i < 26; i++) { cp_arr[i] = new char; *(cp_arr[i]) = (char)(65 + i); }

The variable cp_arr has been declared as an array of 26 pointers to char. Allocate 26 character values, initialized to the letters 'A' through 'Z' and assign their pointers to the elements of cp_arr (in that order).

double *dp; dp = new double[n];

The variable dp is to refer to an array of double. Assuming the integer variable n has been assigned a value, declare dp appropriately, allocate an array of n doubles and assign the resulting pointer to dp.

for(int i = 0; i < 20; i++) { ip_arr[i] = new int; }

The variable ip_arr has been declared as an array of 20 pointers to integer (i.e., each element of ip_arr is a pointer to an integer). Allocate 20 integer values and assign their pointers to the elements of ip_arr.

arr2 = new int[20]; for (int i = 0; i < 10 ; i++) { *(arr2 + i) = *(arr1 + i); } for (int j = 10; j < 20; j++) { *(arr2 + j) = 0; }

The variables arr1 and arr2 have been declared as pointers to integers. An array of 10 elements has been allocated, its pointer assigned to arr1, and the elements initialized to some values. Allocate an array of 20 elements, assign its pointer to arr2, copy the 10 elements from arr1 to the first 10 elements of arr2, and initialize the remainder of the elements of arr2 to 0.

int *temp; temp = xp; xp = yp; yp = temp;

The variables xp and yp have both been declared as pointers to integers, and have been assigned values (i.e., they are each pointing to an integer value). Write the code to exchange the values of these two variables (so that after the swap xp points to what yp originally pointed to and vice-versa-- in other words, in this exercise, you are swapping the pointers). Declare any necessary variables.

int x = 0; int *temp = &x; *temp = *xp; *xp = *yp; *yp = *temp;

The variables xp and yp have both been declared as pointers to integers, and have been assigned values. Write the code to exchange the two integers (so that after the swap xp still points at the same location, but it now contains the integer value originally contained in the location pointed to by y; and vice versa-- in other words, in this exercise you are swapping the integers, not the pointers). Declare any necessary variables.

void divide (int x, int y, int *q, int *r);

Write a statement that declares a prototype for a function divide that takes four arguments and returns no value. The first two arguments are of type int . The last two arguments arguments are pointers to int that are set by the function to the quotient and remainder of dividing the first argument by the second argument. The function does not return a value.

void doubleIt(int *x);

Write a statement that declares a prototype for a function doubleIt , which has a single parameter of type pointer to int .

void minMax(int x, int y, int z, int * big, int * small);

Write a statement that declares a prototype for a function minMax , which has five parameters. The first three parameters are integers. The last two are parameters that are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value.

void tripleIt (int *x);

Write a statement that declares a prototype for a function tripleIt , which can be used as follows: int x=5; tripleIt(&x); /* x is now equal to 15 */

void zeroIt (int *x);

Write a statement that declares a prototype for a function zeroIt , which can be used as follows: int x=5; zeroIt(&x); /* x is now equal to 0 */

void divide (int x, int y, int* q, int* r) { *q = x / y; *r = x % y; }

Write the definition of a function divide that takes four arguments and returns no value. The first two arguments are of type int . The last two arguments arguments are pointers to int and are set by the function to the quotient and remainder of dividing the first argument by the second argument. The function does not return a value. The function can be used as follows:

void doubleIt (int * x) { *x = *x * 2; }

Write the definition of a function doubleIt , which doubles the value of its argument but returns nothing so that it can be used as follows: int x=5; doubleIt(&x); /* x is now equal to 10 */

void minMax(int x, int y, int z, int* big, int* small) { int min; int max; min = x; if (min > y) min = y; if (min > z) min = z;

Write the definition of a function minMax that has five parameters. The first three parameters are integers. The last two are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value. The function can be used as follows: int a=31, b=5, c=19 big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */

void tripleIt (int * x) { *x = *x * 3; }

Write the definition of a function tripleIt , which triples its argument but returns nothing so that it can be used as follows: int x=5; tripleIt(&x); /* x is now equal to 15 */

void zeroIt (int * x) { *x = 0; }

Write the definition of a function zeroIt , which is used to zero out a variable. The function is used as follows: int x=5; zeroIt(&x); /* x is now equal to 0 */

divide(x, y, &q, &r);

divide is a function that takes four arguments and returns no value. The first two arguments are of type int . The last two arguments arguments are pointers to int that are set by the function to the quotient and remainder of dividing the first argument by the second argument. The function does not return a value. x and y are two int variables that have been declared and initialized. q and r are two int variables that have been declared. Write a statement that sets the value of q to the quotient of x divided by y , and sets the value of r to the remainder of x divided by y , by calling divide .

doubleIt(&savings);

doubleIt is a function that takes one argument and returns no value. The argument is a pointer to int . The function doubles the value that the argument points to and stores it back. savings is an int variable that has been declared and initialized. Write a statement that doubles the value stored in savings by invoking the function doubleIt . For example, if the value in savings was 7 before your statement, after your statement its value would be 14.

minMax (x, y, z, &big, &small);

minMax is a function that takes five arguments and returns no value. The first three arguments are of type int . The last two arguments are pointers to int that are set by the function to the largest and smallest of the values of the first three parameters, respectively. x , y and z are three int variables that have been declared and initialized. big and small are two int variables that have been declared. Write a statement that sets the value of big to the largest of x , y , z and sets the value of small to the smallest of x , y , z by calling minMax .

tripleIt(&penalty);

tripleIt is a function that takes one argument and returns no value. The argument is a pointer to int . The function triples the value that the argument points to and stores it back. penalty is an int variable that has been declared and initialized. Write a statement that triples the value stored in penalty by invoking the function tripleIt . For example, if the value in penalty was 7 before your statement, after your statement its value would be 21.


Set pelajaran terkait

Project and Development Lifecycles

View Set

Chapter 26- Seed Plants- Hightower Bio1108

View Set

Rule of 9's and the Parkland Formula Burns practice questions

View Set

Exam #1 Business Statistics, BUS 221: Exam 1

View Set