C#

Написать класс в котором генерируется исключение при делении элементов одного массива на другой размеры которых различны

Using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
class ArrayException : Exception
{
public ArrayException(string message)
: base(message)
{ }
}
class Program
{
public static void Main()
{

int[] a = { 100, 200, 300, 400, 500 };
int[] b = { 15, 20, 25, 30, 35, 40 };
const int datasize = 6;

int[] c = new int[6];

try
{
for (int i = 0; i < datasize; i++)
{
c[i] = a[i] / b[i];
Console.WriteLine(c[i]);
}
}
catch (ArrayException ex)
{
Console.WriteLine("Помилка: " + ex.Message);
}
//System.IndexOutOfRangeException
Console.Read();
}
}
}
Ну тут много всего интересного написано, но по-моему - полная херня, которая никоим образом не соответствует заданию. Сделай статический класс, который умеет делить один массив на другой и выбрасывает исключение, если они не равны по длине.
Антон Гарин
Антон Гарин
84 521
Лучший ответ
using System;
namespace Answer {
class Program {
static private void Main() {
var a = new double[5] { 1, 2, 3, 4, 5 };
Print("a", a);
var b = new double[5] { 2, 3, 4, 5, 6 };
Print("b", b);
var ab = ArrayDivision(a, b);
Print("ab", ab);
Console.WriteLine();
var c = new double[4] { 2, 3, 4, 5 };
Print("c", c);
var ac = ArrayDivision(a, c);
Print("ac", ac);
Console.WriteLine();
var d = new double[4] { 5, 10, 0, 15 };
Print("d", d);
var cd = ArrayDivision(c, d);
Print("cd", cd);
Console.ReadKey();
}
static public void Print(string msg, double[] box) {
Console.Write($"{msg,3}: ");
for (var i = 0; i < box.Length; ++i) {
Console.Write($"{box[i],8:F3}");
}
Console.WriteLine();
}
static public double[] ArrayDivision(double[] a, double[] b) {
var box = new double[a.Length];
Array.Clear(box, 0, box.Length);
try {
if (a.Length != b.Length) {
throw new Exception("размеры массивов не совпадают");
}
for (var i = 0; i < a.Length; ++i) {
try {
if (b[i] == 0) {
throw new Exception("попытка деления на ноль");
}
box[i] = a[i] / b[i];
} catch(Exception ex) {
Console.WriteLine($"Ошибка: {ex.Message}");
box[i] = 0;
}
}
} catch (Exception ex) {
Console.WriteLine($"Ошибка: {ex.Message}");
}
return box;
}
}
}