Question

In: Computer Science

Chapter 9 (Pointers) - Review Questions Find the Error Each of the following definitions and program...

Chapter 9 (Pointers) - Review Questions

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

46)should be int *ptr=nullptr(NULL)

47)

int x, *ptr = nullptr;

&x = ptr;

should be ptr=&x;

48)same as above

ptr=&x

49)should be *ptr=100 (ptr assigns the address *ptr has value)

50)It will print 13

it should be like *(numbers+3) this indicates the element

51)If you are assigning a value it should be *iptr=2

52)You cannot assign address to normal variable it shoud be a pointer int *fptr=&level

53)int ivalue should be declared first after words pointer should be declared

54)The parameter should be int *val

55)should be

pint = new int;

56)if it is a null pointer we cannot allocate value so if and else statements inside them should be vice versa

57)should be pint = new int(100); paranthesis not square brackets

58)should return value not an address (return wholenum)

59)should be const int *ptr = arr;

invalid conversion from const int* to int*

60)we canot assign it to constant value

const int localArray[]={1,2,3]

comment if any doubts


Related Solutions

Chapter 9 (Pointers) – Review Questions Short Answer 1.     What does the indirection operator do? 2.    ...
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...
What is invalid about each of the following program? Fixed the error once you find it....
What is invalid about each of the following program? Fixed the error once you find it. (You may find more than one errors in the code.) Question #include <iostream> #include <iomanip> using namespace std; class B { protected: virtual void func1() { cout << "base:" << endl; } }; class D1 : public B { void func1() { cout << "D2:" << endl; } }; class D2 : public B {}; int main() { B Bobj; D1 Dobj; B* p...
Review the definitions of leadership, entrepreneurship, and strategy in section 1.3 of chapter one in the...
Review the definitions of leadership, entrepreneurship, and strategy in section 1.3 of chapter one in the text. Using the internet, find different definitions for the following terms: leadership, entrepreneurship, and strategy. That’s a total of three definitions, one for each term. Next, compare and contrast the definition you found with the one in the textbook. What similarities did you find in the definitions? What are some differences? Lastly, please discuss the important similarities and differences you found between the 3...
Review the definitions of leadership, entrepreneurship, and strategy in section 1.3 of chapter one in the...
Review the definitions of leadership, entrepreneurship, and strategy in section 1.3 of chapter one in the text. Using the internet, find different definitions for the following terms: leadership, entrepreneurship, and strategy. That’s a total of three definitions, one for each term. Next, compare and contrast the definition you found with the one in the textbook. What similarities did you find in the definitions? What are some differences? Lastly, please discuss the important similarities and differences you found between the 3...
Chapter 6.1 review questions, #23 part B. In order to find the probability of a fourth...
Chapter 6.1 review questions, #23 part B. In order to find the probability of a fourth or fifth live birth, I have to multiple the probability of a fourth birth and the probability of a fifth birth. This would be 0.096 x 0.047. My calculator is saying the answer is 0.004512. However, Chegg textbook solutions and the book says the answer is 0.143, which would be the two probabilities being added together. I'm not sure which is wrong; is the...
Using Buck Chapter 17 “Cardiovascular System”; review the objectives, key terms and chapter information and answer the following review questions.
Assignment - Theory "Cardiovascular System"Using Buck Chapter 17 “Cardiovascular System”; review the objectives, key terms and chapter information and answer the following review questions. Copy and Paste your assignment on a separate MS word document or compatible file, and submit your completed assignment electronically via Moodle prior to the due date.All rules of grammar, punctuation and spelling must be observed. Please highlight your answers.The term that describes the procedure in which the surgeon withdraws fluid from the pericardial space by...
Matrix Calculator Goals Write, compile and test a matrix calculator program Review the concept of pointers,...
Matrix Calculator Goals Write, compile and test a matrix calculator program Review the concept of pointers, and multi-dimensional dynamic arrays Create a simple makefile Write a robust input validation. The purpose of this lab is to get familiar with the software environment we will be using this term, and review some C++ concepts to get prepared for future projects and labs. This lab might be very time-consuming since there are a lot of concepts you need to pick up. For...
My java program will not compile. I receive the following error; Test.java:9: error: incompatible types: int[]...
My java program will not compile. I receive the following error; Test.java:9: error: incompatible types: int[] cannot be converted to int int size = new int [start]; //Java Program import java.util.Scanner; public class Test{ public static void main(String []args) { int start, num; System.out.println("Enter the number of elements in a array : "); start = STDIN_SCANNER.nextInt(); int size = new int [start]; System.out.println("Enter the elements of array where last element must be '0' : "); for(int i = 0; i...
Review the four financial statements discussed in Chapter 1. Then answer the following questions in your...
Review the four financial statements discussed in Chapter 1. Then answer the following questions in your initial response: Choose one of the financial statements you reviewed and explain its purpose. Explain what type of information can be derived from the financial statement you chose. What do you think is the major purpose of all financial statements? What is the importance of accounting in a health care manager’s role? How will you use it in your health care career? Word Count:...
Complete the following review questions from Chapter 10 of the textbook: Explain the benefits for organizations...
Complete the following review questions from Chapter 10 of the textbook: Explain the benefits for organizations of implementing a well-defined and structured change control management process. Plagiarism You are expected to write primarily in your own voice, using paraphrase, summary, and synthesis techniques when integrating information from class and outside sources. Use an author’s exact words only when the language is especially vivid, unique, or needed for technical accuracy. Failure to do so may result in charges of Academic Dishonesty....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT