There is a class representing an array. It is necessary for it to overload the operator of the remainder of division so that the output will be an array of the residuals from dividing each of the elements of the array by the specified number.
Abbreviated version of the code displays errors:
- A non-static field, method, or property "Array.length" requires an object reference.
- Cannot apply indexing via [] to an expression of type "Array"
namespace array { class Array { public bool errFlag; int [,] arr; int length; public Array(int length) { this.length = length; arr = new int[length, length]; Console.WriteLine(); } public static Array operator %(Array ob1, int ob2) { for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { ob1[i, j] = ob1[i, j] % ob2; } } return ob1; } public void SetArr() { Console.WriteLine("Заполните массив данными"); for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { Console.Write("Введите значение элемента массива {0}-{1}: ", i + 1, j + 1); arr[i, j] = int.Parse(Console.ReadLine()); } } } public void GetArr() { Console.WriteLine("Значения элементов массива:"); for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { Console.Write(arr[i, j] + "\t"); } Console.WriteLine(); } } } class Program { static void Main(string[] args) { Console.Write("Введите размер массивов с которыми предстоит работать: "); int length = int.Parse(Console.ReadLine()); Array firstArr = new Array(length); firstArr.SetArr(); firstArr.GetArr(); Console.Write("Введите число, с помощью которого будут найдены остатки от деления всех элементов массива : "); int x = int.Parse(Console.ReadLine()); firstArr = firstArr % x; Console.WriteLine("Новый массив, который состоит из остатков от деления"); firstArr.GetArr(); } } }
ob1
, but not try to pull it out of the class. In general, attach the entire class, it is not clear what theArray
. - LLENNpublic static Array operator %(Array ob1, int ob2)
methodpublic static Array operator %(Array ob1, int ob2)
trying to use thelength
field of the instance, so you get errors - tym32167