Answer to Question #350967 in C++ for BScInf

Question #350967

Write C++ statements to do the following: (7)

(i) Define a pointer type int_ptr for pointer variables that contain pointers to int variables. (ii) Declare p2 to be a pointer to an int.

(iii) Obtain an integer value nrElements from the user indicating the number of elements to allocate.

(iv) Dynamically allocate an array of nrElements integers and store its address in p2.

(v) Declare an int array a with 500 elements.

(vi) Assume p2 has been initialized and copy the elements of p2 one by one to the corresponding elements in a.

(vii) Free the memory allocated to the variable that p2 is pointing to.


1
Expert's answer
2022-06-15T17:24:15-0400
#include <iostream>

using namespace std;

int main()
{
	//1)Define a pointer type int_ptr for pointer variables that contain pointers to int variables.
	int **int_ptr;
	//2)Declare p2 to be a pointer to an int
	int* p2;
	//3)Obtain an integer value nrElements from the user indicating the number of elements to allocate.
	int nrElements;
	cout << "Please, emter the number of elements to allocate: ";
	cin>> nrElements;
	//4)Dynamically allocate an array of nrElements integers and store its address in p2.
	p2 = new int[nrElements];
	//5) Declare an int array a with 500 elements.
	int a[500];
	//6)Assume p2 has been initialized and copy the elements of p2 one by one to the corresponding elements in a.
	for (int i = 0; i < nrElements; i++)
	{
		a[i] = p2[i];
	}
	//7)Free the memory allocated to the variable that p2 is pointing to.
	delete p2;
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS