Discussion:
Function to get unique ordinal numbers within a multi-dimensional array core-dumping
(too old to reply)
Generic Usenet Account
2007-05-17 23:41:30 UTC
Permalink
I have this simple program that maps elements with multiple indices,
within an n-dimensional space, into a unique number based on their
location within the multi-dimensional array. For some reason this
program is dumping core. For the life in me, I cannot figure out.
why Any help will be appreciated. Source code follows:


-- Gus

////////////////////////////////////////////////////////////////////

#include <iostream>
#include <iterator>
#include <string>
#include <list>

using namespace std;


#define NUM_INDICES 3

#define HI_INDEX NUM_INDICES-1
#define LO_INDEX 0

int maxIdxVal[NUM_INDICES] = {2, 3, 4};

bool
print_indices(size_t value)
{
// if(value >= (2 * 3 * 4)) return false;

list<int> indices;

size_t residue = value;

for(int cur_idx = HI_INDEX; cur_idx >= LO_INDEX; cur_idx--)
{
size_t idxVal;

idxVal = residue % maxIdxVal[cur_idx];
residue /= maxIdxVal[cur_idx];
indices.push_front(idxVal);
}

copy (indices.begin(), indices.end(), ostream_iterator<int> (cout,
"\t"));
cout << endl;
}

int
main()
{
print_indices(14);
}
p***@yahoo.com
2007-05-18 00:17:33 UTC
Permalink
Post by Generic Usenet Account
I have this simple program that maps elements with multiple indices,
within an n-dimensional space, into a unique number based on their
location within the multi-dimensional array. For some reason this
program is dumping core. For the life in me, I cannot figure out.
-- Gus
I suspect that you are crossing the boundary conditions. I suggest
uncommenting the line
// if(value >= (2 * 3 * 4)) return false;

Better still, throw an exception here. Hope that helps!
--Philos
p***@yahoo.com
2007-05-18 14:10:48 UTC
Permalink
Post by Generic Usenet Account
I have this simple program that maps elements with multiple indices,
within an n-dimensional space, into a unique number based on their
location within the multi-dimensional array. For some reason this
program is dumping core. For the life in me, I cannot figure out.
-- Gus
There is an alternate algorithm that may be employed. That directly
follows from the fact that the "ordinal value" in an n-dimensional
space can be obtained as follows:

OrdVal = Dim_1_val*MAXDIM_1 + Dim_2_val*MAXDIM_2 + Dim_3_val*MAXDIM_3
+ ........ +Dim_N-1_val*MAXDIM_N-1 + Dim_N_val

I leave the implementation of that to you as an exercise ;-)

Perhaps you will not have any issues with the alternate implementation

Philos

Loading...