Last active
June 11, 2023 13:19
-
-
Save gmakc-094423/113805976de7f98582f2c6d490703b4e to your computer and use it in GitHub Desktop.
Домашнее задание к 8 семинару
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Задача 54: Задайте двумерный массив. Напишите программу, которая упорядочит по убыванию элементы каждой строки двумерного массива. | |
Console.Clear(); | |
Console.WriteLine($"Задача 54: Задайте двумерный массив. Напишите программу, которая упорядочит по убыванию элементы каждой строки двумерного массива."); | |
Console.WriteLine($"\nВведите размер массива m x n и диапазон случайных значений:"); | |
int m = InputNumbers("Введите m: "); | |
int n = InputNumbers("Введите n: "); | |
int range = InputNumbers("Введите диапазон: от 1 до "); | |
int[,] array = new int[m, n]; | |
CreateArray(array); | |
WriteArray(array); | |
Console.WriteLine($"\nОтсортированный массив: "); | |
OrderArrayLines(array); | |
WriteArray(array); | |
void OrderArrayLines(int[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
for (int k = 0; k < array.GetLength(1) - 1; k++) | |
{ | |
if (array[i, k] < array[i, k + 1]) | |
{ | |
int temp = array[i, k + 1]; | |
array[i, k + 1] = array[i, k]; | |
array[i, k] = temp; | |
} | |
} | |
} | |
} | |
} | |
int InputNumbers(string input) | |
{ | |
Console.Write(input); | |
int output = Convert.ToInt32(Console.ReadLine()); | |
return output; | |
} | |
void CreateArray(int[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
array[i, j] = new Random().Next(range); | |
} | |
} | |
} | |
void WriteArray(int[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
Console.Write(array[i, j] + " "); | |
} | |
Console.WriteLine(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Задача 56: Задайте прямоугольный двумерный массив. Напишите программу, которая будет находить строку с наименьшей суммой элементов. | |
Console.Clear(); | |
Console.WriteLine($"Задача 56: Задайте прямоугольный двумерный массив. Напишите программу, которая будет находить строку с наименьшей суммой элементов."); | |
Console.WriteLine($"\nВведите размер массива m x n и диапазон случайных значений:"); | |
int m = InputNumbers("Введите m: "); | |
int n = InputNumbers("Введите n: "); | |
int range = InputNumbers("Введите диапазон: от 1 до "); | |
int[,] array = new int[m, n]; | |
CreateArray(array); | |
WriteArray(array); | |
int minSumLine = 0; | |
int sumLine = SumLineElements(array, 0); | |
for (int i = 1; i < array.GetLength(0); i++) | |
{ | |
int tempSumLine = SumLineElements(array, i); | |
if (sumLine > tempSumLine) | |
{ | |
sumLine = tempSumLine; | |
minSumLine = i; | |
} | |
} | |
Console.WriteLine($"\n{minSumLine+1} - строкa с наименьшей суммой ({sumLine}) элементов "); | |
int SumLineElements(int[,] array, int i) | |
{ | |
int sumLine = array[i,0]; | |
for (int j = 1; j < array.GetLength(1); j++) | |
{ | |
sumLine += array[i,j]; | |
} | |
return sumLine; | |
} | |
int InputNumbers(string input) | |
{ | |
Console.Write(input); | |
int output = Convert.ToInt32(Console.ReadLine()); | |
return output; | |
} | |
void CreateArray(int[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
array[i, j] = new Random().Next(range); | |
} | |
} | |
} | |
void WriteArray (int[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
Console.Write(array[i,j] + " "); | |
} | |
Console.WriteLine(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Задача 58: Задайте две матрицы. Напишите программу, которая будет находить произведение двух матриц. | |
Console.Clear(); | |
Console.WriteLine($"Задача 58: Задайте две матрицы. Напишите программу, которая будет находить произведение двух матриц.\n\nСразу зададим матрицу, которую можно перемножить, т.е. количество столбцов первой равно количеству строк второй"); | |
Console.WriteLine($"\nВведите размеры матриц и диапазон случайных значений:"); | |
int m = InputNumbers("Введите число строк 1-й матрицы: "); | |
int n = InputNumbers("Введите число столбцов 1-й матрицы (и строк 2-й): "); | |
int p = InputNumbers("Введите число столбцов 2-й матрицы: "); | |
int range = InputNumbers("Введите диапазон случайных чисел: от 1 до "); | |
int[,] firstMartrix = new int[m, n]; | |
CreateArray(firstMartrix); | |
Console.WriteLine($"\nПервая матрица:"); | |
WriteArray(firstMartrix); | |
int[,] secomdMartrix = new int[n, p]; | |
CreateArray(secomdMartrix); | |
Console.WriteLine($"\nВторая матрица:"); | |
WriteArray(secomdMartrix); | |
int[,] resultMatrix = new int[m,p]; | |
MultiplyMatrix(firstMartrix, secomdMartrix, resultMatrix); | |
Console.WriteLine($"\nПроизведение первой и второй матриц:"); | |
WriteArray(resultMatrix); | |
void MultiplyMatrix(int[,] firstMartrix, int[,] secomdMartrix, int[,] resultMatrix) | |
{ | |
for (int i = 0; i < resultMatrix.GetLength(0); i++) | |
{ | |
for (int j = 0; j < resultMatrix.GetLength(1); j++) | |
{ | |
int sum = 0; | |
for (int k = 0; k < firstMartrix.GetLength(1); k++) | |
{ | |
sum += firstMartrix[i,k] * secomdMartrix[k,j]; | |
} | |
resultMatrix[i,j] = sum; | |
} | |
} | |
} | |
int InputNumbers(string input) | |
{ | |
Console.Write(input); | |
int output = Convert.ToInt32(Console.ReadLine()); | |
return output; | |
} | |
void CreateArray(int[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
array[i, j] = new Random().Next(range); | |
} | |
} | |
} | |
void WriteArray (int[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
Console.Write(array[i,j] + " "); | |
} | |
Console.WriteLine(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Задача 59: Задайте двумерный массив из целых чисел. Напишите программу, которая удалит строку и столбец, на пересечении которых расположен наименьший элемент массива. | |
// Например, задан массив: | |
// 1 4 7 2 | |
// 5 9 2 3 | |
// 8 4 2 4 | |
// 5 2 6 7 | |
// Наименьший элемент - 1, на выходе получим | |
// следующий массив: | |
// 9 4 2 | |
// 2 2 6 | |
// 3 4 7 | |
Console.Clear(); | |
Console.WriteLine($"Задача 59: Задайте двумерный массив из целых чисел. Напишите программу, которая удалит строку и столбец, на пересечении которых расположен наименьший элемент массива."); | |
Console.WriteLine($"\nВведите размер массива m x n и диапазон случайных значений:"); | |
int m = InputNumbers("Введите m: "); | |
int n = InputNumbers("Введите n: "); | |
int range = InputNumbers("Введите диапазон: от 1 до "); | |
int[,] array = new int[m, n]; | |
CreateArray(array); | |
WriteArray(array); | |
int[,] positionSmallElement = new int[1, 2]; | |
positionSmallElement = FindPositionSmallElement(array, positionSmallElement); | |
Console.Write($"Позиция элемента: \n"); | |
WriteArray(positionSmallElement); | |
int[,] arrayWithoutLines = new int[array.GetLength(0) - 1, array.GetLength(1) - 1]; | |
DeleteLines(array, positionSmallElement, arrayWithoutLines); | |
Console.WriteLine($"\nПолучившийся массив:"); | |
WriteArray(arrayWithoutLines); | |
int InputNumbers(string input) | |
{ | |
Console.Write(input); | |
int output = Convert.ToInt32(Console.ReadLine()); | |
return output; | |
} | |
void CreateArray(int[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
array[i, j] = new Random().Next(range); | |
} | |
} | |
} | |
void WriteArray(int[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
Console.Write(array[i, j] + " "); | |
} | |
Console.WriteLine(); | |
} | |
} | |
int[,] FindPositionSmallElement(int[,] array, int[,] position) | |
{ | |
int temp = array[0, 0]; | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
if (array[i, j] <= temp) | |
{ | |
position[0, 0] = i; | |
position[0, 1] = j; | |
temp = array[i, j]; | |
} | |
} | |
} | |
Console.WriteLine($"\nMинимальный элемент: {temp}"); | |
return position; | |
} | |
void DeleteLines(int[,] array, int[,] positionSmallElement, int[,] arrayWithoutLines) | |
{ | |
int k = 0, l = 0; | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
if (positionSmallElement[0, 0] != i && positionSmallElement[0, 1] != j) | |
{ | |
arrayWithoutLines[k, l] = array[i, j]; | |
l++; | |
} | |
} | |
l = 0; | |
if (positionSmallElement[0, 0] != i) | |
{ | |
k++; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Задача 60: Сформируйте трёхмерный массив из неповторяющихся двузначных чисел. Напишите программу, которая будет построчно выводить массив, добавляя индексы каждого элемента. | |
Console.Clear(); | |
Console.WriteLine($"Задача 60: Сформируйте трёхмерный массив из неповторяющихся двузначных чисел. Напишите программу, которая будет построчно выводить массив, добавляя индексы каждого элемента."); | |
Console.WriteLine($"\nВведите размер массива X x Y x Z:"); | |
int x = InputNumbers("Введите X: "); | |
int y = InputNumbers("Введите Y: "); | |
int z = InputNumbers("Введите Z: "); | |
Console.WriteLine($""); | |
int[,,] array3D = new int[x, y, z]; | |
CreateArray(array3D); | |
WriteArray(array3D); | |
int InputNumbers(string input) | |
{ | |
Console.Write(input); | |
int output = Convert.ToInt32(Console.ReadLine()); | |
return output; | |
} | |
void WriteArray (int[,,] array3D) | |
{ | |
for (int i = 0; i < array3D.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array3D.GetLength(1); j++) | |
{ | |
Console.Write($"X({i}) Y({j}) "); | |
for (int k = 0; k < array3D.GetLength(2); k++) | |
{ | |
Console.Write( $"Z({k})={array3D[i,j,k]}; "); | |
} | |
Console.WriteLine(); | |
} | |
Console.WriteLine(); | |
} | |
} | |
void CreateArray(int[,,] array3D) | |
{ | |
int[] temp = new int[array3D.GetLength(0) * array3D.GetLength(1) * array3D.GetLength(2)]; | |
int number; | |
for (int i = 0; i < temp.GetLength(0); i++) | |
{ | |
temp[i] = new Random().Next(10, 100); | |
number = temp[i]; | |
if (i >= 1) | |
{ | |
for (int j = 0; j < i; j++) | |
{ | |
while (temp[i] == temp[j]) | |
{ | |
temp[i] = new Random().Next(10, 100); | |
j = 0; | |
number = temp[i]; | |
} | |
number = temp[i]; | |
} | |
} | |
} | |
int count = 0; | |
for (int x = 0; x < array3D.GetLength(0); x++) | |
{ | |
for (int y = 0; y < array3D.GetLength(1); y++) | |
{ | |
for (int z = 0; z < array3D.GetLength(2); z++) | |
{ | |
array3D[x, y, z] = temp[count]; | |
count++; | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Задача 62: Заполните спирально массив 4 на 4. | |
// 1 2 3 4 | |
// 12 13 14 5 | |
// 11 16 15 6 | |
// 10 9 8 7 | |
Console.Clear(); | |
Console.WriteLine($"Задача 62: Заполните спирально массив 4 на 4."); | |
int n = 4; | |
int[,] sqareMatrix = new int[n, n]; | |
int temp = 1; | |
int i = 0; | |
int j = 0; | |
while (temp <= sqareMatrix.GetLength(0) * sqareMatrix.GetLength(1)) | |
{ | |
sqareMatrix[i, j] = temp; | |
temp++; | |
if (i <= j + 1 && i + j < sqareMatrix.GetLength(1) - 1) | |
j++; | |
else if (i < j && i + j >= sqareMatrix.GetLength(0) - 1) | |
i++; | |
else if (i >= j && i + j > sqareMatrix.GetLength(1) - 1) | |
j--; | |
else | |
i--; | |
} | |
WriteArray(sqareMatrix); | |
void WriteArray (int[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
if (array[i,j] / 10 <= 0) | |
Console.Write($" {array[i,j]} "); | |
else Console.Write($"{array[i,j]} "); | |
} | |
Console.WriteLine(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Дополнительная задача 61: Вывести первые N строк треугольника Паскаля. Сделать вывод в виде равнобедренного треугольника | |
Console.Clear(); | |
Console.WriteLine($"Дополнительная задача 61: Вывести первые N строк треугольника Паскаля. Сделать вывод в виде равнобедренного треугольника."); | |
int n = InputNumbers("Введите количество строк: "); | |
double[,] pascalTriangle = new double[n + 1, 2 * n + 1]; | |
FillPascalTriangle(pascalTriangle); | |
Console.WriteLine(); | |
WriteArray(pascalTriangle); | |
TransformationPascalTriangle(pascalTriangle); | |
Console.WriteLine(); | |
WriteArray(pascalTriangle); | |
void TransformationPascalTriangle(double[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
int count = 0; | |
for (int j = array.GetLength(1) - 1; j >= 0; j--) | |
{ | |
if (array[i, j] != 0) | |
{ | |
array[i, array.GetLength(1) / 2 + j - count] = array[i, j]; | |
array[i, j] = 0; | |
count++; | |
} | |
} | |
} | |
array[array.GetLength(0) - 1, 0] = 1; | |
} | |
void FillPascalTriangle(double[,] pascalTriangle) | |
{ | |
for (int k = 0; k < pascalTriangle.GetLength(0); k++) | |
{ | |
pascalTriangle[k, 0] = 1; | |
} | |
for (int i = 1; i < pascalTriangle.GetLength(0); i++) | |
{ | |
for (int j = 1; j < i + 1; j++) | |
{ | |
pascalTriangle[i, j] = pascalTriangle[i - 1, j] + pascalTriangle[i - 1, j - 1]; | |
} | |
} | |
} | |
void WriteArray(double[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array.GetLength(1); j++) | |
{ | |
if (array[i, j] != 0) | |
{ | |
Console.Write($"{array[i, j]} "); | |
} | |
else Console.Write(" "); | |
} | |
Console.WriteLine(); | |
} | |
} | |
int InputNumbers(string input) | |
{ | |
Console.Write(input); | |
int output = Convert.ToInt32(Console.ReadLine()); | |
return output; | |
} |
спасибо
Огромное спасибо!
огромная благодарность!!
Большое спасибо!!!
Thank You!
Спасибище!!!
You are so kind!!! Thank's!
Большое спасибо, узнал много интересного и нового
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Спасибо!!!