Найти в матрице первую строку, все элементы которой отрицательны. - C#
Формулировка задачи:
Условие: Найти в матрице первую строку, все элементы которой отрицательны. Уменьшить все элементы матрицы на значение первого элемента найденной строки. Выполнить с помощью динамического массива.
Вот что я смог сделать:
Исправьте кто может - я уже битый час не могу понять в чем ошибка ,да и с динамическими массивами еще пару часов назад был не знаком.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using zad2_1;
namespace zad2_1
{
public static class Program
{
private static void Main()
{
Console.WriteLine("Введите размерность");
int n = Convert.ToInt32(Console.ReadLine());
int m = Convert.ToInt32(Console.ReadLine());
int [,]mas=new int[n,m];
for (int i = 0; i<n;i++)
{
for (int j = 0; j < m; j++)
{
Console.Write("Введите элемент " + (i+1) + " " + (j+1) + ": ");
mas[i, j] = int.Parse(Console.ReadLine());
}
}
var matrix = new Matrix<int>(mas);
Console.WriteLine(matrix.ToString());
Console.ReadKey();
}
}
public class Matrix<T> where T : new()
{
private readonly List<List<T>> _matrix;
public Matrix(T[,] data)
{
bool p = false;
int s = 0;
int el = 0;
p = true;
RowsCount = data.GetLength(0);
ColumnCount = data.GetLength(1);
_matrix = new List<List<T>>(RowsCount);
for (int i = 0; i < RowsCount; i++)
{
var list = new List<T>(ColumnCount);
for (int j = 0; j < ColumnCount; j++)
list.Add(data[i, j]);
_matrix.Add(list);
}
for (int i = 0; i < RowsCount; i++)
{
for (int j = 0; j < ColumnCount; j++)
{
if ((int)data.GetValue(i,j) > 0) p = false;
}
if (p == true)
{
s = i;
el = (int)data.GetValue(i, 0);
break;
}
}
if (p == false)
{
Console.WriteLine("Такой строки не найдено");
}
else
{
Console.WriteLine("Строка №{0}", s + 1);
}
int h=0;
for (int i = 0; i < RowsCount; i++)
{
for (int j = 0; j < ColumnCount; j++)
{
h=(int)data.GetValue(i, j) - (int)data.GetValue(i,0);
}
}
for (int i = 0; i < RowsCount; i++)
{
for (int j = 0; j < ColumnCount; j++)
{
Console.Write((int)h + "\t");
}
Console.WriteLine();
}
//var list1 = new List<T>();
//list1.Add((int)data.GetValue(i, j));
//_matrix.Add(list1);
}
public T this[int i, int j]
{
get { return _matrix[i][j]; }
set { _matrix[i][j] = value; }
}
public int RowsCount { get; private set; }
public int ColumnCount { get; private set; }
public override string ToString()
{
var stringBuilder = new StringBuilder();
for (int i = 0; i < RowsCount; i++)
{
for (int j = 0; j < ColumnCount; j++)
{
stringBuilder.Append(_matrix[i][j]);
stringBuilder.Append(" ");
}
stringBuilder.AppendLine();
}
return stringBuilder.ToString();
}
}
}
Все, разобрался - помощь больше не нужна..
Вот код если кому пригодиться, может поможет:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using zad2_1;
namespace zad2_1
{
public static class Program
{
private static void Main()
{
Console.WriteLine("Введите размерность");
int n = Convert.ToInt32(Console.ReadLine());
int m = Convert.ToInt32(Console.ReadLine());
int [,]mas=new int[n,m];
for (int i = 0; i<n;i++)
{
for (int j = 0; j < m; j++)
{
Console.Write("Введите элемент " + (i+1) + " " + (j+1) + ": ");
mas[i, j] = int.Parse(Console.ReadLine());
}
}
var matrix = new Matrix<int>(mas);
Console.WriteLine(matrix.ToString());
Console.ReadKey();
}
}
public class Matrix<T> where T : new()
{
private readonly List<List<T>> _matrix;
public Matrix(T[,] data)
{
bool p = false;
int s = 0;
int el = 0;
p = true;
Console.WriteLine("полученный массив: ");
RowsCount = data.GetLength(0);
ColumnCount = data.GetLength(1);
_matrix = new List<List<T>>(RowsCount);
for (int i = 0; i < RowsCount; i++)
{
var list = new List<T>(ColumnCount);
for (int j = 0; j < ColumnCount; j++)
list.Add(data[i, j]);
_matrix.Add(list);
}
for (int i = 0; i < RowsCount; i++)
{
p = true;
for (int j = 0; j < ColumnCount; j++)
{
if ((int)data.GetValue(i,j) > 0) p = false;
}
if (p == true)
{
s = i;
el = (int)data.GetValue(i, 0);
break;
}
}
if (p == false)
{
Console.WriteLine("Такой строки не найдено");
}
else
{
Console.WriteLine("Строка №{0}", s + 1);
}
for (int i = 0; i < RowsCount; i++)
{
for (int j = 0; j < ColumnCount; j++)
{
if ((int)data.GetValue(i, j) < 0)
{
data.SetValue((int)data.GetValue(i, j) + el, i, j);
}
else if ((int)data.GetValue(i, j) > 0)
{
data.SetValue((int)data.GetValue(i, j) + el, i, j);
}
}
}
for (int i = 0; i < RowsCount; i++)
{
for (int j = 0; j < ColumnCount; j++)
{
Console.Write((int)data.GetValue(i, j) + "\t");
}
Console.WriteLine();
}
Console.WriteLine("Исходный массив: ");
}
public T this[int i, int j]
{
get { return _matrix[i][j]; }
set { _matrix[i][j] = value; }
}
public int RowsCount { get; private set; }
public int ColumnCount { get; private set; }
public override string ToString()
{
var stringBuilder = new StringBuilder();
for (int i = 0; i < RowsCount; i++)
{
for (int j = 0; j < ColumnCount; j++)
{
stringBuilder.Append(_matrix[i][j]);
stringBuilder.Append(" ");
}
stringBuilder.AppendLine();
}
return stringBuilder.ToString();
}
}
}Решение задачи: «Найти в матрице первую строку, все элементы которой отрицательны.»
textual
Листинг программы
var d = matrix.FirstOrDefault(e => e.All(n => n < 0));
if (d != null)
{
foreach (List<int> i in matrix)
{
for (int k = 0; k < i.Count; k++)
i[k] -= d[0];
}
}