[英]Shifting values in 2d array
我是編程新手,我正在研究一種方法,該方法將給定列中的所有最高值存儲在列底部的二維數組中,我使用 Random 類來獲取不同的值。
我被困在那里,現在不知道該怎么做,任何幫助將不勝感激,
到目前為止我的代碼
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);
}
}
}
目前尚不清楚將要實施的內容:
一種將給定列中所有最高值存儲在列底部的二維數組中的方法
如果您想找出給定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.