Условие: Найти в матрице первую строку, все элементы которой отрицательны. Уменьшить все элементы матрицы на значение первого элемента найденной строки. Выполнить с помощью динамического массива.
Вот что я смог сделать:
Кликните здесь для просмотра всего текста
C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
| 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();
}
}
} |
|
Исправьте кто может - я уже битый час не могу понять в чем ошибка ,да и с динамическими массивами еще пару часов назад был не знаком.
Добавлено через 45 минут
Все, разобрался - помощь больше не нужна..
Добавлено через 4 часа 8 минут
Вот код если кому пригодиться, может поможет:
Кликните здесь для просмотра всего текста
C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
| 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();
}
}
} |
|