static void Main()
        {
            IEnumerable <int> intArr =new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            IEnumerable<double> dblArr =new[] { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };

            Console.WriteLine("Sum of: ");
            Console.WriteLine("Integer array: {0}", intArr.Sum());
            Console.WriteLine("Double array: {0}", dblArr.Sum());

            Console.WriteLine();
            Console.WriteLine("Product of: ");
            Console.WriteLine("Integer array: {0}", intArr.Product());
            Console.WriteLine("Double array: {0}", dblArr.Product());

            Console.WriteLine();
            Console.WriteLine("Average of: ");
            Console.WriteLine("Integer array: {0}", intArr.Average());
            Console.WriteLine("Double array: {0}", dblArr.Average());

            Console.WriteLine();
            Console.WriteLine("Min of: ");
            Console.WriteLine("Integer array: {0}", intArr.Min());
            Console.WriteLine("Double array: {0}", dblArr.Min());

            Console.WriteLine();
            Console.WriteLine("Max of: ");
            Console.WriteLine("Integer array: {0}", intArr.Max());
            Console.WriteLine("Double array: {0}", dblArr.Max());
        }
Example #2
0
    static void Main()
    {
        var numbers = new[] { 3, 5, 12, 15, 30, 35 };

        Console.WriteLine("Sum: {0}", numbers.Sum());
        Console.WriteLine("Product: {0}", numbers.Product());
        Console.WriteLine("Min: {0}", numbers.Min());
        Console.WriteLine("Max: {0}", numbers.Max());
        Console.WriteLine("Average: {0}", numbers.Average());

        Console.WriteLine();
    }
Example #3
0
        static void Main()
        {
            var items = new[]
            {
                1.2,
                2.3,
                3.4
            };

            Console.Write("Elements: ");
            foreach (var item in items)
            {
                Console.Write("{0} ", item);
            }
            Console.WriteLine();

            Console.WriteLine("Sum: " + items.Sum());
            Console.WriteLine("Product: " + items.Product());
            Console.WriteLine("Min: " + items.Min());
            Console.WriteLine("Max: " + items.Max());
            Console.WriteLine("Average: " + items.Average());
        }
        /*2.	Implement a set of extension methods for IEnumerable<T> that implement the following group functions:
         * sum, product, min, max, average.*/
        static void Main()
        {
            List<int> listInt = new List<int> { 1, 2, 3, 4, 5 };
            double[] testDouble = new[] { 4.5, 6.0, 5.7, 34.7 }; //sum = 50.9

            Console.WriteLine("---list <int>---");
            Console.WriteLine("Sum: {0}", listInt.Sum());
            Console.WriteLine("Average: {0}", listInt.Average());
            Console.WriteLine("Product: {0}", listInt.Product());
            Console.WriteLine("Minimum: {0}", listInt.Min());
            Console.WriteLine("Minimum: {0}", listInt.Max());

            Console.WriteLine("---double---");
            Console.WriteLine("Sum: {0}", testDouble.Sum());
            Console.WriteLine("Average: {0}", testDouble.Average());
            Console.WriteLine("Product: {0}", testDouble.Product());
            Console.WriteLine("Minimum: {0}", testDouble.Min());
            Console.WriteLine("Minimum: {0}", testDouble.Max());

            List<int> testEmpty = new List<int>();
            Console.WriteLine(testEmpty.Sum()); //return error when enumaration is empty, does not work about array(return 0)
        }
Example #5
0
File: Program.cs Project: GAlex7/TA
        static void Main()
        {
            var myStr = new StringBuilder("Proba");
            Console.WriteLine(myStr.Substring(0, 3));

            var myCollection = new[] { 1, 2, 3, 4, 5, 13 };
            Console.WriteLine("Average -> {0}", myCollection.Average());
            Console.WriteLine("Product -> {0}", myCollection.Product());
            Console.WriteLine();

            var students = new Student[4];
            students[0] = new Student
            {
                FirstName = "Ivan",
                LastName = "Georgiev",
                Age = 19,
                GroupNumber = 1,
                Email = "[email protected]",
                Tel = "0888123456",
                Marks = new List<int> { 6, 6 },
                FN = 123406789,
            };

            students[1] = new Student
            {
                FirstName = "Ana",
                LastName = "Georgieva",
                Age = 17,
                GroupNumber = 1,
                Email = "[email protected]",
                Tel = "02/8123456",
                Marks = new List<int> { 2, 2, 4 },
                FN = 123406789,
            };

            students[2] = new Student
            {
                FirstName = "Gergana",
                LastName = "Hristova",
                Age = 21,
                GroupNumber = 2,
                Email = "[email protected]",
                Tel = "0888123456",
                Marks = new List<int> { 6, 3, 6, 4 },
                FN = 123405789,
            };

            students[3] = new Student
            {
                FirstName = "Ivan",
                LastName = "Antonov",
                Age = 25,
                GroupNumber = 2,
                Email = "[email protected]",
                Tel = "028123458",
                Marks = new List<int> { 4, 3 },
                FN = 123404789,
            };

            var result = Check.ExtMethods.FirstBeforeLast(students);
            Print(result);

            result = Check.ExtMethods.AgeRange(students, 18, 24);
            Print(result);

            result = students
                .OrderByDescending(x => x.FirstName)
                .ThenByDescending(x => x.LastName)
                .ToArray();
            Print(result);

            var linkResult =
                from student in students
                where student.GroupNumber == 2
                orderby student.FirstName
                select student;
            Print(linkResult);

            result = students
                .Where(x => x.GroupNumber == 2)
                .OrderBy(x => x.FirstName)
                .ToArray();
            Console.WriteLine("Students in group 2 are (selected by Lambda):");
            Print(result);

            result = students
                .Where(x => x.Email.Substring(x.Email.IndexOf('@') + 1).Equals("abv.bg"))
                .ToArray();
            Console.WriteLine("Student(s) with email in abv.bg is/are:");
            Print(result);

            var resultSofiaPhone =
                from student in students
                where student.Tel.StartsWith("02")
                select student;
            Console.WriteLine("Student(s) with phone in Sofia is/are:");
            Print(resultSofiaPhone);

            var excellentStudents =
                from student in students
                where student.Marks.Contains(6)
                select new { FullName = student.FirstName + " " + student.LastName, Marks = string.Join(", ", student.Marks) };
            Console.WriteLine(string.Join(", ", excellentStudents));
            Console.WriteLine();

            result = students
                .Where(x => x.Marks.Count == 2)
                .ToArray();
            Console.WriteLine("Students with exactly two marks:");
            Print(result);

            var marks = students
                .Where(x => x.FN.ToString().Substring(4, 2) == "06")
                .Select(x => x.Marks);
            Console.WriteLine("All marks in 2006");
            foreach (var mark in marks)
            {
                Console.WriteLine(string.Join(", ", mark));
            }

            var res =
                from x in students
                group x by x.GroupNumber;
            Console.WriteLine("Students in groups:");
            foreach (var group in res)
            {
                Console.Write("Group ");
                Console.WriteLine(group.Key);
                foreach (var st in group)
                {
                    Console.WriteLine(st);
                }
            }
            Console.WriteLine();

            var resLambda = students
                .GroupBy(x => x.GroupNumber);
            Console.WriteLine("Students in groups (Lambda):");
            foreach (var group in resLambda)
            {
                Console.Write("Group ");
                Console.WriteLine(group.Key);
                foreach (var st in group)
                {
                    Console.WriteLine(st);
                }
            }
        }