I've been trying to simplify the following by putting it into a loop..
int A0 = 0, A1 = 0, A2 = 0;
for (A0 = 0; A0 < nums.Length; A0++)
{
for (A1 = 0; A1 < nums.Length; A1++)
{
for (A2 = 0; A2 < nums.Length; A2++)
{
string ss = nums[A0] + nums[A1] + nums[A2];
dataGridView1.Rows.Add(new string[] { ss });
}
}
}
like A0, A1 and A2, i need to go until A75. I can get the result if i nest like the above. But how can i put it on a loop..??
I tried this one:
int[] A = new int[3];
for (int i = 0; i < A.Length; i++)
{
for (A[i] = 0; A[i] < nums.Length; A[i]++)
{
string ss = "";
for (int j = 0; j < A.Length; j++) ss += nums[A[i]];
dataGridView1.Rows.Add(new string[] { ss });
}
}
But it will perform only like..
int A0 = 0, A1 = 0, A2 = 0;
for (A0 = 0; A0 < nums.Length; A0++)
{
string ss = nums[A0] + nums[A1] + nums[A2];
dataGridView1.Rows.Add(new string[] { ss });
}
for (A1 = 0; A1 < nums.Length; A1++)
{
string ss = nums[A0] + nums[A1] + nums[A2];
dataGridView1.Rows.Add(new string[] { ss });
}
for (A2 = 0; A2 < nums.Length; A2++)
{
string ss = nums[A0] + nums[A1] + nums[A2];
dataGridView1.Rows.Add(new string[] { ss });
}
An equivalent to
public void DoNestedThings()
{
for(var A0 = 0; A0 < _max; A0 ++)
{
//...
for(var i5 = 0; i5 < _max; i5++)
{
DoThing(new List<int>{i0, ..., i5});
}
}
}
Would be:
private void DoNestedThings(int depth, Stack<int> indexes)
{
if(depth == 0)
{
DoThing(indexes);
return;
}
for(var i = 0; i < _max; i++)
{
indexes.Push(i);
DoNestedThings(depth-1, indexes);
indexes.Pop();
}
}
public void DoNestedThings()
{
DoNestedThings(5, new Stack<int>());
}
This replaces nested loops with a single loop, but then uses recursion to enter into that loop multiple times. Each time the DoNestedThings method is called with depth > 0, you enter into another loop.
(Note the order of the indexes passed to DoThing will be reversed)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With