namespace matrix { class Program { static void Main(string[] args) { Random rand = new Random(); Console.WriteLine("Введите размерность матрицы MxM :"); int M = Convert.ToInt32(Console.ReadLine()); int[,] matrix = new int[M, M]; int sum = 0; for (int j = 0; j < M; j++) { for (int m = 0; m < M; m++) { matrix[j, m] = rand.Next(-10, 10); Console.Write(matrix[j, m] + " "); } Console.WriteLine(); if((j + 1) % 2 == 0) { for (int i = 0; i < M; i++) { sum += matrix[j, i]; } } } Console.Write(" Сумма четных строк матрицы = {0}", sum); Console.ReadKey(); } } } Closed due to the fact that Nicolas Chabanovsky is off topic ♦ 27 Sep '16 at 5:07 .
- Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
- I just can't figure out which function finds the sum of even lines, how exactly should this part of the code look like? - KOD
- According to community rules, questions should not be reduced to completing tasks for students. Give an example of your implementation and ask a question describing specific problems. - Nicolas Chabanovsky ♦
|
2 answers
matrix.Where((x, i) => i % 2 == 0).SelectMany(x => x).Sum(); naturally, the matrix should look like int[][] matrix = new int[M][]; and creating through for, new each line, use Linq
- There’s nothing to do with laboratory work, I’m trying to figure it out for myself, for the time being it’s not very good, but I’m very grateful for the attention I paid - KOD
|
Perhaps in the first couple you should not look at what Linq is and try to understand. So I think it will be easier: link
var sum = 0; // проходим по всем строкам for(int i = 0; i < M; i++){ // если поппалась четная if(i%2 == 0){ //cуммируем ее пройдя по всем ее элементам for(int j =0; j < M ; j++){ sum+= matrix[i,j]; } } } Console.WriteLine(sum); - You can just go with step two - and the test is not required:
for(int i = 0; i < M; i = i + 2)- Trymount
|