简体   繁体   中英

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.

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:

 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; 
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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