help with multidimential arrays in c++

Topplayer

Senior member
Jan 11, 2006
444
0
0
Thanks for looking

hopefuly I make sense when i ask this question.
I have a program that uses a dynamic multidimential array.
The reason it is dynamic is because the user has to be able to input the dimensions.
I have to make functions that go threw they array but not knowing the about the amount of rows or columns before hand is messing me up.

I was wondering if there was a function that could give me the amount of rows and/or columns in a multidimential array



any help would make my life easer.



Thanks in advance
Jonny Blaze
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,591
5
0
If you have to allocate the arrays dynamically - then also retain the values that you use to size the arrays in the first place.
 

erwos

Diamond Member
Apr 7, 2005
4,778
0
76
Originally posted by: Common Courtesy
If you have to allocate the arrays dynamically - then also retain the values that you use to size the arrays in the first place.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
In C++ memory is referred to by an address, and it is the type of the thing at the address that tells the compiler how much memory is at that address, and how to interpret it. Since you don't know anything more than the type of a single element, there is nothing to tell the compiler how much memory each pointer addresses.

So, either keep the array bounds around as the other replies suggested, or place a signal value at the end of each row, and have an empty row in the last position containing just the signal value. If you can't set aside a signal value then you have to keep the bounds around (which you should do anyway, in any situation where you can).
 

drebo

Diamond Member
Feb 24, 2006
7,035
1
81
It sounds to me like a linked list would be a far more efficient and simple data structure for what you're looking to accomplish.
 

exdeath

Lifer
Jan 29, 2004
13,679
10
81
Originally posted by: drebo
It sounds to me like a linked list would be a far more efficient and simple data structure for what you're looking to accomplish.

+1.

Linked list of inherited (or composition objects) with varying sizes, but with a common interface (or container) would be much better.

But if you have to use an array (assignment maybe?) then you just keep the dimensions requested by the user. Then also use them for bounds checking (requested_x < specified_x) if the user attempts to access something larger than the size array he initially requested.

Edit: nm on the linked list, misunderstood thinking the elements would be variably sized, but it's only the array itself whose size is determined by user dimensions at run time. You'll need to save the number of dimensions, as well as allocate space to store value of each dimension size.

input num dimensions desired -> n;

dims = new int [n];

loop on n to input size of each dimension -> dims[n];

numbytes = loop on n and accumulate the product of all dim[n] (dim[0] * dim[1] ... dim[n-1)]

array = new byte[numbytes]

Edit2: if by multidimensional array you mean a 2D array, then you just do:

input -> dim_x, dim_y;

array = new int[dim_y][dim_y];

Then walk the array with something like

for i = 0 to dim_x-1 where dim_x, and dim_y are valid and constant for the life of they array they were used to create.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |