Подсчет положительных и отрицательных элементов массива. Реализовать через класс Array - C#
Формулировка задачи:
using System; namespace ConsoleApplication1 { class Class1 { static void Main() { const int n = 9; int[] a = new int[n] { 3, 12, 5, -9, 8, -4, 7, 8, -2 }; Console.WriteLine("Исходный массив: "); for (int i = 0; i < n; ++i) Console.Write("\t" + a[i]); Console.WriteLine(); int f = 0; int s = 0;int g=0; for (int i = 0; i < n; ++i) if (a[i] < 0) { s++; } for (int i = 0; i < n; ++i) if (a[i] > 0) { ++f; } for (int i = 0; i == n; ++i) if (a[i] >= 0) { g++; } Console.WriteLine("Количество положительных " + f); Console.WriteLine("Количество нулевых " + g); Console.WriteLine("Количество отрицательных " + s); Console.ReadKey(); } } }
Решение задачи: «Подсчет положительных и отрицательных элементов массива. Реализовать через класс Array»
textual
Листинг программы
using System; using System.Collections; class Class1 { static void Main() { const int n = 9; int[] a = new int[n] { 3, 12, 5, -9, 8, -4, 7, 8, -2 }; ArrayList al = new ArrayList(a); int f = 0; int s = 0; int g = 0; Console.WriteLine("Исходный массив: "); foreach (int i in al) { Console.Write("\t" + i); if (i < 0) ++f; else if (i > 0) ++s; else ++g; } Console.WriteLine("\nКоличество положительных " + f); Console.WriteLine("Количество нулевых " + g); Console.WriteLine("Количество отрицательных " + s); Console.ReadKey(); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д