Дана целочисленная квадратная матрица, получить вектор - C#
Формулировка задачи:
Дана целочисленная квадратная матрица. Получить вектор В , где bi значение первого попорядку положительного элемента i-той строки (если таких элементов нет то принять bi=1);
Решение задачи: «Дана целочисленная квадратная матрица, получить вектор»
textual
Листинг программы
- using System;
- using System.Linq;
- namespace ConsoleApplication3
- {
- class Program
- {
- static void Print(int[,] array)
- {
- int countRows = array.GetLength(0);
- int countColumns = array.GetLength(1);
- Console.WriteLine();
- for (int i = 0; i < countRows; i++)
- {
- for (int j = 0; j < countColumns; j++)
- {
- Console.Write(string.Format("{0}\t",array[i,j]));
- }
- Console.WriteLine();
- }
- }
- static void Print(int[] array)
- {
- int countColumns = array.GetLength(0);
- Console.WriteLine();
- for (int j = 0; j < countColumns; j++)
- {
- Console.Write(string.Format("{0}\t", array[j]));
- }
- }
- static int GetFirstPositiveElement(int[,] array, int rows)
- {
- int b = 1;
- int countRows = array.GetLength(0);
- int countColumns = array.GetLength(1);
- if (rows < countRows)
- {
- for (int i = 0; i < countColumns; i++)
- {
- if (array[rows, i] > 0)
- {
- b = array[rows, i];
- break;
- }
- }
- }
- return b;
- }
- static void Main(string[] args)
- {
- Random rnd= new Random();
- Console.WriteLine("Enter N:");
- int n = int.Parse(Console.ReadLine());
- int[,] array= new int[n,n];
- int[] b= new int[n];
- for(int i=0;i<n;i++)
- for (int j = 0; j < n; j++)
- array[i, j] = rnd.Next(-100, 100);
- Print(array);
- for (int i = 0; i < n; i++)
- {
- b[i] = GetFirstPositiveElement(array, i);
- }
- Print(b);
- Console.ReadKey();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д