These are two simple programs. In first program, i used static array, while in second program i used dynamic array. Both programs do the same job i.e. initiaze the array by asking the user to enter the their test scores.
The problem is:
1)i have read in many books that we can't use static array if we do not know the exact size of the array. instead of this we just use dynamic array. but in both these programs we do not know the size of the array.
** according to above statement my first program should not run but it runs correcly.
so i want to know what is the difference these two programs as they do the same job.
// static array
#include<iostream>
using namespace std;
int main()
{
int size;
cout<<"enter number of students"<<endl;
cin>> size;
int array [size];
for(int i =0; i< size; i++)
{
cout<< "enter marks of "<< (i+1) << " student: ";
cin >> array [i];
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int size;
int * ptr;
cout<<"enter number of students"<<endl;
cin>> size;
ptr =new int [size];
for(int i = 0; i< size; i++)
{
cout<< "enter marks of "<< (i+1) << " student: ";
cin >> ptr [i];
}
delete [] ptr;
ptr = 0;
return 0;
}