n No of loops

Pages: 12
hi,

i have a problem to implement...i want a recursive function or any other technique that can get the work of n loops...

for.e.g.

for(i to t)
{
for(j to t)
{
for(k to t)

this all are nested loops....the thing is that i don't know the number of loops in the begining ....it will be known run time...for e.g if n=3 than 3 nested loops that run same number of times as above and 4 if n=4....plz help me to implement it...
Something like this?
1
2
3
4
5
6
7
8
void foo(int n)
{
	for(i to t)
		if (n <= 1)
			// do whatever it is you want to do.
		else
			foo(n-1);
}
1
2
3
4
5
6
void loopLooper(int loops)
int i = 0;
while(i < loops)
  loopLooper(loops);
  i++;
}


this work?
Last edited on
Have a function that takes at least the number of iterations per nested loop level, and how deep to nest. In the function if the nest level is positive and not zero, loop for iterations and call the function with the same parameters but nest level minus one. If the nest level is zero, do whatever action needs to be done. If you will need to track all the loop indices, this will be a bit more complex, but you will have to pass an array or vector or something containing the indices at each level.
actually i want the work of n loops...as the program will know "n" during run time so recursive function is a good strategy...may be u both are right...

so for e.g. if n=4

for(i to t)
{
for(j to t)
{
for(k to t)
{
for(l to t)

for this i think peter is right
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void DoLoops(unsigned NLoops, unsigned t)
{
    if(NLoops > 0)
    {
        for(unsigned i = 0; i < t; ++i)
        {
            DoLoops(NLoops-1, t);
        }
    }
    else
    {
        //do whatever
    }
}
Like this?
actually i have to make all possible combinations of a 2d array..for e.g. if i have a array of 4x3 ...i m using 4 loops,all runs up to 3.. to get all combinations...

for.e.g if i have a 4x3 array as given below..

1 2 3
4 5 6
7 8 9
10 11 12

i will have to make combinations like
1,4,7,10
1,4,7,11
1,4,7,12
1,4,8,10
1,4,8,11
1,4,8,12
1,4,9,10
1,4,9,11
1,4,9,12

1,5,8,10
1,5,8,11
1,5,8,12
...........and so on....

in short all such combinations...the max number of possible combinations in this case will be 3 power 4....and if i have a array of nxm then maximum combinations will be m power n....can any one help creating it....i want help to solve it in generic
Last edited on
I don't think this was what your first post asked for.
Also. Why did you repost this topic? You're last thread is perfectly suitable. Did you take any of the advice given to you there?
Last edited on
yes i took....i have implemented the logic of peter and L B
Hmm.. Then think about it logically if you can't figure it out with code.
All combinations. Does this include:
1,1,1,1
2,2,2,2
...
As well?
no
void recursive_function(int tasks)
{

if(tasks==0)
{
print();
}

else
{
for(int j=0;j<2;j++)
{
temp[tasks-1][j]=1;
recursive_function(tasks-1);
}

}


}


i m trying to achieve my goals throgh this....but all in vain
How about:
1,10,6,2?
no...u can't repeat numbers in the same row...as u said....1,10,6,2...here 1 & 2 are in the same row
..aslo u cant repeat numbers by themselves...as 1,1,1,1, or 2,2,2,2 etc
Oh ok. So its simply a um.. sandwich making thing. pick type of break, type of meat, type of cheese, and type of vegetable. One each.
column - 1 - 2 - 3
row 1 - 01 02 03
row 2 - 04 05 06
row 3 - 07 08 09
row 4 - 10 11 12

Take one number from each row and form a sequence of the numbers in ascending order according to row number. Find every combination possible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
First we need to define some variables.

Let max_rows = number of rows.
Let max_columns = number of columns.
Let rowIndex = current row number.
Let columnIndex = current column number.

Let currentSequence hold the current combination that we are working on...
Let combinationList hold our combinations in the format a, b, c, d; a, b, c, d; a, b, c, d...

// We can find the maximum number of combinations doing what you said:
//    max_rows ^ max_columns. Or something like that. But, we don't need this
//    information. What we need is every combination in list form.

//Recursive Function

rowIndex = 0
columnIndex = 0
combinationList = ""
currentSequence = ""
// we have "" to represent that these variables hold nothing right now.

GetCombination(columnIndex, rowIndex, reference to combinationList, reference to currentSequence)
{

    if rowIndex is less than max_rows
    {

        while columnIndex is less than max_columns
        {

            add the number in the array at the (rowIndex)th row and at the (columnIndex)th column to currentSequence
            add a comma and space to currentSequence

            GetCombination(columnIndex, rowIndex+1, combinationList, currentSequence)

            if rowIndex is max_rows-1
            {

                replace the (max_rows)th comma with semicolon in currentSequence
                add currentSequence to combinationList

            }

            if rowIndex is zero
            {
                remove all characters from currentSequence
            }
            else
            {
                remove all characters after the (rowIndex)th space in currentSequence
            }

            increment columnIndex

        }

    }

}

GetCombination(0, 0, combinationList, currentSequence)


I think this would be the pseudocode implementation of what you want.

Now im'a go plug it into C++ cus I'm curious as to if it actually works.

EDIT: My psuedocode has just a couple flaws. Let me tweak them out a bit.
Last edited on
I can suggest the following code. At least it works though it is not an optimal code. It is written with using MS VC++ 2010.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "stdafx.h"
#include	<iostream>
#include	<vector>
#include	<algorithm>


void traverse( int **a, int Rows, int Cols )
{
	static std::vector<int> v( Rows );
	static int pos;

	if ( pos == Rows )
	{
		std::for_each( v.begin(), v.end(), []( int x ) { std::cout << x << ' '; } );
		std::cout <<  std::endl;
	}
	else
	{
		for ( int i = 0; i < Cols; i++ )
		{
			v[pos] = a[pos][i];
			++pos;
			repeat1( a, Rows, Cols );
		}
	}

	if ( pos != 0 ) --pos;

	return;
}

int _tmain(int argc, _TCHAR* argv[])
{
	const int Rows = 4;
	const int Cols = 3;

	int **a = new int * [Rows];

	for ( int i = 0; i < Rows; i++ )
	{
		a[i] = new int[Cols];
		for ( int j = 0; j < Cols; j++ )
		{
			a[i][j] = i * Cols + j;
		}
	}

	for ( int i = 0; i < Rows; i++ )
	{
		for ( int j = 0; j < Cols; j++ )
		{
			std::cout << a[i][j] << ' ';
		}
		std::cout << std::endl;
	}

	traverse( a, Rows, Cols );

	for ( int i = 0; i < Rows; i++ )
	{
		delete [] a[i];
	}
	delete [] a;

	return 0;
}
Last edited on
Ok. Here is my final recursive function.

Note that rowIndex and columnIndex can be anything and you will get the proper output!

CombinationList and currentSequence need to be strings or your own character array manager. Note that they do not have to be empty! The function will still work. But. There is no point for currentSequence to have any characters.

combinationList = ""
currentSequence = ""

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
GetCombination(columnIndex, rowIndex,
    reference to combinationList, reference to currentSequence)
{

    if rowIndex is less than max_rows
    {

        while columnIndex is less than max_columns
        {

            add the number in the array at the (rowIndex)th row and
                at the (columnIndex)th column to currentSequence
            add a comma and space to currentSequence

            GetCombination(columnIndex, rowIndex+1, combinationList, currentSequence)

            if rowIndex is max_rows-1
            {

                remove all characters after and including the (max_rows)th
                    comma in currentSequence.
                add whatever ending you want to the currentSequence. a newline
                    will put every combination on its own line.

                add currentSequence to combinationList

            }

            if rowIndex is zero
            {
                remove all characters from currentSequence
            }
            else
            {
                remove all characters after the (rowIndex)th space in currentSequence
            }

            increment columnIndex

        }

    }

}


Let's assume that the array is:
column - 1 - 2 - 3
row 1 - 01 02 03
row 2 - 04 05 06
row 3 - 07 08 09
row 4 - 10 11 12

Let's call our function with the proper stuff:
GetCombination(0, 0, reference to combinationList, reference to currentSequence)

What is the output? This is directly from my console window (keep in mind that I added some newline functionality):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1, 4, 7, 10; 1, 4, 7, 11; 1, 4, 7, 12;
1, 4, 8, 10; 1, 4, 8, 11; 1, 4, 8, 12;
1, 4, 9, 10; 1, 4, 9, 11; 1, 4, 9, 12;
1, 5, 7, 10; 1, 5, 7, 11; 1, 5, 7, 12;
1, 5, 8, 10; 1, 5, 8, 11; 1, 5, 8, 12;
1, 5, 9, 10; 1, 5, 9, 11; 1, 5, 9, 12;
1, 6, 7, 10; 1, 6, 7, 11; 1, 6, 7, 12;
1, 6, 8, 10; 1, 6, 8, 11; 1, 6, 8, 12;
1, 6, 9, 10; 1, 6, 9, 11; 1, 6, 9, 12;

2, 4, 7, 10; 2, 4, 7, 11; 2, 4, 7, 12;
2, 4, 8, 10; 2, 4, 8, 11; 2, 4, 8, 12;
2, 4, 9, 10; 2, 4, 9, 11; 2, 4, 9, 12;
2, 5, 7, 10; 2, 5, 7, 11; 2, 5, 7, 12;
2, 5, 8, 10; 2, 5, 8, 11; 2, 5, 8, 12;
2, 5, 9, 10; 2, 5, 9, 11; 2, 5, 9, 12;
2, 6, 7, 10; 2, 6, 7, 11; 2, 6, 7, 12;
2, 6, 8, 10; 2, 6, 8, 11; 2, 6, 8, 12;
2, 6, 9, 10; 2, 6, 9, 11; 2, 6, 9, 12;

3, 4, 7, 10; 3, 4, 7, 11; 3, 4, 7, 12;
3, 4, 8, 10; 3, 4, 8, 11; 3, 4, 8, 12;
3, 4, 9, 10; 3, 4, 9, 11; 3, 4, 9, 12;
3, 5, 7, 10; 3, 5, 7, 11; 3, 5, 7, 12;
3, 5, 8, 10; 3, 5, 8, 11; 3, 5, 8, 12;
3, 5, 9, 10; 3, 5, 9, 11; 3, 5, 9, 12;
3, 6, 7, 10; 3, 6, 7, 11; 3, 6, 7, 12;
3, 6, 8, 10; 3, 6, 8, 11; 3, 6, 8, 12;
3, 6, 9, 10; 3, 6, 9, 11; 3, 6, 9, 12;


EDIT: Changed a few lines in the pseudo-code to help out.
Last edited on
What if combinationList = "Here are all the possible combinations:\n\n"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Here are all the possible combinations:

1, 4, 7, 10; 1, 4, 7, 11; 1, 4, 7, 12;
1, 4, 8, 10; 1, 4, 8, 11; 1, 4, 8, 12;
1, 4, 9, 10; 1, 4, 9, 11; 1, 4, 9, 12;
1, 5, 7, 10; 1, 5, 7, 11; 1, 5, 7, 12;
1, 5, 8, 10; 1, 5, 8, 11; 1, 5, 8, 12;
1, 5, 9, 10; 1, 5, 9, 11; 1, 5, 9, 12;
1, 6, 7, 10; 1, 6, 7, 11; 1, 6, 7, 12;
1, 6, 8, 10; 1, 6, 8, 11; 1, 6, 8, 12;
1, 6, 9, 10; 1, 6, 9, 11; 1, 6, 9, 12;

2, 4, 7, 10; 2, 4, 7, 11; 2, 4, 7, 12;
2, 4, 8, 10; 2, 4, 8, 11; 2, 4, 8, 12;
2, 4, 9, 10; 2, 4, 9, 11; 2, 4, 9, 12;
2, 5, 7, 10; 2, 5, 7, 11; 2, 5, 7, 12;
2, 5, 8, 10; 2, 5, 8, 11; 2, 5, 8, 12;
2, 5, 9, 10; 2, 5, 9, 11; 2, 5, 9, 12;
2, 6, 7, 10; 2, 6, 7, 11; 2, 6, 7, 12;
2, 6, 8, 10; 2, 6, 8, 11; 2, 6, 8, 12;
2, 6, 9, 10; 2, 6, 9, 11; 2, 6, 9, 12;

3, 4, 7, 10; 3, 4, 7, 11; 3, 4, 7, 12;
3, 4, 8, 10; 3, 4, 8, 11; 3, 4, 8, 12;
3, 4, 9, 10; 3, 4, 9, 11; 3, 4, 9, 12;
3, 5, 7, 10; 3, 5, 7, 11; 3, 5, 7, 12;
3, 5, 8, 10; 3, 5, 8, 11; 3, 5, 8, 12;
3, 5, 9, 10; 3, 5, 9, 11; 3, 5, 9, 12;
3, 6, 7, 10; 3, 6, 7, 11; 3, 6, 7, 12;
3, 6, 8, 10; 3, 6, 8, 11; 3, 6, 8, 12;
3, 6, 9, 10; 3, 6, 9, 11; 3, 6, 9, 12;


All the work is practically done for you.
Pages: 12