1. Engineering
  2. Computer Science
  3. c...

Question: c...

Question details


Dynamic variables can also be arrays. Dynamic arrays are much more flexible than static arrays, because it is not necessary to know their size before running the program. The size of the array can be declared at runtime. Dynamic arrays use pointers to store their address, just like dynamic variables double *myarr int size; cin >> size; myarr = new double [size]; You can then use myarr just as a regular array (you dont even need the to access its content-more on this in class). Just remember to delete the array when you are no longer using it, especially since arrays are much better at taking up space in the heap than single variables delete [] myarri Now, write a program that asks a user to specify how many numbers he wishes to write, then stores the values input by the user into a dynamic array. How many numbers will you enter? 4 1 2 34 Finally, it prints the array. The array you entered is 1 23 4 Remember to deallocate memory at the end of the program.main.cpp Load default template... 1 #include <iostream» 2 using namespace std; 4int main O 6 int size,i; 7 int p; 9/Acquire array size from keyboard 10 12 13 14 //Create new dynamic array and store its address in p 15 if CpNULL) 16cout << Error: memory could not be allocated 17 else 18

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution