Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

3-D Arrays in C++

Posted on 2004-10-22
3
Medium Priority
?
221 Views
Last Modified: 2010-04-01
I'm trying to recall how to build a 3-D array in C++.  I know that 2-D arrays can be declared as int myArray[#rows][#cols] but if I remember correctly, doesn't a 3-D have to be constructed with new int** filling each single dimension array then adding the next dimension or layer on top of it?  Along those same lines, after the array is constructed, what is the order or dimension that the brackets refer to?  ie.  myArray[#deep][#rows][#cols] ?? isn't that the most widely accepted abstracted view?  Elaborating a little bit more, to calculate the address of an element in integer myArray[2][3][4] in myArray with the following dimensions myArray[5][6][7]  the calculation would be something like this address = base + ((2*6+4) * 7 + 3) * (sizeof int)
0
Comment
Question by:VBStudent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Assisted Solution

by:HendrikTYR
HendrikTYR earned 260 total points
ID: 12387484
1. You may still declare it as:
int myArray[5][5][5];

2. You may call the dimensions whatever you want, ie. rows, colls, deep or year, month, day etc.
  you reference the elements in the same way it was declared.  int myArray[1st-dim][2nd-dim][3rd-dim];

3. You don't have to "calculate" the address of course, just use &(myArray[2][3][4]);
...but, in your example it would be:
address = base + ((2 * (6 + 7)) + (3 * 6) + 4) * sizeof(int);

Regards
Hendrik
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 260 total points
ID: 12388245
Have a look to a previous question where I implement a 3D class, you will notice similar calculations:
http://www.experts-exchange.com/Q_21021623.html
0
 

Author Comment

by:VBStudent
ID: 12389932
Thanks for the help and the prompt response.  I actually needed both solutions.  Thanks to both of you.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question