consider this program:
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "How many numbers would you like to type? ";
cin >> i;
int * p;
p= new int[i];
cout << "Enter the numbers: \n";
for (int n=0; n<i; n++)
cin >> p[n];
cout << "You have entered: ";
for (int n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
return 0;
}
and this one:
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "How many numbers would you like to type? ";
cin >> num;
int arr[num];
cout << "Enter the numbers: \n";
for (int a = 0; a <num; ++a)
cin >>arr[a];
cout << "You have entered: ";
for (int a = 0; a <num; ++a)
cout <<arr[a]<< ", ";
return 0;
}
Both programs are accomplishing the same task and -to me- the latter is a lot
easier to understand than the former. And now my question is why do we need dynamic memory allocation anyway?