Skip to content

Instantly share code, notes, and snippets.

@bannovGB
Created September 6, 2022 12:35
Show Gist options
  • Save bannovGB/23a7bb20104492fb76e09c02e1d15fd7 to your computer and use it in GitHub Desktop.
Save bannovGB/23a7bb20104492fb76e09c02e1d15fd7 to your computer and use it in GitHub Desktop.
47-52
/*Задача 47. Задайте двумерный массив размером m×n, заполненный случайными вещественными числами.
m = 3, n = 4.
0,5 7 -2 -0,2
1 -3,3 8 -9,9
8 7,8 -7,1 9 */
int rows = ReadInt("Введите количество строк: ");
int colums = ReadInt("Введите количество столбцов: ");
double[,] numbers = new double[rows, colums];
FillArray2D(numbers);
PrintArray2D(numbers);
void FillArray2D(double[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)//GetLength(0) - строки
{
for (int j = 0; j < array.GetLength(1); j++)//GetLength(1) - столбцы
{
array[i, j] = new Random().NextDouble();
}
}
}
void PrintArray2D(double[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)//GetLength(0) - строки
{
for (int j = 0; j < array.GetLength(1); j++)//GetLength(1) - столбцы
{
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
int ReadInt(string message)
{
Console.Write(message);
return Convert.ToInt32(Console.ReadLine());
}
/*Задача 50. Напишите программу, которая на вход принимает позиции элемента в двумерном массиве, и возвращает значение этого элемента или же указание, что такого элемента нет. Во вводе первая цифра - номер строки, вторая - столбца. Цифры не больше 9.
Например, задан массив:
1 4 7 2
5 9 2 3
8 4 2 4
17 -> такого числа в массиве нет */
int rows = ReadInt("Введите количество строк: ");
int colums = ReadInt("Введите количество столбцов: ");
int[,] numbers = new int[rows, colums];
int idRows = ReadInt("Введите строку: ");
int idColums = ReadInt("Введите столбец: ");
FillArray2D(numbers);
PrintArray2D(numbers);
if ((idRows < rows)&(idColums < colums))
{
Console.WriteLine(numbers[idRows, idColums]);
}
else
{
Console.WriteLine("Такого числа в массиве нет");
}
void FillArray2D(int[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)//GetLength(0) - строки
{
for (int j = 0; j < array.GetLength(1); j++)//GetLength(1) - столбцы
{
array[i, j] = new Random().Next(-9, 9);
}
}
}
void PrintArray2D(int[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)//GetLength(0) - строки
{
for (int j = 0; j < array.GetLength(1); j++)//GetLength(1) - столбцы
{
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
int ReadInt(string message)
{
Console.Write(message);
return Convert.ToInt32(Console.ReadLine());
}
/* Задача 52. Задайте двумерный массив из целых чисел. Найдите среднее арифметическое элементов в каждом столбце.
Например, задан массив:
1 4 7 2
5 9 2 3
8 4 2 4 Среднее арифметическое каждого столбца: 4,6; 5,6; 3,6; 3. */
int rows = ReadInt("Введите количество строк: ");
int colums = ReadInt("Введите количество столбцов: ");
int[,] numbers = new int[rows, colums];
FillArray2D(numbers);
PrintArray2D(numbers);
double sum = 0;
double result = 0;
for (int j = 0; j < numbers.GetLength(1); j++)
{
for (int i = 0; i < numbers.GetLength(1); i++)
{
sum += numbers[i, j];
}
result = sum / numbers.GetLength(1);
result = Math.Round(result, 2);
Console.Write(result + "; ");
result = 0;
sum = 0;
}
void FillArray2D(int[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)//GetLength(0) - строки
{
for (int j = 0; j < array.GetLength(1); j++)//GetLength(1) - столбцы
{
array[i, j] = new Random().Next(0, 9);
}
}
}
void PrintArray2D(int[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)//GetLength(0) - строки
{
for (int j = 0; j < array.GetLength(1); j++)//GetLength(1) - столбцы
{
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
int ReadInt(string message)
{
Console.Write(message);
return Convert.ToInt32(Console.ReadLine());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment