Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 10-06-2006, 10:16 AM   #1 (permalink)
Forum Newbie
 
poorchava's Avatar
 
Join Date: May 2006
Location: Wroclaw, Poland
Age: 21
Posts: 63
Send a message via MSN to poorchava
Default Question about array initialization

I'm currently writing a program, that needs a two-dimensional array on float values. How do initialize a 2-dimensional array without specifying it's exact size?
poorchava is offline   Reply With Quote
Old 10-06-2006, 10:57 AM   #2 (permalink)
Forum Expert
 
Courageous's Avatar
 
Join Date: Nov 2005
Location: San Diego, CA
Posts: 1,824
Default

You can declare it without specifying its exact size, but you cannot "initialize" an array without doing so. For that, you'll need some other data structure. What are you trying to do?
Courageous is offline   Reply With Quote
Old 10-07-2006, 04:06 PM   #3 (permalink)
Forum Expert
 
Join Date: Sep 2002
Age: 23
Posts: 1,472
Default

int dimension1 = 100;
int dimension2 = 550;
float[,] myArray = (float[,])Array.CreateInstance(typeof(float), dimension1, dimension2);
Ravatar is offline   Reply With Quote
Old 10-07-2006, 07:11 PM   #4 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Or, put another way that isn't the slower way of doing it:
Code:
Int32 size1 = 250
    , size2 = 100;

Int32 [,] array = new Int32[size1, size2]
Note, that I'm not saying it's a bad way, just a slower way that doesn't take into account what the C# compiler intrinsically knows about.

Also, I'm not so sure that's what the OP wants. I'm thinking it's something along the lines of:
Code:
Int32 lowerSize = 100;
Int32 [,] array = new Int32[,size];
That is, leaving the upper bounds undetermined, which, as Courageous said, cannot be done using native arrays but can be done with other data structures.

There's something along the lines of what he's talking about in C++, but it doesn't really have to do with array initialization (still it's the first thing I thought of when reading his post):
Code:
void f(int a[][10])
{}

int main()
{
    int a[1][10];
    f(a);
}
But that's really just another way of saying "int (*a)[10]".

Of course, I could be wrong as well, just have to wait until they answer Courageous' question.
Sep102 is offline   Reply With Quote
Old 10-08-2006, 03:32 PM   #5 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

he is probably talking about pointers but as far as I can tell, you cant do it in any language. you cant initialize something if you dont know the size. you need to use dynamic arrays/data structures.
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
noobie is offline   Reply With Quote
Old 10-20-2006, 02:21 AM   #6 (permalink)
Forum Newbie
 
poorchava's Avatar
 
Join Date: May 2006
Location: Wroclaw, Poland
Age: 21
Posts: 63
Send a message via MSN to poorchava
Default

it can be easily done on PHP (so says my friend- who's a PHP freak )
poorchava is offline   Reply With Quote
Old 10-20-2006, 04:01 AM   #7 (permalink)
Forum Expert
 
Join Date: Sep 2002
Age: 23
Posts: 1,472
Default

Quote:
Originally Posted by Sep102
Or, put another way that isn't the slower way of doing it:
Code:
Int32 size1 = 250
    , size2 = 100;

Int32 [,] array = new Int32[size1, size2]
Note, that I'm not saying it's a bad way, just a slower way that doesn't take into account what the C# compiler intrinsically knows about.

Also, I'm not so sure that's what the OP wants. I'm thinking it's something along the lines of:
Code:
Int32 lowerSize = 100;
Int32 [,] array = new Int32[,size];
That is, leaving the upper bounds undetermined, which, as Courageous said, cannot be done using native arrays but can be done with other data structures.

There's something along the lines of what he's talking about in C++, but it doesn't really have to do with array initialization (still it's the first thing I thought of when reading his post):
Code:
void f(int a[][10])
{}

int main()
{
    int a[1][10];
    f(a);
}
But that's really just another way of saying "int (*a)[10]".

Of course, I could be wrong as well, just have to wait until they answer Courageous' question.
Yea.. for some reason I was under the impression you couldn't define array bounds at runtime. Guess I've been working in C++ too much recently.
Ravatar is offline   Reply With Quote
Old 10-20-2006, 04:36 AM   #8 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

however you could use pointers in C++ for that matter (arrays are basically pointers): int** myArray
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
noobie is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5