Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Array of array recursion

Reply
Thread Tools

Array of array recursion

 
 
kings_oz@yahoo.com
Guest
Posts: n/a
 
      09-22-2005
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.

 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      09-22-2005

<> wrote in message
news: ups.com...
>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


 
Reply With Quote
 
 
 
 
kings_oz@yahoo.com
Guest
Posts: n/a
 
      09-22-2005
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

 
Reply With Quote
 
kings_oz@yahoo.com
Guest
Posts: n/a
 
      09-22-2005
the idea is similar to recursive a directory of folder and files and
printing out all its contents

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-22-2005
benben wrote:

>
> <> wrote in message
> news: ups.com...
>>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
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      09-22-2005
wrote:
>
> 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[i] );
}

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 );
}


--
Karl Heinz Buchegger

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      09-22-2005

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


Haha sure! How about

struct array_item
{
std::vector<array_item> items;
};

>
>
> Best
>
> Kai-Uwe



 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      09-22-2005
kings...@yahoo.com wrote:
> 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

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      09-23-2005
kings...@yahoo.com wrote:
> 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[i][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:rintf("%0d-%02d-%02d ",
myArray[i][j][k] >> 24,
(myArray[i][j][k] >> 16) & 0xFF,
myArray[i][j][k] & 0xFF);
}

Of course vectors would be a better choice than arrays.

Greg

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
va_arg... recursion: changing arguments and the using recursion jononanon@googlemail.com C Programming 8 04-26-2012 08:37 PM
Please Help: Recursion and Array Problem (Noob) James K. Ruby 2 05-25-2011 09:44 PM
how can I puts a char array reserve by recursion algoritym? emre esirik(hacettepe computer science and engineering) C Programming 4 11-26-2007 09:32 AM
recursion with perl B McInnes Perl 4 11-04-2003 06:08 AM
Output buffering problems during recursion Tim Mohler Perl 1 09-16-2003 12:35 AM



Advertisments