Computer Science Canada

arrays

Author:  AsianSensation [ Sun Apr 04, 2004 9:18 pm ]
Post subject:  arrays

it seems that I can't declare an array with a variable as it's upper bound. Even if I declare the variable to be a constant, it still won't let me declare the array.

code:
int boundry = 7;
string name [boundry];


code:
const int boundry = 7;
string name [boundry];


both of the above doesn't seem to work. So, is there an easy way to declare an array with unknown boundry? Right now I'm using linked lists, just wondering if there is an easier way.

Author:  Tony [ Sun Apr 04, 2004 9:22 pm ]
Post subject: 

I'm preaty sure that I've read that in C++ array size must be decleared at compile time, so you can't use variables Confused

Author:  AsianSensation [ Sun Apr 04, 2004 9:23 pm ]
Post subject: 

so I'm stuck with linked lists? that's not remotely cool. I really don't want to make bunch of recursive functions for a small task like this....

Author:  Catalyst [ Sun Apr 04, 2004 9:44 pm ]
Post subject: 

code:

float *array;
int sizeOfArray=12;

array=new float[sizeOfArray];

// Do Something

delete [] array;


Author:  AsianSensation [ Sun Apr 04, 2004 10:07 pm ]
Post subject: 

oh, cool, thanks Catalyst.

Author:  Tony [ Sun Apr 04, 2004 10:50 pm ]
Post subject: 

ohh Surprised flexable arrays Very Happy noted Wink

Author:  wtd [ Wed Apr 07, 2004 12:00 am ]
Post subject: 

Well, they're sort of flexible. You can't resize them after initializing.

If you need something resizeable, you can use the vector class that's already been written.

code:
#include <vector>

int main()
{
   std::vector<int> my_vector(boundary);

   // Then, to add something onto the end...
   my_vector.push_back(42);
}

Author:  Andy [ Mon Apr 12, 2004 3:30 pm ]
Post subject: 

cant u just run a for loop through the subsets after the upperbound and declare them to null?

Author:  bugzpodder [ Wed Apr 14, 2004 7:06 pm ]
Post subject: 

Quote:
Even if I declare the variable to be a constant, it still won't let me declare the array.

sure it would. except what you did isnt technically a constant

try something like const int MAXSIZE=1000 or #define MAXSIZE 1000

Author:  wtd [ Wed Apr 14, 2004 7:11 pm ]
Post subject: 

bugzpodder wrote:
try something like const int MAXSIZE=1000 or #define MAXSIZE 1000


The former is the preferred method.


: