[code snippet] A class for filtering odd and even numbers

A class for filtering odd and even numbers.
    class int_filter
    {
        private delegate bool filter(int x);
        private int[] inseq;

        public int_filter()
        { }

        public int[] interface_filteredint()
        {
            filter a = new filter(iseven);

            return FileterArray(inseq, a);

        }
        public int_filter(int[] in_seq)
        {
            inseq = in_seq;

        }
        private int[] FileterArray(int[] input_seq, filter check_condition)
        {
            List<int> alist=new List<int>();

            foreach(int x in input_seq)
            {
                if (check_condition(x))
                    alist.Add(x);

            }
            return alist.ToArray();

        }
        private bool iseven(int input_to_check)
        {
            if (input_to_check % 2 == 0)
                return true;
            else
                return false;

        }
    }

Instantiating and testing the class.
         int[] anint={3,4,5,6,3,9,20,3,22,10,2};
            int_filter atest=new int_filter(anint);
            int[] int2 = atest.interface_filteredint();
            foreach (int x in int2)
                Console.WriteLine(x);

                Console.ReadKey();
Share on Google Plus

About Chien

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment