Array of array recursion

K

kings_oz

I have an array or items each item in the array can contain another
array of other items and that array of other items can contain more
array of item. I want recursively iterate through the array and print
out all the items. Can anyone show me some light. Thanks in advance.
 
B

benben

I have an array or items each item in the array can contain another
array of other items and that array of other items can contain more
array of item. I want recursively iterate through the array and print
out all the items. Can anyone show me some light. Thanks in advance.

struct array_item
{
array_item items[];
};

Ben
 
K

kings_oz

item_array[][]; for exam has a length of 10, in one of the 10 items the
item is another array containing 10 items which say two of its
locations have another array of items .............
I want to recursively print out each item in the array
pseudo
--- iterate the first array
if the element found is another array iterate this array
 
K

kings_oz

the idea is similar to recursive a directory of folder and files and
printing out all its contents
 
K

Kai-Uwe Bux

benben said:
I have an array or items each item in the array can contain another
array of other items and that array of other items can contain more
array of item. I want recursively iterate through the array and print
out all the items. Can anyone show me some light. Thanks in advance.

struct array_item
{
array_item items[];
};

Ben

May I suggest

struct array_item
{
array_item items[];
std::size_t length;
};

Otherwise, I do not see how any traversal algorithm is supposed to know
where to stop.


Best

Kai-Uwe
 
K

Karl Heinz Buchegger

I have an array or items each item in the array can contain another
array of other items and that array of other items can contain more
array of item. I want recursively iterate through the array and print
out all the items. Can anyone show me some light. Thanks in advance.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct Item
{
Item( const char* Name = "" ) : m_Name( Name ) {}

string m_Name;
vector< Item > m_Childs;
};

void Indent( int Level )
{
for( int i = 0; i < Level; ++i )
cout << " ";
}

void PrintIt( int Level, Item& What )
{
Indent( Level );
cout << What.m_Name << endl;

for( size_t i = 0; i < What.m_Childs.size(); ++i )
PrintIt( Level + 2, What.m_Childs );
}

void PrintIt( Item& What )
{
PrintIt( 0, What );
}

int main()
{
Item GrandChild3( "GrandChild3" );
GrandChild3.m_Childs.push_back( Item( "GrandGrandChild1" ) );

Item Child1( "Child1" );
Child1.m_Childs.push_back( Item( "GrandChild1" ) );
Child1.m_Childs.push_back( Item( "GrandChild2" ) );
Child1.m_Childs.push_back( GrandChild3 );

Item Child2( "Child2" );
Child2.m_Childs.push_back( Item( "GrandChild10" ) );
Child2.m_Childs.push_back( Item( "GrandChild11" ) );

Item Root( "Root" );

Root.m_Childs.push_back( Child1 );
Root.m_Childs.push_back( Child2 );

PrintIt( Root );
}
 
M

mlimber

I have an array or items each item in the array can contain another
array of other items and that array of other items can contain more
array of item. I want recursively iterate through the array and print
out all the items. Can anyone show me some light. Thanks in advance.

First, don't use arrays unless you absolutely must. Prefer std::vector.
Something like this, which can easily be extended for more nested
vectors:

#include <vector>
using namespace std;

struct Item1 { int datum; };
struct Item2
{
float myDatum;
vector<Item1> other;
};

int main()
{
vector<Item2> items;
// Add items with push_back or whatever
// ...

// Iterate through list
typedef vector<Item2>::const_iterator CI1;
for( CI1 i=items.begin(); i != items.end(); ++i )
{
// Can manipulate each member here, e.g.,
i->myDatum *= 2;

// Can iterate through the nested vector like this...
typedef vector<Item2>::const_iterator CI2;
for( CI2 j=i->other.begin(); j != i->other.end(); ++j )
{
// Do something here, e.g.,
j->datum++;
}
}
return 0;
}

Cheers! --M
 
G

Greg

I have an array or items each item in the array can contain another
array of other items and that array of other items can contain more
array of item. I want recursively iterate through the array and print
out all the items. Can anyone show me some light. Thanks in advance.

What is preventing you from doing so?

int main()
{
// an array of arrays of arrays of ints
int myArray[10][20][30];

// initialize
for (int i = 0; i < 10; i++)
for(int j = 0; j < 20; j++)
for (int k = 0; k < 30; k++)
myArray[j][k] = i << 24 | j << 16 | k;

// inspect
for (int i = 0; i < 10; i++)
for (int j = 0; j < 20; j++)
for (int k = 0; k < 30; k++)
std::printf("%0d-%02d-%02d ",
myArray[j][k] >> 24,
(myArray[j][k] >> 16) & 0xFF,
myArray[j][k] & 0xFF);
}

Of course vectors would be a better choice than arrays.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top