giving length to array by user?

Is there any other way (except link-list) to give length of array as a input by user?
You can allocate dynamically memory for an array by using new or malloc functions. Or you can use standard container std::vector
Last edited on
hmmm..thanks
Though if you use C then you can specify the size of an array on a flight.
i have done through this way(below) as a example to check is it working or not...still its going good..may be it is not good approach i am new in c/c++..

#include<iostream>
using namespace std;
int *ptr;
int i=0;
int x=0;
void main()
{
cout<<"Plesae enter the length of array";
cin>>i;
ptr=(int*) calloc (i,sizeof(int));



for(int j=0;j<i;j++)
{
x+=2;
*(ptr+j)=x;
//cout<<x;
cout<<*(ptr+j);
}


}
@atif1512
A better approach is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    cout << "Please enter the length of the array: ";
    int i;
    cin >> i;
    vector<int> v(i);

    for(size_t j=0; j < v.size(); ++j)
    {
        v[j] = 2*j;
        cout << v[j] << ' ' ;
    }
    cout << '\n';
}

online demo: http://ideone.com/aDFDL

i am new in c/c++.

There is no such thing as "c/c++". If you are new to C++, learn about vectors.
ok.thanks i will.. study about vectors..
Topic archived. No new replies allowed.