Как можно вместа цикла for записать через цикл foreach? - C#
Формулировка задачи:
Здравствуйте. Как можно вместа цикла for записать через цикл foreach?
static void Change(int[,] a)
{
for (int i = b - 1; i < a.GetLength(0); ++i)
for (int j = bb - 1; j < a.GetLength(1); ++j)
if (a[i, j] > 0) a[i, j] = 0;
}
вот сам код программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace pr_8
{
class Class
{
static int b, bb;
static int[,] Input(out int n, out int m)
{
Console.WriteLine("введите размерность массива");
Console.Write("n = ");
n = int.Parse(Console.ReadLine());
Console.Write("m = ");
m = int.Parse(Console.ReadLine());
Console.WriteLine("Введите начало и конец интервала [a,b]");
Console.Write("a = ");
b = int.Parse(Console.ReadLine());
Console.Write("b = ");
bb = int.Parse(Console.ReadLine());
int[,] a = new int[n, m];
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
{
Console.Write("a[{0},{1}]= ", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}
return a;
}
static void Print(int[,] a)
{
for (int i = 0; i < a.GetLength(0); ++i, Console.WriteLine())
for (int j = 0; j < a.GetLength(1); ++j)
Console.Write("{0,5} ", a[i, j]);
}
static void Change(int[,] a)
{
for (int i = b - 1; i < a.GetLength(0); ++i)
for (int j = bb - 1; j < a.GetLength(1); ++j)
if (a[i, j] > 0) a[i, j] = 0;
}
static void Main(string[] args)
{
int n, m;
int[,] myArray = Input(out n, out m);
Console.WriteLine("Исходный массив:");
Print(myArray);
Change(myArray);
Console.WriteLine("Измененный массив:");
Print(myArray);
Console.ReadKey();
}
}
}Решение задачи: «Как можно вместа цикла for записать через цикл foreach?»
textual
Листинг программы
static void Change(int[,] a)
{
for (int i = b - 1; i < a.GetLength(0); ++i)
for (int j = bb - 1; j < a.GetLength(1); ++j)
if (a[i, j] > 0) a[i, j] = 0;
}