Заместо два цикла for записать одним for - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте. Возник вопрос как можно записать в методе заместо заместо два цикла for записать одним for? вот код программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Mass2d
{
    class Program
    {
        
        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());
            
            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[,] arr,int a, int b)
        {
            for (int i = 0; i < arr.GetLength(0); ++i)
                for (int j = 0; j < arr.GetLength(1); ++j)
                    if (arr[i, j] >= a && arr[i, j]<=b) 
                        arr[i, j] = 0;
        }
 
             static void Main(string[] args)
        {
            
            int n, m;
            int[,] myArray = Input(out n, out m);
            Console.WriteLine("Исходный массив:");
            Print(myArray);

            int a, b;
            Console.WriteLine("Введите начало интервала");
            Console.Write("a = ");
            a = int.Parse(Console.ReadLine());
            Console.WriteLine("Введите конец интервала");
            Console.Write("b = ");
            b = int.Parse(Console.ReadLine());
 
            Change(myArray,a,b);
            Console.WriteLine("Измененный массив:");
            Print(myArray);
 
            Console.ReadKey();
        }
    }
}
вот в этом методе ?
static void Change(int[,] arr,int a, int b)
        {
            for (int i = 0; i < arr.GetLength(0); ++i)
                for (int j = 0; j < arr.GetLength(1); ++j)
                    if (arr[i, j] >= a && arr[i, j]<=b) 
                        arr[i, j] = 0;
        }

Решение задачи: «Заместо два цикла for записать одним for»

textual
Листинг программы
        static void Changeone(int[,] arr, int a, int b)
        {
 
            int x = 0, y = 0;
            for (int i = 0; i < arr.Length; i++, y++)
            {
                if (y == arr.GetLength(1))
                {
                    y = 0;
                    x++;
                }
                if (arr[x, y] >= a && arr[x, y] <= b)
                    arr[x, y] = 0;
            }
 
        }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4 из 5
Похожие ответы