15,797,923 members
1.00/5 (1 vote)
See more:
How to convert 3D arrays to 1D arrays ?
for example i want to Edit arr[i][j][k]; but i don't want addressing array with this way.
in fact i want to give one number (instead 3 number ) to program for access the element of array.
(How to Save 3d array to 1d array???!!!)
Posted
Updated 26-Oct-12 9:16am
v4
OriginalGriff 26-Oct-12 13:58pm
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
meysamp73 26-Oct-12 14:15pm
thanks
Sergey Alexandrovich Kryukov 26-Oct-12 15:06pm
I think I understand what's the problem -- please see my answer.
--SA
meysamp73 26-Oct-12 14:21pm
hey
help me
Sandeep Mewara 26-Oct-12 14:42pm
Your question is still not clear even though you have updated it.

Rephrase it.

Solution 2

Multidimensional arrays are just an abstraction for programmers. They have the same structure as single-dimensional arrays with the same total number of elements. All you need is understanding of the order of the indices in the multidimensional syntax: the elements are consider arranged sequentially in the following way: the change in rightmost index by one points to the nearest element in memory.

http://www.osdata.com/programming/arrays/arrays.html[^],
http://www.cplusplus.com/doc/tutorial/arrays/[^], section "Multidimensional arrays".

So, all you need it to understand how array elements addressed by multiple indices are arranged in memory, and that all indices are zero-based. It's all about understanding of indexing and syntax, nothing else.

For example, having
C++
`int arr[4][6][12];`

these addressings are equivalent:
C++
```bool sameThing = arr[1][1][0] == arr[24]; // true; 6 * 4
sameThing = arr[1][1][1] == arr[25]; // true
// and so on...```

—SA

v4
pasztorpisti 28-Oct-12 22:09pm
+5 to counter vote. The OP wanted a ready made solution, he isn't really interested in whats going on behind the curtains... I think you made a mistake here: arr[3][5][0] with linear indexing is 3*6*12 + 5*12 + 0 but this isn't a reason for a 1.
Sergey Alexandrovich Kryukov 28-Oct-12 23:08pm
Thank you.
No, the reason for 1 is likely a very different: I fount some 10 1's in a row -- it looks more like personal hatred. Funny. The error..? well, I'll check up, thank you.
--SA
pasztorpisti 29-Oct-12 6:05am
I know what you are talking about and really don't know what these people want to achieve with this. They are just wasting their time.
Sergey Alexandrovich Kryukov 29-Oct-12 10:59am
(Sigh...) You are right, but there is nothing wrong in addressing the same object with arrays of different rank; and the skill of doing such things is important. If some programming "trick" is possible but using makes no sense, I usually don't provide such solution but try to explain why it's useless. This question is not exactly the case. Good point about wasted time though -- and it's up to you how to spend it, but I would not recommend address anything which makes no sense.
--SA
pasztorpisti 29-Oct-12 11:16am
Sorry, I think my previous post was confusing, I meant the downvoters waste their time. Knowing how array indexing works is useful. This is again just about getting to know an anti-pattern that is always useful. :-) It might come handy if you implement a custom datastructure.

Solution 4

Please take a look at the last function below :) :
C++
```#define IS_IDX_VALID(i, count) (0 <= i && i < count)

class CTest
{
enum { slotCount = 10, rowCount = 10, colCount = 20, invalidValue = INT_MIN };

int m_arValues[slotCount][rowCount][colCount];

void InitValues() {..}

public:
CTest() { InitValues(); }

int GetValue(int s, int r, int c) const
{
int iResult(invalidValue);

bool bValidParams(IS_IDX_VALID(s, slotCount) &&
IS_IDX_VALID(r, rowCount) &&
IS_IDX_VALID(c, colCount) &&);
ASSERT(bValidParams);

if (bValidParams) {
iResult = m_arValues[s][r][c];
}

return iResult;
}

int GetValue(int iAbsAddr) const
{
int iResult(invalidValue);

static int siSlotMaxCount(rowCount * colCount)
static int siAbsMax(slotCount * siSlotMaxCount);

if (IS_IDX_VALID(iAbsAddr, siAbsCount)) {
int s = iAbsAddr / siSlotMaxCount,
t = iAbsAddr % siSlotMaxCount,
r = t / colCount,
c = t % colCount;

iResult = m_arValues[s][r][c];
}

return iResult;
}
};```

v3
meysamp73 26-Oct-12 15:42pm
this is good alghorithm to solve my problem but i want to use this formula (http://www.hpkclasses.ir/Courses/DataStructure/ds0300.html) and manual convert
in fact first i write thats program in C then write in assemly language..
however thanks so much..

Solution 3

hi,
suppose your 3d arr[i][j][k] array has integers then declare another 1d array of size i*j*k and use nested loops to read each element in the 3d array and assign them to the 1d array.

for more about nested loops :

http://www.tutorialspoint.com/cplusplus/cpp_nested_loops.htm[^]

hope this helps

v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Maciej Los 80 Richard MacCutchan 60 Member 13219060 45 Andre Oosthuizen 40 Dave Rave 20
 Richard Deeming 538 Andre Oosthuizen 360 Maciej Los 350 Dave Kreskowiak 335 Richard MacCutchan 243

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900