home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1779
Discussions: 188,401, Posts: 2,243,601, Members: 232,629
Old August 7th, 2004, 01:56 AM   Digg it!   #1 (permalink)
Ultimate Member
 
lost-and-found's Avatar
 
Join Date: Oct 2001
Location: Illinois
Posts: 2,959
Send a message via AIM to lost-and-found
c++ question

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?
__________________

lost-and-found is offline   Reply With Quote
Old August 7th, 2004, 02:01 AM     #2 (permalink)
Kawaru wa yo!
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 16,137
Blog Entries: 7
Isn't it just

int *array=new int[size1][size2]; ?

Whir is online now   Reply With Quote
Old August 7th, 2004, 02:01 AM     #3 (permalink)
Ultimate Member
 
lost-and-found's Avatar
 
Join Date: Oct 2001
Location: Illinois
Posts: 2,959
Send a message via AIM to lost-and-found
nope

lost-and-found is offline   Reply With Quote
Old August 7th, 2004, 02:07 AM     #4 (permalink)
Kawaru wa yo!
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 16,137
Blog Entries: 7
Whir is online now   Reply With Quote
Old August 7th, 2004, 02:09 AM     #5 (permalink)
Ultimate Member
 
FatalException's Avatar
 
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).
FatalException is offline   Reply With Quote
Old August 7th, 2004, 02:11 AM     #6 (permalink)
Ultimate Member
 
FatalException's Avatar
 
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.
FatalException is offline   Reply With Quote
Old August 7th, 2004, 02:31 AM     #7 (permalink)
Kawaru wa yo!
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 16,137
Blog Entries: 7
It's a starting point.

No need to be derisive about it, right?
Whir is online now   Reply With Quote
Old August 7th, 2004, 02:36 AM     #8 (permalink)
Ultimate Member
 
FatalException's Avatar
 
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.
FatalException is offline   Reply With Quote
Old August 7th, 2004, 02:42 AM     #9 (permalink)
Kawaru wa yo!
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 16,137
Blog Entries: 7
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]
Whir is online now   Reply With Quote
Old August 7th, 2004, 02:44 AM     #10 (permalink)
Ultimate Member
 
FatalException's Avatar
 
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).
FatalException is offline   Reply With Quote
Reply


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

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
If you ever are bored and want a little project.... Tekk IMO Community 14 July 3rd, 2004 01:31 AM
Photoshop elements question mad1 Graphic Design and Digital Photography 6 December 29th, 2003 11:11 AM
kinda stupid question, but oh well :) whats low level format ? ZL1 General Tech Discussion 22 November 10th, 2003 10:14 PM
Tech-Support Seekers Turn To the Web Mike Tech News Discussion 3 January 9th, 2002 01:21 AM

Most Active Discussions
Is It Just Me? (2906)
Unarmed man on his stomach shot by .. (6)
Misery Loves Company... (2144)
New Build ( Finally ) (6)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
Folderchat Weekday thread (444)
Upgrading RAM (6)
Recent Discussions
How do you move a hard-drive to.. (4)
Laptop proccesor to desktop mob.. (1)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
For cheap price and good qualit.. (1)
Left 4 Dead Small Freezes (3)
RCA 52Inch HDTV wont turn on (4)
wishin i could edit my aol prof.. (0)
Sporadic internet connectivity (2)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 03:44 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28