Create a dynamic array of 100 integer values named myNums. Use a pointer variable (like ptr) which points to this array. Use this pointer variable to initialize the myNums array from 2 to 200 and then display the array elements. Delete the dynamic array myNums at the end. You just need to write part of the program.

Respuesta :

Answer:

The required part of the program in C++ is as follows:

int *ptr;

int *myNums = new int(100);

srand (time(NULL));

for (int i = 0; i < 100; i++) {

 myNums[i] = rand() % 200 + 2; }

ptr = myNums;

cout << "Output: ";

for (int i = 0; i < 100; i++) {

 cout <<*(ptr + i) << " "; }

delete[] myNums;

Explanation:

This declares a pointer variable

int *ptr;

This declares the dynamic array myNums

int *myNums = new int(100);

This lets the program generate different random numbers

srand (time(NULL));

This iterates through the array

for (int i = 0; i < 100; i++) {

This generates an integer between 2 and 200 (inclusive) for each array element

 myNums[i] = rand() % 200 + 2; }

This stores the address of first myNums in the pointer

ptr = myNums;

This prints the header "Output"

cout << "Output: ";

This iterates through the pointer

for (int i = 0; i < 100; i++) {

Print each element of the array, followed by space

 cout <<*(ptr + i) << " "; }

This deletes the dynamic array

delete[] myNums;

ACCESS MORE