public static double CalculateAverageAgeOfAnimals(Animal[] animals) { int sumOfAge = 0; for (int i = 0; i < animals.Length; i++) { sumOfAge += animals[i].Age; } double averageAge = (double)sumOfAge/animals.Length; return averageAge; }
public static double AvarageAge(Animal[] animals) { double totalAge = 0; foreach (Animal animal in animals) { totalAge += animal.Age; } return (totalAge / animals.Length); }
static void Main() { //Create arrays of different kinds of animals Animal[] animals = new Animal[5]; animals[0] = new Kitten("Kitty", 1); animals[1] = new Tomcat("Tom", 5); animals[2] = new Frog("Froggy"); animals[3] = new Frog("granny Frog", 6, Sex.Female); animals[4] = new Dog("Balkan", 4, Sex.Male); //test sound Console.WriteLine("Test sounds:"); Console.WriteLine(); foreach (var item in animals) { Console.WriteLine(item); Console.WriteLine(item.ProduceSound()); } Console.WriteLine(); //and calculate the average age of each kind of animal using a static method (you may use LINQ). var animalsAverageAgeByType = animals.GroupBy(a => a.GetType().Name) .Select(g => new { Kind = g.Key, AverageAge = g.Average(item => item.Age) }); Console.WriteLine("Average age of each kind of animal using a static method"); foreach (var item in animalsAverageAgeByType) { Console.WriteLine(item); } Console.WriteLine(); var animalsAverageAgeByTypeLINQ = from a in animals group a by a.GetType().Name into k select new { Kind = k.Key, AverageAge = k.Average(a => a.Age) }; Console.WriteLine("Average age of each kind of animal using LINQ"); foreach (var item in animalsAverageAgeByTypeLINQ) { Console.WriteLine(item); } Console.WriteLine(); }
static void Main() { var animals = new Animal[] { new Tomcat("Tom", 7.2), new Tomcat("Grumpy", 3.1), new Kitten("Sarah", 3.2), new Frog("Jef", 0.7, Gender.Male), new Dog("Wow", 5.3, Gender.Male), new Kitten("Miko", 2.8), new Frog("Mary", 4.2, Gender.Female) }; Console.WriteLine("Average age of frogs is: {0}", Animal.GetAverageAge<Frog>(animals)); Console.WriteLine("Average age of dogs is: {0}", Animal.GetAverageAge<Dog>(animals)); Console.WriteLine("Average age of cats is: {0}", Animal.GetAverageAge<Cat>(animals)); }
static void Main() { Animal[] animals = new Animal[] { new Kitten(GetRandomName(), GetRandomAge()), new Tomcat(GetRandomName(), GetRandomAge()), new Dog(GetRandomName(), GetRandomAge(), Gender.Male), new Cat(GetRandomName(), GetRandomAge(), Gender.Male), new Frog(GetRandomName(), GetRandomAge(), Gender.Female), new Frog(GetRandomName(), GetRandomAge(), Gender.Male) }; Console.WriteLine("Some animals:"); foreach (Animal animal in animals) { Console.WriteLine(animal); } Console.WriteLine(); var kittens = new List<Kitten>(); for (int i = 0; i < 5; i++) { Kitten currerntKitten = new Kitten(GetRandomName(), GetRandomAge()); kittens.Add(currerntKitten); } double avrKitten = Kitten.CalculateAverageAge(kittens); Console.WriteLine("Kitten average age: " + avrKitten); var dogs = new List<Dog>(); for (int i = 0; i < 5; i++) { Dog currerntDog = new Dog(GetRandomName(), GetRandomAge(), Gender.Male); dogs.Add(currerntDog); } double avrDog = Dog.CalculateAverageAge(dogs); Console.WriteLine("Dog average age: " + avrDog); }
public static void PrintAverageAge(Animal[] animals) { float catsCount = 0f; uint catsAge = 0; float dogsCount = 0f; uint dogsAge = 0; float frogsCount = 0f; uint frogsAge = 0; foreach (var animal in animals) { if (animal.Species == Species.Cat) { catsCount++; catsAge += animal.Age; } else if (animal.Species == Species.Dog) { dogsCount++; dogsAge += animal.Age; } else if (animal.Species == Species.Frog) { frogsCount++; frogsAge += animal.Age; } } var result = new StringBuilder(); result.AppendLine(string.Format("Average age of cats: {0:F2}", catsAge / catsCount)); result.AppendLine(string.Format("Average age of dogs: {0:F2}", dogsAge / dogsCount)); result.AppendLine(string.Format("Average age of frogs: {0:F2}", frogsAge / frogsCount)); Console.WriteLine(result); }
static void Main(string[] args) { Animal[] animalList = new Animal[] { new Cat(8, "Sheakspeer", true), new Dog(4, "Koki", true), new Frog(20, "Arko", true), new Cat(68, "Silvester", true), new Cat(34, "Garfield", true), new Dog(5, "Ava", true), new Kitten(1, "Smilie"), new Tomcat(80, "Tom") }; double result = Animal.CalculateAvarageAge(animalList, "Frog"); double result2 = Animal.CalculateAvarageAge(animalList, "Dog"); double result3 = Animal.CalculateAvarageAge(animalList, "Cat"); double result4 = Animal.CalculateAvarageAge(animalList, "Kitten"); Console.WriteLine(result); Console.WriteLine(result2); Console.WriteLine(result3); Console.WriteLine(result4); }
public static double AverageAge(Animal[] arr) { return arr.Average(x => x.Age); }
public static double AverageAge(Animal[] animals) { return animals.Average(x => x.Age); }
private static double CalculateAverageYears(Animal[] collection) { double averageAge = collection.Average(x => x.Age); return Math.Round(averageAge, 2); }
public static double AverageAge(Animal[] animal) { var age = animal.Select(x => x.Age); double result = age.Average(); return result; }
private static void DisplayAvarageAge(Animal[] animals) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("{0:f2}", Animal.AvarageAge(animals)); Console.ForegroundColor = ConsoleColor.White; }
public static void CalculateAverageAgeOfKind(Animal[] animals) { //calculate the average age of each kind of animal var animalGroups = (from animal in animals group animal by animal.GetType().Name into groups select new { name = groups.Key, average = (from anim in groups select anim.Age).Average() }); foreach (var item in animalGroups) { Console.WriteLine(item); } }