WHY VECTOR? The better approach over an array is the vector . This may sound bizarre to the newbies, learning c++. Let me ask you a question before we walk through the code. Let us take int *arr=new int[n]; this creates n integer space holders in heap, pretty intuitive right. Assume that we have filled up the integer space holders with some integer values and we pass this array arr to a function. Let the function be auto fun(int a[]) //what is a here, well your quick answer is it's an integer array X---you are wrong buddy. It is an integer pointer.It's size depends on the architecture. auto fun(int a[]) {int n=sizeof(a)/sizeof(a[0]);/*this gives you different answer in different computers based on the its architecture, whether it's a 16 or 32 or 64 bit*/ } Because we are actually calculating the size of integer pointer. sizeof(int *)/sizeof(int); To understand this better see this post To avoid all these in-deliberate