none
The Conver to int[] don't work RRS feed

  • Question

  • Goal:  
    Change <T> and T[]  to <int> and <int>.

    Problem:
    The code still don't work after changing to <int> and <int>.

    I don't get enough with feedback in order to solve it.

    Info:

    *The source of the code is from this link 

    *The soucecode is below

    Thank you!

    https://social.msdn.microsoft.com/Forums/en-US/0f21b92d-1998-4cc3-b020-56090db5bb68/generating-all-possible-combinations-of-a-list-of-numbers?forum=csharplanguage
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        public class Program
        {
            // https://social.msdn.microsoft.com/Forums/en-US/0f21b92d-1998-4cc3-b020-56090db5bb68/generating-all-possible-combinations-of-a-list-of-numbers?forum=csharplanguage
    
    
            static void Main(string[] args)
            {
                int[] numbers = new[]
                {
                    //0, 1, 2, 2
                       1, 1, 1, 2, 4, 4
    
                    //0, 0, 0, 0, 4 ,5
    
                };
    
                Permute(numbers, Output);
                Console.WriteLine();
                /*
                string[] strings = new[]
                {
                    "Red", "Red","Red", "Red", "Yellow", "Black"
                    //"Yellow", "Yellow", "Yellow", "Yellow", "Black", "Brown"
    
                };
    
                Permute(strings, Output);
                Console.WriteLine();
                */
    
                int ff = 23;
            }
    
    
            private static void Output<int>(int[] permutation)
            {
                foreach (int item in permutation)
                {
                    Console.Write(item);
                    Console.Write(" ");
                }
    
                Console.WriteLine();
            }
    
    
            public static void Permute(int[] items, Action<int[]> output)
            {
                Permute(items, 0, new int[items.Length], new bool[items.Length], output);
            }
    
    
            private static void Permute<int>(int[] items, int item, int[] permutation, bool[] used, Action<int[]> output)
            {
                for (int i = 0; i < items.Length; ++i)
                {
                    if (!used[i])
                    {
                        used[i] = true;
                        permutation[item] = items[i];
    
                        if (item < (items.Length - 1))
                        {
                            Permute(items, item + 1, permutation, used, output);
                        }
                        else
                        {
                            output(permutation);
                        }
    
                        used[i] = false;
                    }
                }
            }
    
    
    
    
    
            /*
            private static void Output<T>(T[] permutation)
            {
                foreach (T item in permutation)
                {
                    Console.Write(item);
                    Console.Write(" ");
                }
    
                Console.WriteLine();
            }
    
            public static void Permute<T>(T[] items, Action<T[]> output)
            {
                Permute(items, 0, new T[items.Length], new bool[items.Length], output);
            }
    
    
            private static void Permute<T>(T[] items, int item, T[] permutation, bool[] used, Action<T[]> output)
            {
                for (int i = 0; i < items.Length; ++i)
                {
                    if (!used[i])
                    {
                        used[i] = true;
                        permutation[item] = items[i];
    
                        if (item < (items.Length - 1))
                        {
                            Permute(items, item + 1, permutation, used, output);
                        }
                        else
                        {
                            output(permutation);
                        }
    
                        used[i] = false;
                    }
                }
            }
       */
    
        } // Class
    }
    

    Monday, July 30, 2018 8:26 PM

Answers

  • Hi Sakura Data,

    Thank you for posting here.

    For your question, if you want to use number in your code, there is no need to use generic methods. Please try the code below.

    using System;
    
    namespace ConsoleApplication1
    {
        public class Program
        {
            static void Main(string[] args)
            {
                int[] numbers = new[]
                {
                    //0, 1, 2, 2
                       1, 1, 1, 2, 4, 4
                    //0, 0, 0, 0, 4 ,5
                };
                Permute(numbers, Output);
                Console.WriteLine();
            }
            private static void Output(int[] permutation)
            {
                foreach (int item in permutation)
                {
                    Console.Write(item);
                    Console.Write(" ");
                }
                Console.WriteLine();
            }
            public static void Permute(int[] items, Action<int[]> output)
            {
                Permute(items, 0, new int[items.Length], new bool[items.Length], output);
            }
            private static void Permute(int[] items, int item, int[] permutation, bool[] used, Action<int[]> output)
            {
                for (int i = 0; i < items.Length; ++i)
                {
                    if (!used[i])
                    {
                        used[i] = true;
                        permutation[item] = items[i];
                        if (item < (items.Length - 1))
                        {
                            Permute(items, item + 1, permutation, used, output);
                        }
                        else
                        {
                            output(permutation);
                        }
                        used[i] = false;
                    }
                }
            }
        }
    }
    
     
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sakura Data Tuesday, July 31, 2018 3:58 PM
    Tuesday, July 31, 2018 8:11 AM
    Moderator

All replies

  • Hi Sakura Data,

    Thank you for posting here.

    For your question, if you want to use number in your code, there is no need to use generic methods. Please try the code below.

    using System;
    
    namespace ConsoleApplication1
    {
        public class Program
        {
            static void Main(string[] args)
            {
                int[] numbers = new[]
                {
                    //0, 1, 2, 2
                       1, 1, 1, 2, 4, 4
                    //0, 0, 0, 0, 4 ,5
                };
                Permute(numbers, Output);
                Console.WriteLine();
            }
            private static void Output(int[] permutation)
            {
                foreach (int item in permutation)
                {
                    Console.Write(item);
                    Console.Write(" ");
                }
                Console.WriteLine();
            }
            public static void Permute(int[] items, Action<int[]> output)
            {
                Permute(items, 0, new int[items.Length], new bool[items.Length], output);
            }
            private static void Permute(int[] items, int item, int[] permutation, bool[] used, Action<int[]> output)
            {
                for (int i = 0; i < items.Length; ++i)
                {
                    if (!used[i])
                    {
                        used[i] = true;
                        permutation[item] = items[i];
                        if (item < (items.Length - 1))
                        {
                            Permute(items, item + 1, permutation, used, output);
                        }
                        else
                        {
                            output(permutation);
                        }
                        used[i] = false;
                    }
                }
            }
        }
    }
    
     
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sakura Data Tuesday, July 31, 2018 3:58 PM
    Tuesday, July 31, 2018 8:11 AM
    Moderator
  • Thank you for your help!

    I have a curious question.

    What tool do you use in order to create a video that is an animated picture.

    Thank you!
    Tuesday, July 31, 2018 3:58 PM