Chapter 9 (Pointers) – Review Questions
Short Answer
1. What does the indirection operator do?
2. Look at the following code.
int x = 7;
int *ptr = &x;
What will be displayed if you send the expression *ptr to cout? What happens if you send the expression ptr to cout?
3. So far you have learned three different uses for the * operator. What are they?
4. What math operations are allowed on pointers?
5. Assuming ptr is a pointer to an int, what happens when you add 4 to ptr?
6. Look at the following array definition.
int numbers[] = {2, 4, 6, 8, 10};
What will the following statement display?
cout << *(numbers + 3) << endl;
7. What is the purpose of the new operator?
8. What is the purpose of the delete operator?
9. Under what circumstances can you successfully return a pointer from a function?
10. What is the difference between a pointer to a constant and a constant pointer?
11. What are two advantages of declaring a pointer parameter as a constant pointer?
Fill-in-the-Blank
12. Each byte of memory is assigned a unique ___________.
13. The _________ operator can be used to determine a variable’s address.
14. ____________variables are designed to hold addresses.
15. The ___________ operator can be used to work with the variable a pointer points to.
16. Array names can be used as _________, and vice versa.
17. Creating variables while a program is running is called ____________.
18. The ____________ operator is used to dynamically allocate memory.
19. A pointer that contains the address 0 is called a(n) ________ pointer.
20. When a program is finished with a chunk of dynamically allocated memory, it should free it with the __________ operator.
21. You should only use pointers with delete that were previously used with __________.
Algorithm Workbench
22. Look at the following code:
double value = 29.7;
double *ptr = &value;
Write a cout statement that uses the ptr variable to display the contents of the value variable.
23. Look at the following array definition:
int set[10];
Write a statement using pointer notation that stores the value 99 in set[7]
24. Write a code that dynamically allocates an array of 20 integers, then uses a loop to allow the user to enter values for each element of the array.
25. Assume tempNumbers is a pointer that points to a dynamically allocated array. Write code that releases the memory used by the array.
26. Look at the following function definition:
void getNumber(int &n)
{
cout << “Enter a number: “;
cin >> n;
}
In this function, the parameter n is a reference variable. Rewrite the function so n is a pointer.
27. Write the definition of ptr, a pointer to a constant int.
28. Write the definition of ptr, a constant pointer to an int.
True or False
29. Each byte of memory is assigned a unique address. T F
30. The * operator is used to get the address of a variable. T F
31. Pointer variables are designed to hold addresses. T F
32. The & symbol is called the indirection operator. T F
33. The & operator dereferences a pointer. T F
34. When the indirection operator is used with a pointer variable, you are actually working with the value the pointer is pointing to. T F
35. Array names cannot be dereferenced with the indirection operator. T F
36. When you add a value to a pointer, you are actually adding that number times the size of the data type referenced by the pointer. T F
37. The address operator is not needed to assign an array’s address to a pointer. T F
38. You can change the address that an array name points to. T F
39. Any mathematical operation, including multiplication and division, may be performed on a pointer. T F
40. Pointers may be compared using the relational operators. T F
41. When used as function parameters, reference variables are much easier to work with than pointers. T F
42. The new operator dynamically allocates memory. T F
43. A pointer variable that has not been initialized is called a null pointer. T F
44. The address 0 is generally considered unusable. T F
45. In using a pointer with the delete operator, it is not necessary for the pointer to have been previously used with the new operator. T F
Find the Error
Each of the following definitions and program segments has errors. Locate as many as you can.
46. int ptr* = nullptr;
47. int x, *ptr = nullptr;
&x = ptr;
48. int x, *ptr = nullptr;
*ptr = &x;
49. int x, *ptr = nullptr;
ptr = &x;
ptr = 100; //Store 100 in x
cout << x << endl;
50. int numbers[] = {10, 20, 30, 40, 50};
cout << “The third element in the array is ”;
cout << *numbers + 3 << endl;
51. int values[20], *iptr = nullptr;
iptr = values;
iptr *= 2;
52. float level;
int fptr = &level;
53. int *iptr = &ivalue;
int ivalue;
54. void doubleVal(int val)
{
*val *= 2;
}
55. int *pint = nullptr;
new pint;
56. int *pint = nullptr;
pint = new int;
if (pint == nullptr)
*pint = 100;
else
cout << “Memory allocation error\n”;
57. int *pint = nullptr;
pint = new int[100]; //Allocate memory
.
.
Code that process the array.
.
.
delete pint; // Free memory
58. int *getNum()
{
int wholeNum;
cout << “Enter a number: “;
cin >> wholeNum;
return &wholeNum;
}
59. const int arr[] = {1, 2, 3};
int *ptr = arr;
60. void doSomething(int * const ptr)
{
int localArray[] = {1, 2, 3};
ptr = localArray;
}
In: Computer Science
Rossdale Co. stock currently sells for $73.31 per share and has a beta of 1.24. The market risk premium is 6.90 percent and the risk-free rate is 2.82 percent annually. The company just paid a dividend of $4.37 per share, which it has pledged to increase at an annual rate of 3.25 percent indefinitely. What is your best estimate of the company's cost of equity?
Multiple Choice
9.13%
9.53%
7.88%
10.39%
11.19%
In: Finance
***Using Java
Using the switch/case construct, write a program that takes a character from the user and classifies it as a number (‘1’,’2’, ‘3’, …), a letter from the alphabet (‘A’, ‘a’, ‘B’, ‘b’, …, ‘Z’, ‘z’), an arithmetic operator (‘+’, ‘-‘, ‘/’, ‘*’, ‘%’), a comparison operator (‘<’, ‘>’, ‘=’), punctuation (‘!’, ‘?’, ‘,’, ‘,’, ‘:’, ‘;’), and all other characters as a Special Character, and informs the user of the same.
If the user entered the character A, the system should say: “You entered a letter!”
If the user entered the character (, the system should say: “You entered a special character!”
In: Computer Science
Write a java program
The last digit of a credit card number is the check digit, which protects againts transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Follwing steps explains the algorithm in determining if a credit card number is a valid card.
Starting from the right most digit, form the sum of every other digit. For example, if the credit card is number is 43589795 then you form the sum 5+7+8+3 = 23
1
Double each digit that we have not included in the preceding step. Add all digits of resulting numbers. For example, with the number given above, doubling the digits starting with next to last one, yields 18, 18, 10, 8. Adding all digits in these values yield 1+8+1+8+1+0+8 = 27
Add sum of the two preceding steps. If the last digit of the result is zero, then the number is valid number
Write a Java program that implements this algorithm (Designing your solution and perhaps wring the algorithm in pseudocode might be helpful). Your program should ask the user 8 digit credit card number and the printout if the credit card is valid or invalid card
Grading Criteria:
a) The correctness of your program/solution
b) Variable naming (self describing)
c) Identification of proper data type and constants (if applicable)
d) Appropriate commenting and Indentation.
NOTE: the given card number 43589795 should produce a valid number
In: Computer Science
Do you think that human beings are essentially good or bad, or a combination of both? Why? Argue for, and bring evidence to support, the position you have taken. How does your position affect your approach to morality - for example, should a moral system be strict, clear, and absolutistic, or, permissive, flexible, and relativistic? Your answer should be in essay form, and a minimum of 500-700 words in length.
In: Psychology
/*
* Add operation counts
* f(N) formula (show your work)
* O(N) reduction -
*/
public static long sum2(int N)//f(N) = ; O(N) =
{
long opCount = 0;
long sum = 0;
for(int i = 0; i < N; i++)
{
for(int j = 0; j
< N; j++)
{
sum++;
}
}
System.out.println("f(N) = [your
answer]");
System.out.println("O(N) = [your
answer]");
System.out.println("OpCount :
"+opCount);
return sum;
}
In: Computer Science
The space shuttle carries about 7.35×104 kg of solid
aluminum fuel, which is oxidized with ammonium perchlorate
according to the following reaction:
10Al(s)+6NH4ClO4(s)→4Al2O3(s)+2AlCl3(s)+12H2O(g)+3N2(g)
The space shuttle also carries about 6.02×105 kg of
oxygen (which reacts with hydrogen to form gaseous water).
Part A
Assuming that aluminum and oxygen are the limiting reactants, determine the total energy produced by these fuels. (ΔH∘ffor solid ammonium perchlorate is -295 kJ/mol.)
Part B
Suppose that a future space shuttle was powered by matter-antimatter annihilation. The matter could be normal hydrogen (containing a proton and an electron) and the antimatter could be antihydrogen (containing an antiproton and a positron). What mass of antimatter would be required to produce the energy equivalent of the aluminum and oxygen fuel currently carried on the space shuttle?
In: Chemistry
Why is the applicability of statistical quality control (SQC) an important attribute of an MTS operation? Why is it not applicable to an MTO operation?
In: Civil Engineering
Hello! I need to add a method that will display the total number of times the user answered the question correctly, how many time he was right or wrong and the percentage of time they were correct in answering. import java.util.Random; import java.util.Scanner; public class Test { private static int getUserInput() { int n; Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); return n; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String choice = "Yes"; Random random = new Random(); int[] userdata = new int[100]; int count = 0; while (!choice.equals("No")) { int randomInt = 2 * (random.nextInt(5) + 1); System.out.println("write the number in half " + randomInt + "?"); userdata[count++] = randomInt; int userInput = getUserInput(); if (userInput == (randomInt / 2)) { System.out.println("That is correct!"); } else { System.out.println("That is incorrect!"); } System.out.print("Generate another random number?"); choice = scanner.next(); } int min = userdata[0], max = userdata[0]; for (int i = 0; i < count; i++) { if (userdata[i] > max) max = userdata[i]; if (userdata[i] < min) min = userdata[i]; } System.out.println("The highest random number you were given: " + max); System.out.println("The lowest random number you were given: " + min); } }
In: Computer Science
Let X be a non-degenerate ordered set with the order topology. A
non-degenerate set is a set with more than one
element.
Show the following:
(1) every open interval is open, (2) every closed interval is
closed, (3) every open
ray is open, and (4) every closed ray is closed.
Please note: Its a topology question.
In: Advanced Math
You have created about 50 users for a new branch office of the csmtech.local domain that will be opening soon. The accounts are in the BranchOff OU and are currently disabled. You want to enable them when the branch office opens next week. Construct a dsquery command that pipes to a dsmod command that will enable all accounts that are disabled
In: Computer Science
In C++, Create a program that can try out every possible logical combination of the variables A, B, and C, and determine which combinations will yield a true statement. Take note that there are eight different possible combinations of the three variables. Make certain you test all eight of the combinations.
(1) (A and B) or (A and C)
(2) (A and C) and (B and !C)
(3) (A or B) and !(B or C)
(4) (A or (!A and C)) and (!A and !B)
(5) ((B and C) or (C and A)) and ((A or B) and C)
In: Computer Science
ANALYZING DIFFERENT SIDES OF AN ISSUE
For each of the following issues, identify reasons that support each side of the issue. Issue:
Issue: |
|
1. Multiple choice and true/false exams should be given in collegelevel courses. |
Multiple choice and true/false exams should not be given in college-level courses. |
Issue: |
|
2. Immigration quotas should be reduced. |
Immigration quotas should be increased. |
Issue: |
|
3. The best way to deal with crime is to give long prison sentences. |
Long prison sentences will not reduce crime. |
Issue: |
|
4. When a couple divorces, the children should choose the parent with whom they wish to live. |
When a couple divorces, the court should decide all custody issues regarding the children. |
In: Psychology
Saint Nick Enterprises has 15,500 shares of common stock outstanding at a price of $59 per share. The company has two bond issues outstanding. The first issue has 7 years to maturity, a par value of $1,000 per bond, and sells for 94 percent of par. The second issue matures in 21 years, has a par value of $2,000 per bond, and sells for 101.5 percent of par. The total face value of the first issue is $150,000, while the total face value of the second issue is $250,000. What is the capital structure weight of debt?
Multiple Choice
.2737
.3015
.3468
.3733
.1938
In: Finance
Part A
Pick an appropriate solvent to dissolve isopropyl alcohol (polar, contains an OHOH group) .
Check all that apply.
Hexane (C6H14)(C6H14) |
Ethanol (CH3CH2OH)(CH3CH2OH) |
Methanol (CH3OH)(CH3OH) |
Toluene (C7H8)(C7H8) |
Water (H2O)(H2O) |
Carbon tetrachloride (CCl4)(CCl4) |
Part B
State the kind of intermolecular forces that would occur between the solute and solvent in isopropyl alcohol (polar, contains an OHOH group) .
Check all that apply.
dispersion |
ion-dipole |
hydrogen bonding |
dipole-dipole |
Part C
Pick appropriate solvent(s) to dissolve sodium chloride (ionic).
Check all that apply.
Methanol (CH3OH)(CH3OH) |
Ethanol (CH3CH2OH)(CH3CH2OH) |
Hexane (C6H14)(C6H14) |
Toluene (C7H8)(C7H8) |
Water (H2O)(H2O) |
Carbon tetrachloride (CCl4)(CCl4) |
Part D
State the kind of intermolecular forces that would occur between the solute and solvent in sodium chloride (ionic).
Check all that apply.
ion-dipole |
dispersion |
dipole-dipole |
hydrogen bonding |
Part E
Pick appropriate solvent(s) to dissolve paraffine oil (nonpolar).
Check all that apply.
Ethanol (CH3CH2OH)(CH3CH2OH) |
Toluene (C7H8)(C7H8) |
Carbon tetrachloride (CCl4)(CCl4) |
Hexane (C6H14)(C6H14) |
Methanol (CH3OH)(CH3OH) |
Water (H2O)(H2O) |
Part F
State the kind of intermolecular forces that would occur between the solute and solvent in paraffine oil (nonpolar).
Check all that apply.
ion-dipole |
dipole-dipole |
hydrogen bonding |
dispersion |
SubmitRequest Answer
Part G
Pick an appropriate solvent to dissolve sodium nitrate (ionic).
Check all that apply.
Ethanol (CH3CH2OH)(CH3CH2OH) |
Water (H2O)(H2O) |
Toluene (C7H8)(C7H8) |
Methanol (CH3OH)(CH3OH) |
Hexane (C6H14)(C6H14) |
Carbon tetrachloride (CCl4)(CCl4) |
Part H
State the kind of intermolecular forces that would occur between the solute and solvent in sodium nitrate (ionic).
Check all that apply.
dipole-dipole |
hydrogen bonding |
dispersion |
ion-dipole |
In: Chemistry