Автор: mmotop20oo12
Дата сообщения: 08.11.2015 00:00
Здравствуйте. Писал класс, инкапсулирующий массив, создал метод, заполняющий его случайными числами с помощью класс Random. Только вот какая беда, создавая 2 экземпляра класса "массив", метод, заполняющий их, делает это одинаково, т.е. a11 = 5, и b11 = 5, и т.д.
Как можно убрать этот "псевдорандом"? Спасибо
[more]
Код:
using System;
namespace classArray
{
class sizeExeption : ApplicationException
{
public sizeExeption() : base() { }
public sizeExeption(string str) : base(str) { }
public override string ToString()
{
return base.ToString();
}
}
class newArray
{
int[,] arr;
int rows, columns;
public newArray()
{
rows = 1;
columns = 1;
arr = new int[rows, columns];
}
public newArray(int aRows_Columns)
{
rows = Math.Abs(aRows_Columns);
columns = Math.Abs(aRows_Columns);
arr = new int[rows, columns];
}
public newArray(int aRows, int aColumns)
{
rows = Math.Abs(aRows);
columns = Math.Abs(aColumns);
arr = new int[rows, columns];
}
//************** МЕТОД ЗАПОЛНЯЮЩИЙ МАССИВ *****************
public void FillArray()
{
Random rnd = new Random();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
arr[i, j] = rnd.Next(-1, 5);
}
}
}
public virtual void Show()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Console.Write("{0,3}", arr[i, j]);
}
Console.WriteLine();
}
}
static void CheckSizes(newArray aFirstArr, newArray aSecondArr)
{
try
{
if ((aFirstArr.rows != aSecondArr.rows) | (aFirstArr.columns != aSecondArr.columns))
{
throw new sizeExeption("массивы не одинаковы по размерам");
}
}
catch (sizeExeption)
{
throw;
}
}
public static newArray operator +(newArray aFirstArr, newArray aSecondArr)
{
try
{
CheckSizes(aFirstArr, aSecondArr);
newArray temp = new newArray(aFirstArr.rows, aFirstArr.columns);
for (int i = 0; i < aFirstArr.rows; i++)
{
for (int j = 0; j < aFirstArr.columns; j++)
{
temp.arr[i, j] = aFirstArr.arr[i, j] + aFirstArr.arr[i, j];
}
}
return temp;
}
catch (sizeExeption exn)
{
Console.WriteLine(exn.Message);
Console.WriteLine("будет сгенерирована нулевая матрица");
return new newArray(aFirstArr.rows, aSecondArr.columns);
}
}
public static newArray operator -(newArray aFirstArr, newArray aSecondArr)
{
try
{
CheckSizes(aFirstArr, aSecondArr);
newArray temp = new newArray(aFirstArr.rows, aFirstArr.columns);
for (int i = 0; i < aFirstArr.rows; i++)
{
for (int j = 0; j < aFirstArr.columns; j++)
{
temp.arr[i, j] = aFirstArr.arr[i, j] - aFirstArr.arr[i, j];
}
}
return temp;
}
catch (sizeExeption exn)
{
Console.WriteLine(exn.Message);
Console.WriteLine("будет сгенерирована нулевая матрица");
return new newArray(aFirstArr.rows, aSecondArr.columns);
}
}
static int Continue_Multiply(int aI, int aJ, newArray aFirstArr, newArray aSecondArr)
{
int result = 0;
for (int i = 0; i < aFirstArr.rows; i++)
{
result += aFirstArr.arr[aI, i] * aSecondArr.arr[i, aJ];
}
return result;
}
public static newArray operator *(newArray aFirstArr, newArray aSecondArr)
{
try
{
CheckSizes(aFirstArr, aSecondArr);
newArray temp = new newArray(aFirstArr.rows, aFirstArr.columns);
for (int i = 0; i < aFirstArr.rows; i++)
{
for (int j = 0; j < aFirstArr.columns; j++)
{
temp.arr[i, j] = Continue_Multiply(i, j, aFirstArr, aSecondArr);
}
}
return temp;
}
catch (sizeExeption exn)
{
Console.WriteLine(exn.Message);
Console.WriteLine("будет сгенерирована нулевая матрица");
return new newArray(aFirstArr.rows, aSecondArr.columns);
}
}
}
class mainProgramm
{
static void Main(string[] args)
{
newArray arr1 = new newArray(3, 3);
newArray arr2 = new newArray(3, 3);
arr1.FillArray();
arr2.FillArray();
arr1.Show();
Console.WriteLine();
arr2.Show();
Console.WriteLine();
}
}
}