Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Program
- {
- static int FindNegativesInStroke(double[,] a, int i)
- {
- int negatives = 0;
- for (int j = 0; j < a.GetLength(dimension: 1); j++)
- {
- if (a[i, j] < 0)
- negatives++;
- }
- return negatives;
- }
- static void WriteStroke(double[,] a, int i)
- {
- for (int j = 0; j < a.GetLength(1); j++)
- Console.Write("{0,6}", a[i, j]);
- Console.WriteLine();
- }
- static void SortByNegatives(double[,] a)
- {
- Dictionary<int, int> arrayDictionary = new Dictionary<int, int>();
- for (int i = 0; i < a.GetLength(dimension: 0); i++)
- {
- int negatives = FindNegativesInStroke(a, i);
- arrayDictionary.Add(i, negatives);
- }
- Console.WriteLine("Кол-во отриц. чисел в каждой строкe");
- foreach (KeyValuePair<int,int> keyValue in arrayDictionary)
- {
- Console.WriteLine($"{keyValue.Key} - {keyValue.Value} чисел");
- }
- var myList = arrayDictionary.ToList();
- myList.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value));
- Console.WriteLine();
- foreach (KeyValuePair<int, int> keyValue in myList)
- {
- Console.WriteLine($"{keyValue.Key} - {keyValue.Value} чисел");
- }
- Console.WriteLine("Новая матрица");
- foreach (KeyValuePair<int, int> keyValue in myList)
- {
- WriteStroke(a, keyValue.Key);
- }
- }
- static void CreateMatrix(ref double[,] a)
- {
- Random r = new Random();
- Console.WriteLine("\nисходная матрица");
- for (int i = 0; i < a.GetLength(dimension: 0); i++)
- {
- for (int j = 0; j < a.GetLength(dimension: 1); j++)
- {
- a[i, j] = r.Next(-4, 5);
- Console.Write("{0,6}", a[i, j]);
- }
- Console.WriteLine();
- }
- }
- static void Main(string[] args)
- {
- // Расположить строки матрицы в порядке возрастания количества отрицательных элементов.
- // Для нахождения количества отрицательных элементов строки использовать метод.
- double[,] array = new double[5,6];
- CreateMatrix(ref array);
- SortByNegatives(array);
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment