【问题标题】:Problem with program that separates 2D array into even/odd 2D arrays (extra zeroes in even/odd arrays)将二维数组分成偶数/奇数二维数组的程序问题(偶数/奇数数组中的额外零)
【发布时间】:2018-11-08 22:30:23
【问题描述】:

我正在练习二维数组,并正在制作一个程序,根据值是偶数还是奇数,将具有随机整数值的二维数组分成两个单独的数组。

但是,该程序似乎在偶数和奇数数组的每一行中添加了额外的零。我究竟做错了什么?

我认为问题出在 sort() 函数中,我确定偶数和奇数数组的大小,但我不确定。

class Program
{
    static void Main(string[] args)
    {
        int[,] arr = new int[10, 10];

        // Fills 2D array with random values and prints them out 
        Random r = new Random();
        for (int y = 0; y < arr.GetLength(0); y++)
        {
            for (int x = 0; x < arr.GetLength(1); x++)
            {
                arr[y, x] = r.Next(1, 99);
                Console.Write(arr[y,x] + " ");
            }
            Console.WriteLine();
        }

        Console.WriteLine();

        // Function that separates original array into 2 separate ones (even and odd) 
        sort(arr);

        Console.ReadLine();
    }

    public static void sort(int[,] array)
    {
        int j1 = 0;
        int i1 = 0;
        int j2 = 0;
        int i2 = 0;

        // Increases the size of the even/odd arrays whenever the value of the original array is even/odd respectively 
        // I think this is where the problem is
        for (int y = 0; y < array.GetLength(0); y++)
        {
            for (int x = 0; x < array.GetLength(1); x++)
            {
                if (array[y,x] % 2 == 0)
                {
                    i1 += 1;
                }
                else
                {
                    i2 += 1;
                }
            }
            j1 += 1;
            j2 += 1;
        }

        int[,] evenArr = new int[j1, i1];
        int[,] oddArr = new int[j2, i2];

        // Sets the values for the even/odd arrays 
        for (int y = 0; y < array.GetLength(0); y++)
        {
            for (int x = 0; x < array.GetLength(1); x++)
            {
                if (array[y, x] % 2 == 0)
                {
                    evenArr[y, x] = array[y, x];
                }
                else
                {
                    oddArr[y, x] = array[y, x];
                }
            }
        }

        // Prints the values for the even array
        for (int y = 0; y < evenArr.GetLength(0); y++)
        {
            for (int x = 0; x < evenArr.GetLength(1); x++)
            {
                Console.Write(evenArr[y, x] + " ");
            }

            Console.WriteLine();
        }

        Console.WriteLine();

        // Prints the values for the odd array 
        for (int y = 0; y < oddArr.GetLength(0); y++)
        {
            for (int x = 0; x < oddArr.GetLength(1); x++)
            {
                Console.Write(oddArr[y, x] + " ");
            }

            Console.WriteLine();
        }
    }
}

【问题讨论】:

    标签: c# arrays visual-studio multidimensional-array


    【解决方案1】:

    当您初始化数组时(evenArr = new int[j1, i1], oddArr = new int[j2, i2];),默认值为零。由于有一个额外的零,这意味着您应该检查长度并可能将其减一。

    【讨论】:

      猜你喜欢
      • 2021-08-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多