Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 arrays
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AsianSensation




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Apr 04, 2004 9:22 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
AsianSensation




PostPosted: Sun Apr 04, 2004 9:23 pm   Post subject: (No 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....
Catalyst




PostPosted: Sun Apr 04, 2004 9:44 pm   Post subject: (No subject)

code:

float *array;
int sizeOfArray=12;

array=new float[sizeOfArray];

// Do Something

delete [] array;

AsianSensation




PostPosted: Sun Apr 04, 2004 10:07 pm   Post subject: (No subject)

oh, cool, thanks Catalyst.
Tony




PostPosted: Sun Apr 04, 2004 10:50 pm   Post subject: (No subject)

ohh Surprised flexable arrays Very Happy noted Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




PostPosted: Wed Apr 07, 2004 12:00 am   Post subject: (No 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);
}
Andy




PostPosted: Mon Apr 12, 2004 3:30 pm   Post subject: (No subject)

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




PostPosted: Wed Apr 14, 2004 7:06 pm   Post subject: (No 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
wtd




PostPosted: Wed Apr 14, 2004 7:11 pm   Post subject: (No subject)

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


The former is the preferred method.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: