Есть 10 рандомных чисел. знаю которые из них чётные а которые нет. нужно отдельно для чётных узнать их суму, среднее арифметическое, максимальное число, минимальное число и также отдельно нужно узнать всё это и для чётных чисел. нужно использовать только цикл for.(ну и там if, elseif, else) Вот код: namespace ConsoleApp7 { class Program { static void Main(string[] args) { Random r = new Random(); int[] array = new int[10]; for (int i = 0; i < array.Length; i++) { array[i] = r.Next(100); bool even; if (array[i] % 2 == 0) { even = true; } else { even = false; } Console.Write("array[" + i + "] = " + array[i]); if (even == true) { Console.WriteLine(" is even"); } else { Console.WriteLine(" is odd"); } } Console.ReadKey(); } } } Код namespace ConsoleApp7 { class Program { static void Main(string[] args) { Random r = new Random(); int[] array = new int[10]; for (int i = 0; i < array.Length; i++) { array[i] = r.Next(100); bool even; if (array[i] % 2 == 0) { even = true; } else { even = false; } Console.Write("array[" + i + "] = " + array[i]); if (even == true) { Console.WriteLine(" is even"); } else { Console.WriteLine(" is odd"); } } Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp7 { class Program { static void Main(string[] args) { Random r = new Random(); int[] array = new int[10]; double evensSum = 0; double evensavg = 0; double oddsSum = 0; double oddsavg = 0; for (int i = 0; i < array.Length; i++) { array[i] = r.Next(100); bool? even = null; if (array[i] % 2 == 0) { even = true; } else if (array[i] % 2 == 1) { even = false; } else { even = null; } Console.Write("array[" + i + "] = " + array[i]); if (even == true) { Console.WriteLine(" is even"); } else { Console.WriteLine(" is odd"); } if (even == true) { evensSum += array[i]; evensavg = evensSum / i; } else { oddsSum += array[i]; oddsavg = oddsSum / i; } } Console.WriteLine("Avarage of Odds: " + oddsavg); Console.WriteLine("Avarage of Evens: " + evensavg); Console.WriteLine("Sum of Odds: " + oddsSum); Console.WriteLine("Sum of Evens: " + evensSum); Console.ReadKey(); } } } Код using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp7 { class Program { static void Main(string[] args) { Random r = new Random(); int[] array = new int[10]; double evensSum = 0; double evensavg = 0; double oddsSum = 0; double oddsavg = 0; for (int i = 0; i < array.Length; i++) { array[i] = r.Next(100); bool? even = null; if (array[i] % 2 == 0) { even = true; } else if (array[i] % 2 == 1) { even = false; } else { even = null; } Console.Write("array[" + i + "] = " + array[i]); if (even == true) { Console.WriteLine(" is even"); } else { Console.WriteLine(" is odd"); } if (even == true) { evensSum += array[i]; evensavg = evensSum / i; } else { oddsSum += array[i]; oddsavg = oddsSum / i; } } Console.WriteLine("Avarage of Odds: " + oddsavg); Console.WriteLine("Avarage of Evens: " + evensavg); Console.WriteLine("Sum of Odds: " + oddsSum); Console.WriteLine("Sum of Evens: " + evensSum); Console.ReadKey(); } } } Почему среднее арифметическое показывается неверно?
mikef0x, вынеси из if evensavg = evensSum / i; oddsavg = oddsSum / i; Код evensavg = evensSum / i; oddsavg = oddsSum / i; а так на самом деле хуево, что у тебя каждый проход цикла for выполняются эти строчки
хевый, но рабочий code public static void Main() { Random r = new Random(); int[] array = new int[2]; double evensSum = 0; double oddsSum = 0; int oddCount = 0; int evenCount = 0; for (int i = 0; i < array.Length; i++) { array[i] = r.Next(100); if (array[i] % 2 == 0) { evensSum += array[i]; Console.WriteLine(array[i] + " is even" ); evenCount += 1; } else { oddsSum += array[i]; oddCount += 1; Console.WriteLine(array[i] + " is odd" ); } } Console.WriteLine("Avarage of Odds: " + oddsSum/oddCount); Console.WriteLine("Avarage of Evens: " + evensSum/evenCount); Console.WriteLine("Sum of Odds: " + oddsSum); Console.WriteLine("Sum of Evens: " + evensSum); } Код public static void Main() { Random r = new Random(); int[] array = new int[2]; double evensSum = 0; double oddsSum = 0; int oddCount = 0; int evenCount = 0; for (int i = 0; i < array.Length; i++) { array[i] = r.Next(100); if (array[i] % 2 == 0) { evensSum += array[i]; Console.WriteLine(array[i] + " is even" ); evenCount += 1; } else { oddsSum += array[i]; oddCount += 1; Console.WriteLine(array[i] + " is odd" ); } } Console.WriteLine("Avarage of Odds: " + oddsSum/oddCount); Console.WriteLine("Avarage of Evens: " + evensSum/evenCount); Console.WriteLine("Sum of Odds: " + oddsSum); Console.WriteLine("Sum of Evens: " + evensSum); }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp7 { class Program { static void Main(string[] args) { Random r = new Random(); int[] array = new int[10]; double evenSum = 0; double oddSum = 0; double oddcount = 0; double evencount = 0; int oddmax = array[0]; int oddmin = array[0]; int evenmax = array[0]; int evenmin = array[0]; for (int i = 0; i < array.Length; i++) { array[i] = r.Next(100); bool? even = null; if (array[i] % 2 == 0) { even = true; } else if (array[i] % 2 == 1) { even = false; } else { even = null; } Console.Write("array[" + i + "] = " + array[i]); if (even == true) { Console.WriteLine(" is even"); evenSum += array[i]; evencount++; } else { Console.WriteLine(" is odd"); oddSum += array[i]; oddcount ++; } if (even == true && array[i] > evenmax) { evenmax = array[i]; } if (even == true && array[i] < evenmin) { evenmin = array[i]; } if (even == false && array[i] > oddmax) { oddmax = array[i]; } if (even == false && array[i] < oddmin) { oddmin = array[i]; } } Console.WriteLine("Avarage of Odds: " + oddSum/oddcount); Console.WriteLine("Avarage of Evens: " + evenSum/evencount); Console.WriteLine("Sum of Odds: " + oddSum); Console.WriteLine("Sum of Evens: " + evenSum); Console.WriteLine("Maximum even: " + evenmax); Console.WriteLine("Minimum even: " + evenmin); Console.WriteLine("Maximum odd: " + oddmax); Console.WriteLine("Minimum odd: " + oddmin); Console.ReadKey(); } } } Код using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp7 { class Program { static void Main(string[] args) { Random r = new Random(); int[] array = new int[10]; double evenSum = 0; double oddSum = 0; double oddcount = 0; double evencount = 0; int oddmax = array[0]; int oddmin = array[0]; int evenmax = array[0]; int evenmin = array[0]; for (int i = 0; i < array.Length; i++) { array[i] = r.Next(100); bool? even = null; if (array[i] % 2 == 0) { even = true; } else if (array[i] % 2 == 1) { even = false; } else { even = null; } Console.Write("array[" + i + "] = " + array[i]); if (even == true) { Console.WriteLine(" is even"); evenSum += array[i]; evencount++; } else { Console.WriteLine(" is odd"); oddSum += array[i]; oddcount ++; } if (even == true && array[i] > evenmax) { evenmax = array[i]; } if (even == true && array[i] < evenmin) { evenmin = array[i]; } if (even == false && array[i] > oddmax) { oddmax = array[i]; } if (even == false && array[i] < oddmin) { oddmin = array[i]; } } Console.WriteLine("Avarage of Odds: " + oddSum/oddcount); Console.WriteLine("Avarage of Evens: " + evenSum/evencount); Console.WriteLine("Sum of Odds: " + oddSum); Console.WriteLine("Sum of Evens: " + evenSum); Console.WriteLine("Maximum even: " + evenmax); Console.WriteLine("Minimum even: " + evenmin); Console.WriteLine("Maximum odd: " + oddmax); Console.WriteLine("Minimum odd: " + oddmin); Console.ReadKey(); } } } Сделал так но пишет что "Minimum odd : 0" "Minimum even : 0" (Минимум всегда 0) что делать?