473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Array of array recursion

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.

Sep 22 '05 #1
8 2479

<ki******@yahoo.com> wrote in message
news:11*********************@g43g2000cwa.googlegro 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
Sep 22 '05 #2
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

Sep 22 '05 #3
the idea is similar to recursive a directory of folder and files and
printing out all its contents

Sep 22 '05 #4
benben wrote:

<ki******@yahoo.com> wrote in message
news:11*********************@g43g2000cwa.googlegro 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
Sep 22 '05 #5
ki******@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.


#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
kb******@gascad.at
Sep 22 '05 #6
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

Sep 22 '05 #7
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

Sep 22 '05 #8
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::printf("%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

Sep 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: J. J. Cale | last post by:
In IE6 both these functions *seem* to be working. I don't see much recursion in this group except the occasional setTimeout problems. Besides the obvious stack problems that can occur if one...
37
by: Carol Depore | last post by:
How do I determine the maximum array size? For example, int a works, but a does not (run time error). Thank you.
4
by: Daniel | last post by:
I need to build the maze board(like 2d array) using a tree. But I find that my buildTree function doesn't work. Could anyone give me some hints on debugging it? Thanks bool BuildTree(TreeNodePtr...
5
by: Manish | last post by:
This is the print_r() for a variable $categories. $categories :: Array ( =Array ( =Array (
1
by: GB | last post by:
Hello, I have a recursion function which generate 1-dimensional vector of integers so for 3 cycles I have, for example, following vectors: int vec1 = {1,3,2,4}; int vec2 = {2,5,6,2}; int vec3 =...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
1
by: dennis.sprengers | last post by:
Consider the following array and string: $trail = array('products/veggies', 'products', 'services/cleaning'); $path = 'products/veggies/1243/more'; I am trying to write a function that matches...
14
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be...
45
by: hank | last post by:
I have this code here, it converts decimal numbers to binary. There is one problem. The output is printed 'in reverse' and I have no clue at all how to solve this problem. Output of program: ...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.