Индексатор для проверки выхода массива за границы - C#
Формулировка задачи:
Помогите сделать индексатор, временно сделал просто catch, не могу разобраться.
Правил программу по требованию в этой теме (может чего забыл, посмотрите пожалуйста)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Laba8
{
class Work
{
static private int length;
static private int begin;
static private int theEnd;
int[] Array;
public int Length
{
get { return length; }
set { length = value; }
}
public int Begin
{
get { return begin; }
set { begin = value; }
}
public int TheEnd
{
get { return theEnd; }
set { theEnd = value; }
}
public Work()
{
//констурктор без параметров
}
public Work(int length, int begin, int theEnd)
{
this.Length = length;
this.Begin = begin;
this.TheEnd = theEnd;
}
public void PrintAllArray(int[] Array, int Lenght, int Begin, int End)
{
Random R = new Random();
for (int i = 0; i < Length; i++)
{
Array[i] = R.Next(Begin, TheEnd);
Console.Write("{0} ", Array[i]);
}
Console.ReadLine();
}
static public void SumArray(int[] Array, int[] b, int x)
{
int[] Copy = new int[x];
for (int i = 0; i < length; i++)
{
Copy[i] = Array[i] + b[i];
Console.Write("{0} ", Copy[i]);
}
}
static public void DiffArray(int[] Array, int[] b, int x)
{
int[] Copy = new int[x];
for (int i = 0; i < length; i++)
{
Copy[i] = Array[i] - b[i];
Console.Write("{0} ", Copy[i]);
}
}
public void MultArrayOnScalar(int[] a, int temp, int x)
{
int[] Copy = new int[x];
for (int i = 0; i < length; i++)
{
Copy[i] = a[i] * temp;
Console.Write("{0} ", Copy[i]);
}
}
public void DelArrayOnScalar(int[] a, int temp, int x)
{
double[] Copy = new double[x];
for (int i = 0; i < length; i++)
{
Copy[i] = Convert.ToDouble(a[i]) / temp;
Console.Write("{0} ", Copy[i]);
}
}
}
class Program
{
static void Main(string[] args)
{
Work one = new Work(10, -10, 3);
Work two = new Work(6, -3, 5);
int[] One = new int[one.Length];
int[] Two = new int[two.Length];
Console.WriteLine("Первый массив:");
one.PrintAllArray(One, one.Length, one.Begin, one.TheEnd);
Console.WriteLine("Второй массив:");
two.PrintAllArray(Two, two.Length, two.Begin, two.TheEnd);
try
{
Console.Write("Индекс элемента = ");
int i = Int32.Parse(Console.ReadLine());
Console.WriteLine("One[{0}]={1}", i, One[i]);
}
catch (FormatException){ Console.WriteLine("Неверный формат"); }
catch { Console.WriteLine("Выход за границы массива"); } // примитивная проверка на выход за границы
Console.WriteLine("Поэлементная сумма:");
Work.SumArray(One, Two, One.Length);
Console.WriteLine();
Console.WriteLine("Поэлементная разность:");
Work.DiffArray(One, Two, One.Length);
Console.WriteLine();
Console.WriteLine("Уножение на скаляр:");
one.MultArrayOnScalar(One, 2, One.Length);
Console.WriteLine();
Console.WriteLine("Деление на скаляр:");
one.DelArrayOnScalar(One, 2, One.Length);
Console.WriteLine();
}
}
}Решение задачи: «Индексатор для проверки выхода массива за границы»
textual
Листинг программы
public int length
{
get { return array.Length; }
}