Посчитать сумму всего ступенчатого массива, используя оператор foreach - C#
Формулировка задачи:
В моём коде считает только сумму эл-тов строк,а нужно всего массива,с помощью оператора foreach.
Class1.cs*
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lab_2
{
class massiv
{
int[][] myArr;
public void CreateMas()
{
Random r = new Random();
int u = r.Next(5, 5); //колл строк
myArr = new int[u][];
for (int i = 0; i < u; i++)
{
myArr[i] = new int[r.Next(1, 8)]; //длинна строк
}
for (int i = 0; i < myArr.Length; i++)
{
for (int j = 0; j < myArr[i].Length; j++)
{
myArr[i][j] = r.Next(1, 20); //заполнение
}
}
}
public void Delivery() //выдача
{
for (int i = 0; i < myArr.Length; i++)
{
for (int j = 0; j < myArr[i].Length; j++)
Console.Write(myArr[i][j] + " ");
Console.WriteLine(" ");
}
Console.WriteLine(" ");
}
public void SumLines() //выдача суммы эл-тов массива
{
Console.WriteLine("Сумма элементов массива каждой строки:");
int m = 0;
int[] ray = new int[myArr.Length];
for (int i = 0; i < myArr.Length; i++)
{
for (int j = 0; j < myArr[i].Length; j++)
{
m = m + myArr[i][j];
if (j == myArr[i].Length - 1)
{
ray[i] = m;
m = 0;
}
}
}
Console.WriteLine(" ");
foreach (int x in ray)
Console.Write(" " + x);
Console.WriteLine(" ");
Console.WriteLine(" ");
}
}
}Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lab_2
{
class Program
{
static void Main()
{
massiv mas = new massiv();
mas.CreateMas();
mas.Delivery();
mas.SumLines();
Console.ReadKey();
}
}
}Решение задачи: «Посчитать сумму всего ступенчатого массива, используя оператор foreach»
textual
Листинг программы
Console.WriteLine(sum);