Hi.
I want to declare an array of integer, but I want the program to take input from user on how many rows the array should hold. How can I do this?
I've tried:
int nCities;
cin>>nCities;
int cities[nCities][nCities];
but didn't work.
4 replies to this topic
#1
Posted 03 October 2010 - 05:08 AM
|
|
|
#2
Posted 03 October 2010 - 05:23 AM
int nCities; cin >> nCities; int** cities; cities = new int* [nCities]; for (int i = 0; i < nCities; ++i) cities[i] = new int[nCities]; ... for (int i = 0; i < nCities; ++i) delete cities[i]; delete [] cities;
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#3
Posted 03 October 2010 - 07:02 AM
>>int cities[nCities][nCities];
I think that will be allowed in the next version of c++ standards. But the compilers will probably take about 4 to 5 years to implement it. I have not tried it yet but I think the newest version of g++ supports it.
I think that will be allowed in the next version of c++ standards. But the compilers will probably take about 4 to 5 years to implement it. I have not tried it yet but I think the newest version of g++ supports it.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
#4
Posted 03 October 2010 - 11:22 PM
So what does this do exactly? I'm a little confused
int** cities ---> cities is a pointer to a pointer to an integer?
cities = new int* [nCities] ---> what cities points to is an array of pointers with nCities elements ??
for (int i = 0; i < nCities; ++i)
cities[i] = new int[nCities]; ---> hmm ?
int** cities ---> cities is a pointer to a pointer to an integer?
cities = new int* [nCities] ---> what cities points to is an array of pointers with nCities elements ??
for (int i = 0; i < nCities; ++i)
cities[i] = new int[nCities]; ---> hmm ?
#5
Posted 04 October 2010 - 04:15 AM
kristoffera said:
So what does this do exactly? I'm a little confused
int** cities ---> cities is a pointer to a pointer to an integer?
cities = new int* [nCities] ---> what cities points to is an array of pointers with nCities elements ??
for (int i = 0; i < nCities; ++i)
cities = new int[nCities]; ---> hmm ?
int** cities ---> cities is a pointer to a pointer to an integer?
cities = new int* [nCities] ---> what cities points to is an array of pointers with nCities elements ??
for (int i = 0; i < nCities; ++i)
cities = new int[nCities]; ---> hmm ?
A conclusion is where you got tired of thinking.
#define class struct // All is public.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









