August 7th, 2004, 01:56 AM
|
#1 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Illinois
Posts: 2,959
|
hope this is the right place for this, cause I have not idea where the programming forum has gone.
How do I create a 2d variable size array. I wanna have the user specify the size of a matrix in the program, and from that I wanna create an array with the given size.
I know how to do that with a 1d array Code: int *array=new int[sizeinbytes]; but how do I make it work with a 2 d array? |
| |
August 7th, 2004, 02:01 AM
|
#2 (permalink)
| | Kawaru wa yo!
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 16,137
|
Isn't it just
int *array=new int[size1][size2]; ? |
| |
August 7th, 2004, 02:01 AM
|
#3 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Illinois
Posts: 2,959
| |
| |
August 7th, 2004, 02:07 AM
|
#4 (permalink)
| | Kawaru wa yo!
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 16,137
| |
| |
August 7th, 2004, 02:09 AM
|
#5 (permalink)
| | Ultimate Member
Join Date: Jun 2004 Location: Indianapolis, Indiana
Posts: 1,386
|
If you don't find an answer to this quickly and need something that is functional, you could always define a struct which contains (size1) elements of the variable type you want and then make a one dimensional array of that struct of the appropriate size using the new syntax as you demonstrated above. This would give the same effect as having a two dimensional array. You may need to use a class instead of a struct, now that I think about it, since a class is created when it is instanciated and you can pass the appropriate size to the class's constructor to make the "row" array within it the appropriate size (ie. the size entered by the user). |
| |
August 7th, 2004, 02:11 AM
|
#6 (permalink)
| | Ultimate Member
Join Date: Jun 2004 Location: Indianapolis, Indiana
Posts: 1,386
| Quote: |
Originally Posted by Whir | No, it doesn't use the "new" command anywhere in there, nor does it use a pointer to point to the array elements as this user requested. Please read the code more carefully. That tutorial explains how to define a two dimensional array of static size that uses ONLY basic array subscript notation. This is a far more advanced topic than what that tutorial covers. |
| |
August 7th, 2004, 02:31 AM
|
#7 (permalink)
| | Kawaru wa yo!
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 16,137
|
It's a starting point.
No need to be derisive about it, right? |
| |
August 7th, 2004, 02:36 AM
|
#8 (permalink)
| | Ultimate Member
Join Date: Jun 2004 Location: Indianapolis, Indiana
Posts: 1,386
|
How was what I said derisive? I merely stated that the topic we are discussing is more advanced than the topic covered by the tutorial. You were the one that fought back with the "according to... it is," comment when the original poster said your advice was incorrect. I simply stated again that your advice was once again incorrect and your source did not in any way provide support for your advice. Catching someone trying to push their idea as the correct one when it is not and pointing out their mistake to them is hardly derisive, especially after they persist and provide "evidence" to support their idea. You simply need to accept criticism of your ideas more readily and not take them as personally. |
| |
August 7th, 2004, 02:42 AM
|
#9 (permalink)
| | Kawaru wa yo!
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 16,137
|
I wasn't speaking of the fact that I was wrong. I'm wrong all the time.
He didn't mention anything about how he wanted to access the data in his original request to begin with. He wanted to know how to make multi-dimensional arrays. That's what the tut is for. Obviously you read more into it as a proficient coder. I only commented to begin with because I moved the thread and wanted to make sure it stuck.
[shrug] |
| |
August 7th, 2004, 02:44 AM
|
#10 (permalink)
| | Ultimate Member
Join Date: Jun 2004 Location: Indianapolis, Indiana
Posts: 1,386
|
int *array=new int[sizeinbytes];
That implies a pointer (int *variable_name.....) (for future reference). |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |