
Использование массивов в качестве аргументов и возвращаемых значений
После создания массив можно передавать как аргумент или получать в виде возвращаемого значения функции.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace mass
{
class Program
{
const int maxSize=100;
static void RandomFormArray(out int size, out int[] arr)
{
int i;
Random rnd = new Random();
do
{
Console.WriteLine("Введите количество элементов массива");
size = Convert.ToInt32(Console.ReadLine());
} while (size < 0 || size > maxSize);
arr = new int[size];
for (i = 0; i < size; i++)
arr[i] = rnd.Next(-100, 100);
Console.WriteLine("Массив создан");
}
static int[] RandomFormArray(out int size)
{
int i;
Random rnd = new Random();
do
{
Console.WriteLine("Введите количество элементов массива");
size = Convert.ToInt32(Console.ReadLine());
} while (size < 0 || size > maxSize);
int[] arr = new int[size];
for (i = 0; i < size; i++)
arr[i] = rnd.Next(-100, 100);
Console.WriteLine("Массив создан");
return arr;
}
static void PrintMas(int size, int[] arr)
{
foreach (int elem in arr)
Console.Write(elem.ToString() + " ");
Console.WriteLine();
}
static void Main(string[] args)
{
int size;
int[] arr1 = RandomFormArray(out size);
PrintMas(size, arr1);
int []arr2;
RandomFormArray(out size, out arr2);
PrintMas(size, arr2);
}
}
}