-3

I need find all combinations of several variables.

Example:

I have Field1[] with 3 values: 36, 38, 40. and Field2[] with values: R, V, A, B.

Then with this code I have all the combinations.

for (int x=1;x<=3; x++)
{
    for (int y=1;y<=4;y++)
    {
        debug.print (Field1[x] + "--" + Field2[y]);
    }
}

My problem isn't the lenght of "Field1" or "Field2". My problem is that I don't know how many variables "Field1" or "Field2" I have. Sometimes I can have more FieldX variables, I don't know how many.

How I can do it??

Thanks

Karlos Garcia
  • 174
  • 3
  • 16
  • All arrays in C# have a `.Length` property. Or you could use a `foreach` loop over the arrays, this would also fix the error in your code where you are skipping the first element of each array (and getting an out of bounds exception) – UnholySheep May 26 '20 at 13:21
  • Are your fields (`Field1`, and `Field2`) really using 1-based indices? I doubt it. Are they arrays? In which case the 3 indices are `0`, `1`, and `2`. And the loop idiom is `for (int x=0; x – Wyck May 26 '20 at 13:36
  • 2
    Does this answer your question? [Generating all Possible Combinations](https://stackoverflow.com/questions/3093622/generating-all-possible-combinations) – Wyck May 26 '20 at 13:45
  • My problem isn't the lenght of "Field1" or "Field2". My problem is that I don't know how many variables "Field1" or "Field2" I have – Karlos Garcia May 28 '20 at 12:03

4 Answers4

2

This is a runnable example of it using for and foreach.

class Program
{
    static void Main(string[] args)
    {
        var numbers = new int[] { 36, 38, 40 };
        var letters = new char[] { 'R', 'V', 'A', 'B' };

        Console.WriteLine("Using for example:");

        for (int i = 0; i < numbers.Length; i++)
        {
            for (int j = 0; j < letters.Length; j++)
            {
                Console.WriteLine($"{numbers[i]} -- {letters[j]}");
            }
        }

        Console.WriteLine("Using foreach example:");

        foreach (var number in numbers)
        {
            foreach (var letter in letters)
            {
                Console.WriteLine($"{number} -- {letter}");
            }
        }
    }
}

Output:

Using for example:
36 -- R
36 -- V
36 -- A
36 -- B
38 -- R
38 -- V
38 -- A
38 -- B
40 -- R
40 -- V
40 -- A
40 -- B
Using foreach example:
36 -- R
36 -- V
36 -- A
36 -- B
38 -- R
38 -- V
38 -- A
38 -- B
40 -- R
40 -- V
40 -- A
40 -- B
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
  • My problem isn't the lenght of "numbers" or "letters". My problem is that I don't know how many variables "numbers" or "letters" I have. – Karlos Garcia May 27 '20 at 06:47
1

This may be what your needing...its a List of objects(arrays). This will give you your count.

using System;
using System.Collections;
using System.Collections.Generic;

namespace ArrayCount
{
    class Program
    {
        public static void Main()
        {
            var numbers = new int[] { 36, 38, 40 };
            var letters = new char[] { 'R', 'V', 'A', 'B' };
            var words = new string[] { "this", "is", "the", "example" };

            List<object> arrayList = new List<object>
            {
                numbers,
                letters,
                words
            };

            Console.WriteLine("    Count:    {0}", arrayList.Count);
            Console.WriteLine("    Values:");
            PrintValues(arrayList);
        }

        public static void PrintValues(List<object> arrayList)
        {
            foreach (object obj in arrayList)
            {
                Console.WriteLine("---------------------");
                Console.WriteLine("Type: {0}", obj);
                foreach (var item in (IEnumerable)obj)
                {
                    Console.WriteLine("Value: {0}", item.ToString());
                }
            }
        }
    }
}

enter image description here

Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
  • Thanks Chris. Now I need the combinations between elements of "Numbers", "Letters" and "Words". I need the combinations: "36,R,this", "36,R,is", "36,R,the", "36,R,example", "36,V,this", "36,V,is", "36,V,the", ..... – Karlos Garcia May 29 '20 at 09:48
0

You would use the Length of the array

for (int x=0; x < Field1.Length; x++)
{
    for (int y=0; y < Field2.Length; y++)
    {
        debug.print (Field1[x] + "--" + Field2[y]);
    }
}

Or you can use the foreach loop

foreach (int x in Field1)
{
    foreach (int y in Field2)
    {
       System.Console.Write($"x:{x} y:{y}");
    } 
}
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
  • My problem isn't the lenght of "Field1" or "Field2". My problem is that I don't know how many variables "Field1" or "Field2" I have – Karlos Garcia May 28 '20 at 11:58
  • @Karlos Garcia The "length" of the array is how many items it has. Really? Look at ALL the answers...everyone is use the LENGTH. – Chris Catignani May 28 '20 at 12:50
  • Thanks Chris. You're right, but I don't think you understand me. I know the elements in Field1 with Field1.Length. But that's not the problem. The problem is how many Fields do I have. I can have Field1 and Field2, or Field1 and Field2 and Field3, or many FieldX. I don't know how many Field I can have. – Karlos Garcia May 28 '20 at 13:32
  • @Karlos Garcia I added another answer that may be what your needing... – Chris Catignani May 28 '20 at 20:51
0

You would do something like this:

for (int x = 0; x < Field1.Length; x++)
{
     for (int y = 0; y < Field2.Length; y++)
         Console.Write(Field1[x] + "--" + Field2[y]);
}

Or using LINQ you dont have to care about the Length property you can take the cross product and iterate over the same

var pairs = (from a in Field1
             from b in Field2
             select new { val1 = a, val2 = b }).ToList();

pairs.ForEach(x => Console.WriteLine(x.val1+ "--" + x.val2));
Shahid Manzoor Bhat
  • 1,307
  • 1
  • 13
  • 32
  • My problem isn't the lenght of "Field1" or "Field2". My problem is that I don't know how many variables "Field1" or "Field2" I have – Karlos Garcia May 28 '20 at 12:00
  • In any case the above solution will perfrom the Cartesian Product of the values of FIeld1 & Field2. Its a quadratic operation and will scale badly if you have more than a million records in Field1 & Field2. Since the complexity would be always O(n*m), n being the number of Values in Field1 & m being the number of values in Field2 – Shahid Manzoor Bhat May 28 '20 at 12:02