using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class rubes { public double[] arr; public rubes(int n) { arr = new double[n]; } public rubes(int n,Random r) { arr = new double[n]; for (int i = 0; i < n; ++i) { int rhelp = r.Next(-10000, 10000); arr[i] = rhelp/Math.Abs(rhelp)*Math.Pow(i, 2); } } public int getLength { get {return arr.Length;} } public static double summelemIf(double[] a) { double first = double.PositiveInfinity, last = double.NegativeInfinity; for (int i = 0; i < a.Length; ++i) { if (a[i] < 0) { first = i; break; } } for (int i = a.Length-1; i >= 0; --i) { if (a[i] < 0) { last = i; break; } } if (first == last) throw new Exception("В массиве 1 отрицательный элемент"); if (first == double.PositiveInfinity) throw new Exception("В массиве нет отрицательных элементов"); if (first+1 == last) throw new Exception("Первый и последний отрицательные элементы идут сразу друг за другом"); double summ =0; for (int i =(int)first+1; i < last; ++i) { summ += a[i]; } return summ; } public static int Read(string s) { for (byte check = 0; ; ++check) { try { Console.Write(s + " "); double x; string d = Console.ReadLine(); if (double.TryParse(d, out x) && (int)double.Parse(d)==double.Parse(d)) return (int)x; else throw new FormatException("Введенные данные имеют неверный тип"); } catch (FormatException e) { Console.WriteLine(e.Message); if (check == 4) { Console.WriteLine("Проверьте данные и перезапустите программу"); Environment.Exit(0); } continue; } } } public static void PrintArray(string s, double[] a) { Console.WriteLine(s); foreach (double e in a) Console.Write(" " + e); Console.WriteLine("\t"); } } class Program { static void Main(string[] args) { try { rubes a = new rubes((int)rubes.Read("Введите размерность массива A")); rubes.PrintArray("Массив А",a.arr); Random r = new Random(); rubes b = new rubes((int)rubes.Read("Введите размерность массива B"), r); rubes.PrintArray("Массив B", b.arr); Console.WriteLine("Сумма элементов массива В согласно условию " + rubes.summelemIf(b.arr)); Console.WriteLine("Размерность массива В " + b.getLength); } catch (Exception e) { Console.WriteLine(e.Message); } } } }