|
|
Hello. I know that a two-dimensional array can be handles as one-dimensional array by converting the indexes to a single one. Supose you have the following:
int *table;
table=new int table[3*3];
When referencig table[2][2] by a single index
index=(2*3+2);
then
table[index] can be recalled.
Now, I'm having a three-dimensional array and because is faster to handle dynamic arrays, I can define it as:
int table1;
table1=new int table[3*3*3];
but I got lost finding the index conversion formulae...
Eric Manuel Rosales Pena Alfaro
PhD student
Unversity of Essex
Wivenhoe Park
Colchester, CO4 3SQ
Essex, Uk
email: emrosa@essex.ac.uk
tel: +44-01206-87311
|
|
|
|
|
Your old formulae was xPos + (yPos x Height). I believe your new formulae would therefore be: (xPos + (yPos x Width)) + (zPos x (Height x Width)). This could probably be further reduced.
"When I left you I was but the learner, now I am the master" - Darth Vader
|
|
|
|
|
Thanks for your help....I tried that but it did not work...however, you gave an idea and I put myself to think deeply and I just found this:
Let be a multidimensional array MArray[max1stdim][max2nddim]...[maxnthdim], the indexes referencing certain location be i1,i2,...,in.
If MArray is declared as
int MArray[max1stdim*max2nddim*...*maxnthdim];
then the index referencing the location (i1,i2,...in-1,in) will be:
index=(i1*max2nddim*max3rddim*...*maxnthdim)+(i2*max3rddim*...*maxnthdim)+...+(in-1)*maxnthdim+in
Then for an array MArray[4][3][2] declared as MArray[3*3*3], the single index to reference (i,j,k)will be:
index=(i*4*3)+(j*2)+k
I have tried this and it works!
Thanks again.....
Eric Manuel Rosales Pena Alfaro
PhD student
Unversity of Essex
Wivenhoe Park
Colchester, CO4 3SQ
Essex, Uk
email: emrosa@essex.ac.uk
tel: +44-01206-87311
|
|
|
|
|
Can ask one question? how can you have "an array MArray[4][3][2] declared as MArray[3*3*3]"? If you are trying to access the 3*3*3 MArray at position [4][3][2] using a single index then that is impossible, because the rows and columns will be indexed from 0 through to 2 for each x,y and z column, so 4 and 3 are invalid index positions. If you were trying to access the last record in a 3*3*3 matrix, the single index value you require is 26 (0 to 8, 9 to 17, 18 to 26). Using my equation earlier, if you were trying to index the last position in the 3d array, so XPos = 2, YPos = 2 and ZPos = 2, you would get:
(2 + (2 * 3)) + (2 * 3 * 3)) = 26.
the index position you desire. Martin has implemented the coded version of the equation (but in the opposite direction to the way I wrote my equation) :
b[i*(d2*d3) + j*d3 + k] = x;
i = ZPos
j = YPos
k = XPos
d2 = Height
d3 = Width
*Please note I made a small error before when I wrote (yPos + Height), this is probably why you couldn't get it to work, it should have read (yPos + Width), which I have now changed it to. Without this correction the formula would only *work* if the 3d array was a cube. It should now work regardless of dimensions.
Alan.
"When I left you I was but the learner, now I am the master" - Darth Vader
|
|
|
|
|
Sorry for the mistake...the dimensions of the array MArray[4][3][2] declared as MAarray[4*3*2]...and trying to access element Marray(3,3,2)....
The rest of the formula is correct and your correction as well...
Thanks,
|
|
|
|
|
Try this:
int d1 = 3;
int d2 = 4;
int d3 = 5;
BYTE x = 0;
BYTE *b = new BYTE[d1*d2*d3];
for (int i = 0; i < d1; i++) {
for (int j = 0; j < d2; j++) {
for (int k = 0; k < d3; k++) {
b[i*(d2*d3) + j*d3 + k] = x;
x++;
}
}
}
I hope it does what you need.
|
|
|
|
|
Thanks for your help....I have tried that and it did work...therefore, making a generalization for this, I just found this:
Let be a multidimensional array MArray[max1stdim][max2nddim]...[maxnthdim], the indexes referencing certain location be i1,i2,...,in.
If MArray is declared as
int MArray[max1stdim*max2nddim*...*maxnthdim];
then the index referencing the location (i1,i2,...in-1,in) will be:
index=(i1*max2nddim*max3rddim*...*maxnthdim)+(i2*max3rddim*...*maxnthdim)+...+(in-1)*maxnthdim+in
Then for an array MArray[4][3][2] declared as MArray[3*3*3], the single index to reference (i,j,k)will be:
index=(i*4*3)+(j*2)+k
I have tried this and it works!
Thanks again.....
Eric Manuel Rosales Pena Alfaro
PhD student
Unversity of Essex
Wivenhoe Park
Colchester, CO4 3SQ
Essex, Uk
email: emrosa@essex.ac.uk
tel: +44-01206-87311
|
|
|
|
|
Would be nice if you will write article about it and compare performance between different types of allocations and accesseing that memory.
|
|
|
|
|
How will I do that? What allocation and accesing methods?. Where will I submit the article?...Would you like to help me?
Thanks,
Eric Manuel Rosales Pena Alfaro
PhD student
Unversity of Essex
Wivenhoe Park
Colchester, CO4 3SQ
Essex, Uk
email: emrosa@essex.ac.uk
tel: +44-01206-87311
|
|
|
|
|
I understood you think addressing multi-dimensional array with one index you are computing by yourself is a faster method than addressing multi-dimensional array with pointers. So what I think is a demonstration program using two arrays of the same size but different access methods and allocation and comparison between speeds. And obviously, you will submit article with that demo program here at CP.
|
|
|
|
|
Can anyone let me know why I see 'Linking...' and after this 'Compiling...' ?
Thank you.
[Sample]
--------------------Configuration: Shared - Win32 Release--------------------
Compiling...
EnumString.cpp
Linking...
Creating library .\Release/Shared.lib and object .\Release/Shared.exp
LINK : warning LNK4089: all references to "IMAGEHLP.dll" discarded by /OPT:REF
Compiling...
Report.cpp
Report2.cpp
ReportPreviewWnd.cpp
Shared.cpp
Generating Code...
Shared.dll - 0 error(s), 3 warning(s)
|
|
|
|
|
Your monitor might be upside down...
"Hey man, Taliban, Tali me Banana."
|
|
|
|
|
i guess slow refresh rate
t!
|
|
|
|
|
big sorry, i did not realize current forum
hm, it me already happened too (no your action after build run?), do not worry
t!
|
|
|
|
|
Isn't it that feature of Pentium when it computes some instructions before it needs them?
|
|
|
|
|
I'm writing a automation EXE server in VC++ 6.0 and I want to send COM/ActiveX events to the calling program... is it possible and how ?
The project was created as a MFC application with Automation support and works fine as a Automation server for my client application.
Now I just need to notify the client of events in my automation EXE application.
Please help.... thanks
|
|
|
|
|
*BUMP*
I really need help here, guys
|
|
|
|
|
|
Hi!
I use Spy++ to watch the messages a windows sends. For example if the user clicks on a menu item Spy++ loggs the following messages:
WM_MENUSELECT
WM_COMMAND wNotifyCode: 0 (sent from a menu) wID:7224
If I doubleclick on the WM_COMMAND log a new windows opens show the parameters (wParam and lParams) sent by WM_COMMAND. Often these values have a wParam like 00001C38.
Now I want to emulate this message. I tried:
SendMessage(wnd,WM_COMMAND,00001C38,0);
But this doesn't compile. So how do I need to write the wParam/lParam parameter shown in Spy++ in order to use it in my own SendMessage function?
thanks in advance
modified 12-Sep-18 21:01pm.
|
|
|
|
|
|
Thanks a lot!
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Is there a difference between
#pragma once and
#ifndef _THISDEF_
#define _THISDEF_
...
#endif ?
I've always defaulted to using the #pragma once but have noticed that others tend not to use it and favour the #ifndef instead, why is this? Am I missing something very simple?
Dylan Kenneally
London, UK
|
|
|
|
|
Good point. The #ifndef statement is only included in VC++ for backwards compatibility on older compilers and (according to MSDN) the #if !defined statement is supposed to be used instead (though it gives no reason as to why? surely the #ifndef code would be more portable because of this?). The #pragma statments are platform independent compiler directives, but I'm not sure of the difference between that and #ifndef or #if !defined, if there is any at all. I would like to know for sure though .
Alan.
"When I left you I was but the learner, now I am the master" - Darth Vader
|
|
|
|
|
There is a difference between #ifndef and #if !defined . You can have some operations in the latter case.
For instance:
#define A
#define B
#if !defined A AND !defined B
#include "header.h"
#else
#include "secondheader.h"
#endif
Best regards,
Alexandru Savescu
|
|
|
|