473,721 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create an N dimensional array with N elements?

BCC
Ive been googling and reading through my books but I haven't figured out a
solution (much less an elegant one) to create a multidimensiona l array with
a runtime determined number of dimensions. I also checked out the
boost::multi_ar ray.hpp, and Giovanni Bavistrelli's Array code. Neither of
these seem to allow dynamic array dimensions.

For example, the user selects a 2 dimensional array, I want to create:
MyObject** array = new MyObject[dim1_size][dim2_size];

And if they select a 4 dim array:
MyObject**** array = new
MyObject[dim1_size][dim2_size][dim3_size][dim4_size];

Besides using bigugly if statements, how do I do this?

Thanks,
Bryan
Jul 22 '05 #1
10 3202
BCC
Upon further reflection, I think creating an n dimensional array may be
besides the point...

What I am trying to do, at the end of the day, is to take a list of objects
(lets just say they are ints for the moment though) and multiply it by
itself N number of times.

So if I have an array with 10 elements in it, I need to create a new list
with 100 elements in it, and populate those 100 elements with the results
of:
[0][0] * [0][0]
[0][0] * [0][1]
etc. etc.

With 3 dims, the list would be 10000 elements long, and so on.

If this was a nested for loop, with N number of nests this would be
simple... but without knowing ahead of time how many loops to use Im not
sure how to go about this.

Any suggestions?
Thanks
B
Jul 22 '05 #2

"BCC" <br***@akanta.c om> wrote in message
news:zI******** *********@newss vr25.news.prodi gy.com...
Upon further reflection, I think creating an n dimensional array may be
besides the point...

What I am trying to do, at the end of the day, is to take a list of objects (lets just say they are ints for the moment though) and multiply it by
itself N number of times.

So if I have an array with 10 elements in it, I need to create a new list
with 100 elements in it, and populate those 100 elements with the results
of:
[0][0] * [0][0]
[0][0] * [0][1]
etc. etc.

With 3 dims, the list would be 10000 elements long, and so on.

If this was a nested for loop, with N number of nests this would be
simple... but without knowing ahead of time how many loops to use Im not
sure how to go about this.

Any suggestions?


Recursion.

Not completely sure this is what you are asking for but hopefully will be
close enough for you to adapt.

#include <algorithm>
#include <iterator>
#include <iostream>
#include <list>
using namespace std;

void generate(std::l ist<int>& res, std::list<int> const& l, int depth, int
value = 1)
{
if (depth == 0)
{
res.push_back(v alue);
}
else
{
for (std::list<int> ::const_iterato r i = l.begin(); i != l.end(); ++i)
generate(res, l, depth - 1, value*(*i));
}
}

int main()
{
// setup starting list
std::list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
// generate result list
std::list<int> res;
generate(res, l, 4);
// print results
std::copy(res.b egin(), res.end(), std::ostream_it erator<int>(std ::cout, "
"));
}

john
Jul 22 '05 #3
BCC wrote:
Upon further reflection, I think creating an n dimensional array may be
besides the point...

What I am trying to do, at the end of the day, is to take a list of objects
(lets just say they are ints for the moment though) and multiply it by
itself N number of times.

So if I have an array with 10 elements in it, I need to create a new list
with 100 elements in it, and populate those 100 elements with the results
of:
[0][0] * [0][0]
[0][0] * [0][1]
etc. etc.

With 3 dims, the list would be 10000 elements long, and so on.

If this was a nested for loop, with N number of nests this would be
simple... but without knowing ahead of time how many loops to use Im not
sure how to go about this.

Any suggestions?


Recursion.
Jul 22 '05 #4
BCC
Hi John, thanks very much for the suggestion- you were pretty close to what
I need, but it is still tripping me up. Heres what I have:

int depth = 2;
int list_size = 5;
int* array = new int[list_size];
for (int i = 0; i < list_size; ++i) array[i] = 0;
vector<int *> all_list;

Generate(all_li st, list_size, depth, array);

// Process array

delete [] array;

void Generate(std::v ector<int *>& res, int list_size, int depth, int*
in_list)
{
if (depth == 0)
{
res.push_back(i n_list);
}
else {
for (int i = 0; i < list_size; ++i) {
if (in_list[i] > 0) continue; // We have already included this seq,
skip
++in_list[i];
Generate(res, list_size, depth - 1, in_list);
}
}
}

What I need it to have is in my resulting list is list_size^depth patterns,
or in this case all unique possible combinations of 1's and 2's:
for 1's
10000
01000
00100
00010
00001
-And-
for 2's
11000
10100
10010
10001
01100
01010
01001
00110
00101
00011

I believe this is all of them. And I need to be able to do this for any
size list and any depth...

I think the function is pretty close, but I am still not getting it. What
am I missing? (hopefully this makes sense!)

Thanks,
Bryan
Jul 22 '05 #5
"BCC" <br***@akanta.c om> wrote in message
news:it******** *******@newssvr 27.news.prodigy .com...
Hi John, thanks very much for the suggestion- you were pretty close to what I need, but it is still tripping me up. Heres what I have:

int depth = 2;
int list_size = 5;
int* array = new int[list_size];
for (int i = 0; i < list_size; ++i) array[i] = 0;
vector<int *> all_list;

Generate(all_li st, list_size, depth, array);

// Process array

delete [] array;

void Generate(std::v ector<int *>& res, int list_size, int depth, int*
in_list)
{
if (depth == 0)
{
res.push_back(i n_list);
}
else {
for (int i = 0; i < list_size; ++i) {
if (in_list[i] > 0) continue; // We have already included this seq,
skip
++in_list[i];
Generate(res, list_size, depth - 1, in_list);
}
}
}

What I need it to have is in my resulting list is list_size^depth patterns, or in this case all unique possible combinations of 1's and 2's:
for 1's
10000
01000
00100
00010
00001
-And-
for 2's
11000
10100
10010
10001
01100
01010
01001
00110
00101
00011


It looks as though all you need is a combination generator, so a Combination
class could do the job (with a Next() member returns the next combination)
and you could forget about the recursion.

DW

Jul 22 '05 #6

"BCC" <br***@akanta.c om> wrote in message
news:it******** *******@newssvr 27.news.prodigy .com...
Hi John, thanks very much for the suggestion- you were pretty close to what I need, but it is still tripping me up. Heres what I have:

int depth = 2;
int list_size = 5;
int* array = new int[list_size];
for (int i = 0; i < list_size; ++i) array[i] = 0;
vector<int *> all_list;

Generate(all_li st, list_size, depth, array);

// Process array

delete [] array;

void Generate(std::v ector<int *>& res, int list_size, int depth, int*
in_list)
{
if (depth == 0)
{
res.push_back(i n_list);
}
else {
for (int i = 0; i < list_size; ++i) {
if (in_list[i] > 0) continue; // We have already included this seq,
skip
++in_list[i];
Generate(res, list_size, depth - 1, in_list);
}
}
}

What I need it to have is in my resulting list is list_size^depth patterns, or in this case all unique possible combinations of 1's and 2's:
for 1's
10000
01000
00100
00010
00001
-And-
for 2's
11000
10100
10010
10001
01100
01010
01001
00110
00101
00011

I believe this is all of them. And I need to be able to do this for any
size list and any depth...

I think the function is pretty close, but I am still not getting it. What
am I missing? (hopefully this makes sense!)


Actually no. When I read your first post I thought you were talking about
combinations, and for combinations a recusrive algorithm is normally the way
to go. But now I see that you are talking about permutations (i.e. unique
combinations) and for permutations an iterative algorithm is correct. It's
quite hard to work out the correct algorithm for permutations but
fortunately its already been done in the STL next_permutatio n function (look
at the code in the <algorithm> header if you are interested).

The other thing that worries me about your code is that you have a vector of
pointers and you are repeatedly pushing the same pointer onto your vector,
which cannot be a good idea.

Here's some code, I switched to a vector of vectors, instead of a vector of
pointers.

#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>

int main()
{
int depth = 2;
int list_size = 5;
std::vector<std ::vector<int> > all_list;
std::vector<int > array(list_size );
for (int i = 1; i <= depth; ++i)
{
// set up initial permutation
for (int j = 0; j < list_size; ++j)
array[j] = j < list_size - i ? 0 : 1;
// add all permutations of this type
do
{
all_list.push_b ack(array);
}
while (next_permutati on(array.begin( ), array.end()));
}
// print results
for (std::vector<st d::vector<int> >::const_iterat or i = all_list.begin( );
i != all_list.end(); ++i)
{
std::copy(i->begin(), i->end(), std::ostream_it erator<int>(std ::cout, "
"));
std::cout << '\n';
}
}
Jul 22 '05 #7
> >
I think the function is pretty close, but I am still not getting it. What am I missing? (hopefully this makes sense!)


Actually no.


I meant, 'no its not close', not 'no it doesn't make sense'. No offence
intended.

john
Jul 22 '05 #8
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2j******** *****@uni-berlin.de...

"BCC" <br***@akanta.c om> wrote in message
news:it******** *******@newssvr 27.news.prodigy .com...
10000
01000
00100
00010
00001
-And-
for 2's
11000
10100
10010
10001
01100
01010
01001
00110
00101
00011

I believe this is all of them. And I need to be able to do this for any
size list and any depth...

I think the function is pretty close, but I am still not getting it. What am I missing? (hopefully this makes sense!)
Actually no. When I read your first post I thought you were talking about
combinations, and for combinations a recusrive algorithm is normally the

way to go. But now I see that you are talking about permutations (i.e. unique
combinations)


Those look like combinations above to me, not permutations (i.e., order
matters for permutations but not combinations), or are you referring to some
other aspect of the problem?

DW

Jul 22 '05 #9
BCC
> Actually no. When I read your first post I thought you were talking about
combinations, and for combinations a recusrive algorithm is normally the way to go. But now I see that you are talking about permutations (i.e. unique
combinations) and for permutations an iterative algorithm is correct. It's
quite hard to work out the correct algorithm for permutations but
fortunately its already been done in the STL next_permutatio n function (look at the code in the <algorithm> header if you are interested).
Ah, that is a nice tip, I didnt know next_permutatio n existed, very useful
and I will most certainly look at the code for this.
The other thing that worries me about your code is that you have a vector of pointers and you are repeatedly pushing the same pointer onto your vector,
which cannot be a good idea.


Agreed. I was thinking in terms of speed (since STL vector is slow), but
with my list sizes I dont imagine that performance will be a problem at all.

And the code is great to get me started.

Thanks for all the help!

Bryan
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
17605
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like array_reverse(). Can anyone tell me why?
4
8181
by: Todd | last post by:
I'm new to c++ and was wondering how to sort a 2 dimensional array. I'm using a select sort for 1 dimensional arrays but it is not working for a 2 dimensional array. The 2 dimensional array are float elements. Thanks in advance
6
3353
by: TrustyTif | last post by:
After surfing the google & MSDN for a few days I found an MSDN site that explained how to "new" a multidimensional array in C++...but, I don't know how to use it, for alas, my brain still doesn't understand pointer very well. Here's the site: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/html/_pluslang_new_operator.asp And here's what I've come up with thus far to have a 2 dimensional array of CStrings.
9
8575
by: Dadi | last post by:
Hi, I can make a simple initialization work like this: Object ONE_ROW = {{"Vodafone", "5550160100197016"}}; But, now I want to create another array that consists of multiple copies of ONE_ROW like this (will not compile): Object TWO_ROWS = {ONE_ROW, ONE_ROW}; // Works in Java ;-)
4
6377
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically allocated a set of pointer to pointers as my multi-dimensional arrays. Here is my code (i have omitted checking calloc's return value to make this shorter): int **filter; filter = calloc( ylength, sizeof(int*) ); for( i = 0 ; i < ylength...
22
2284
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest that it will be evaluated like "x", just as "x" is evaluated like "x" right now.
6
10416
by: Chuck Anderson | last post by:
My knowledge of JavaScript is limited. I learn from example and then adapt those examples to suit my needs. I have stumped myself on this one. I have a form with checkboxes that I want to group by using a two dimensional array. <form name=msgs>
272
14088
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
152
9842
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
0
8834
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9365
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9127
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9056
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8002
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4483
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.