简体   繁体   English

在二维数组中移动值

[英]Shifting values in 2d array

I am new to programming and I am working on a method that stores all the highest values in a given column in a 2d array at the bottom of the column, I use Random class to get different values.我是编程新手,我正在研究一种方法,该方法将给定列中的所有最高值存储在列底部的二维数组中,我使用 Random 类来获取不同的值。

I am stuck there and don't really know what to do now, any help would be much appreciated,我被困在那里,现在不知道该怎么做,任何帮助将不胜感激,

My code so far到目前为止我的代码

 void StoreHighestValueAtBottom(int[,] matrix, int column)
    {
        int max = 0;
        int maxValue = 0;
        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            if (matrix[row, column] > max)
            {
                max = matrix[row, column];
                Array.Copy(matrix, 0, matrix, matrix.GetLength(0) + 1, matrix.GetLength(0) * matrix.GetLength(1) - matrix.GetLength(0) - 1);
                Array.Copy(new int[matrix.GetLength(0) + 1, 1], max, matrix, max, matrix.GetLength(0) + 1);
            }
        }

    }

It's unclear what are going to implement:目前尚不清楚将要实施的内容:

a method that stores all the highest values in a given column in a 2d array at the bottom of the column一种将给定列中所有最高值存储在列底部的二维数组中的方法

If you want to find out (single) max value in the given column and swap this max with the bottom item in the column in order max value to be the last in the column:如果您想找出给定column中的(单个) max并将此max与列中的底部项目交换,以使max成为列中的最后一个:

 void StoreHighestValueAtBottom(int[,] matrix, int column) {
   int maxRow = 0;
   int max = matrix[0, column];

   // Find max value and the row where it is
   for (int row = 1; row < matrix.GetLength(0); ++row) {
     if (matrix[row, column] > max) {
       max = matrix[row, column];
       maxRow = row; 
     } 
   }
   
   // swap bottom item and max item 
   int bottom = matrix[matrix.GetLength(0) - 1, column];
   matrix[matrix.GetLength(0) - 1, column] = max;
   matrix[maxRow, column] = bottom; 
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM