Question

In: Computer Science

When answering these questions, please write them in standard English. I asked this question earlier and...

When answering these questions, please write them in standard English. I asked this question earlier and the answers were all correct, but I had a very hard time understanding what was trying to be said, if you know what I mean.

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;

       }

Solutions

Expert Solution

Hey that is a one big list of question.But anyway these are the answers of some of them.
1. The indirection operator de-references the pointer; allows you to work with the value the pointer is pointing to.

2. When a * is used with ptr while cout, it means value at. So value at ptr is 7. But when only ptr is used with cout the address store at ptr i.e of 7 will be printed.

3. Uses of *
For multiplication: int a = 2*3 = 6

For pointer definition: int *a;

For indirection: *x= 5, stores 5 at location pointed

4. Addition of integer, Subtraction of integer and Subtracting two pointers of the same type are only 3 operations allowed on pointers.

5. After multiplying 4 by the no. of bytes of an int it adds it to the memory address.

6. It is same as numbers[3] which is 8.

7. New operator are used for Dynamic Memory Allocation to set aside a chunk of memory for a specific data type.

8. Delete operator is used to de-allocate the memory of an object.

9. A pointer should only be returned from a function if it is a pointer to an object that was passed into the function.

10. Constant pointer can never change its address they are pointing to on the other hand pointer to a constant means the one which can not change the value it is pointing to.

I hope these helps.


Related Solutions

I please need assistance with questions 6-7-8-9 of this question. I previously asked but another case...
I please need assistance with questions 6-7-8-9 of this question. I previously asked but another case was answered under my question Following a strategy of product differentiation, Positive Charge Ltd. makes high quality electronic components. Positive Charge Ltd. presents the following data for the past two years relating to its AA01 product. Year 1 Year 2 Units of AA01 produced and sold 4,000 4,320 Selling price $800 $850 Direct materials (kilograms) 10,400 12,360 Direct materials costs per kilogram $60 $64...
Please write about these questions like a small paragraph answering he questions below: Do you think...
Please write about these questions like a small paragraph answering he questions below: Do you think that microbes can have cooperative traits? Why or why not? Think about the RNA viruses that evolve as if they were playing the prisoner's dilemma game. What if you did not know about the prisoner's dilemma and you were given the data on fitness at different starting frequencies in the paper presented. How would you interpret those results? Is it useful to think about...
please I don't really know how to start answering this question I really need to understand...
please I don't really know how to start answering this question I really need to understand it please show the work with a clear handwriting A collision in one dimension A mass m1 = 2 kg moving at v1i = 3 ms−1 collides with another mass m2 = 4 kg moving at v2i = −2 ms−1. After the collision the mass m1 moves at v1f = −3.66 ms−1. (a) Calculate the final velocity of the mass m2. (b) After the...
Please I want answers for these questions. Thanks Note: I want them by typing not by...
Please I want answers for these questions. Thanks Note: I want them by typing not by hand writing Q2 Explain why a market structure in which money is used as a medium of exchange is more conducive to the expansion of trade and exchange than a barter system. What is money? Explain the three functions that money performs. What is the relationship between money and inflation? Explain
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can...
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can explain why in terms of concentration gradient and the fact it's in a hypotonice solution which would cause water to go into the blood vessel, but don't know how to answer it in terms of hydrostatic pressure. 6. Imagine blood in a vessel that has an osmolarity of 300 mEq (a measure of the concentration of particles). Now, imagine that the IF around the...
Write several paragraphs to provide more in-depth analysis and consideration when answering the following questions. 1....
Write several paragraphs to provide more in-depth analysis and consideration when answering the following questions. 1. Describe an activity that would require an RFP and why it should be handled with an RFP instead of a RFQ or a partnership. 2. Describe the procurement task. Rate it for the amount of uncertainty of the scope, cost, schedule, and risk. Choose a contract type and explain why that contract type would be an appropriate choice for that set of uncertainties. 3....
Please watch the movie Contagion; write a one-page paper by answering the following questions. 1. What...
Please watch the movie Contagion; write a one-page paper by answering the following questions. 1. What have you learned by watching this movie? 2. How does this movie compared with our current pandemic, ( COVID 19)? 3. Did watching this movie change your perspective on Public Health?
Attention: I had earlier asked the same but the solution was not clear,Kindly show all the...
Attention: I had earlier asked the same but the solution was not clear,Kindly show all the workings explicitly. 1) Your friend grabs a die at random from a drawer containing two 6 -sided dice,one 8-sided,and one 12-sided die. She rolls the die once and reports that the result is 7. a) Make a discrete Bayes table showing the prior ,likelihood,and posterior for the type of die rolled given the data. b) What are your posterior odds that the die has...
Question: How does Professor Damodaran define Intrinsic Valuation? Before answering these questions please watch the 3...
Question: How does Professor Damodaran define Intrinsic Valuation? Before answering these questions please watch the 3 Damodaran Videos and answer the following question. Intrinsic Valuation
Write a performance standard sentence for answering the telephone at a Sobey's grocery store.
Write a performance standard sentence for answering the telephone at a Sobey's grocery store.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT