【问题标题】:Finding min and max in 2-D arrays?在二维数组中查找最小值和最大值?
【发布时间】:2019-03-31 13:28:24
【问题描述】:

运行此代码后,它将生成所需行和列的随机数组。然后循环将数组按其对角线划分为上侧和下侧。在上侧,循环将查找 max number ,在下侧,循环将查找 min number 。然后在最后阶段我需要改变 minmax 的位置。 Max 代替min,反之亦然。代码运行并找到minmax。不知道如何更改它们的位置。

代码

#include <iostream>
#include <time.h>
#include <limits.h>


using namespace std;

int main(){
int rows, columns;
int max = INT_MAX;
int min = INT_MIN;
int XindexOfMax, YindexOfMax;
int XindexOfMin, YindexOfMin;

cout << "Enter rows: ";
cin >> rows;
cout << "Enter columns: ";
cin >> columns;

int **array = new int *[rows];            //generating random array
for(int i = 0; i < rows; i++)
    array[i] = new int[columns];

srand((unsigned int)time(NULL));         //generating randoms

for(int i = 0; i < rows; i++){           //loop for the main array
    for(int j = 0; j < columns; j++){
        array[i][j] = rand() % 10;    
        cout << array[i][j] << " "; 
    }
    cout << "\n";
}

cout << "For finding Max: " << endl;

for(int i = 0; i < rows; i++){             //upper half of the diagonal
    for(int j = 0; j < columns - i; j++){
        cout << array[i][j] << " ";
        if(array[i][j] > max){
            max = array[i][j];
            XindexOfMax = i;              //find x and y coordinates if max
            YindexOfMax = j;
        }

    }
    cout << "\n";
}
cout << "For finding Min: " << endl;

for (int i = 0; i < rows; i++){          // lower half of the diagonal
    for (int j = 0; j < columns; j++){
        if (j < columns - i - 1){
            cout << "  ";
        }
        else{
            cout << array[i][j] << " "; 
            if(array[i][j] < min){
                min = array[i][j];
                XindexOfMin = i;       //find x and y coordinates if min
                YindexOfMin = j;
            }
        }
    }
cout << "\n";
}
cout << "Result" << endl;
//swapping positions of min and max 
std::swap(array[XindexOfMax][YindexOfMax], array[XindexOfMin][YindexOfMin]);

for(int i = 0; i < rows; i++){        
    for(int j = 0; j < columns; j++){
        cout << array[i][j] << " ";   //Printing the final array
    }
    cout << "\n";
}
return 0;
}

【问题讨论】:

  • 请注意,您所拥有的不是整数的二维数组。它是一个一维指针数组,每个指针指向一个一维整数数组。 int (*array)[columns] 将是一个二维数组。
  • 您可以编辑该部分以查看其工作原理吗? @光谱

标签: c++ arrays loops multidimensional-array minmax


【解决方案1】:

除了minmax 值之外,您还需要记住分别找到minmax 的索引。然后您可以交换值(手动或使用std::swap)。

顺便说一句:您需要分别用INT_MININT_MAX 初始化maxmin,而不是相反。 所以应该是

int max = INT_MIN;
int min = INT_MAX;

否则,如果你写int max = INT_MAX,那么像if(array[i][j] &gt; max) 这样的比较将永远不会计算为真,因为没有大于INT_MAX 的整数值。

【讨论】:

  • 我需要知道如何在二维数组中分配索引
  • 像这样:array[i][j] = rand() % 10; //generating randoms
  • @Tim Randall 上面的代码我已经编辑好了,你可以运行看看。在最终结果中,我需要交换 max 和 min 的值。但数组保持原样。
  • @Dr.lackedu:错误在max/min-initialisation;查看编辑后的答案
【解决方案2】:

我相信这会满足您的要求。交换是在 main() 结束时使用 std::swap() 完成的。

我将几个例程分解为函数。答案将通过拆分更多功能来改进,以便每个功能执行单个任务。尽管如此,我还是添加了一个类,但仍试图忠实于您打印半三角形的原始设计。好的。希望您现在可以处理一些课程。

#include <iostream>
#include <time.h>
#include <limits.h>
#include <cmath>

using namespace std;

class Point {
public:
    Point(int x, int y, int value) : x(x), y(y), value(value) {}
    int X() { return x; }
    int Y() { return y; }
    int Value() { return value; }
    void SetValue(int valueArg) { value = valueArg; }
    void SetPoint(int xArg, int yArg, int valueArg) {
        x = xArg;
        y = yArg;
        value = valueArg;
    }

    string to_string() {
        return std::to_string(value);
    }

private:
    int x;
    int y;
    int value;
};

void PrintArray(int **array, int rows, int columns) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            cout << array[i][j] << " ";
        }
        cout << "\n";
    }
}

int main() {
    int rows, columns;
    cout << "Enter rows: ";
    cin >> rows;
    cout << "Enter columns: ";
    cin >> columns;
    int **array = new int *[rows];    //generating random array
    for (int i = 0; i < rows; i++)
        array[i] = new int[columns];
    srand((unsigned int) time(NULL));
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            array[i][j] = rand() % 10;     //generating randoms
        }
    }
    PrintArray(array, rows, columns);
    Point maxPoint = Point(0, 0, INT_MIN); // initialize max
    Point minPoint = Point(0, 0, INT_MAX);;
    cout << "For finding Max: " << endl;
    for (int i = 0; i < rows; i++) {            //separating the upper half
        for (int j = 0; j < columns - i; j++) {
            if (j > columns - i) {
                cout << array[i][j] << " ";
            } else {
                cout << array[i][j] << " ";
                if (array[i][j] > maxPoint.Value()) {
                    maxPoint.SetPoint(i, j, array[i][j]);
                }
            }
        }
        cout << "\n";
    }
    cout << "For finding Min: " << endl;
    for (int i = 0; i < rows; i++) {         //separating the lower half
        for (int j = 0; j < columns; j++) {
            if (j < columns - i - 1) {
                cout << "  ";
            } else {
                cout << array[i][j] << " ";
                if (array[i][j] < minPoint.Value()) {
                    minPoint.SetPoint(i, j, array[i][j]);
                }
            }
        }
        cout << "\n";
    }
    cout << "array before: " << endl;
    PrintArray(array, rows, columns);
    cout << "Swapping " << "maxPoint(" << maxPoint.X() << ", " << 
        maxPoint.Y() << ") with minPoint("
        << minPoint.X() << ", " << minPoint.Y() << ")" << endl;
    std::swap(
        minPoint.GetCellReference(array), 
        maxPoint.GetCellReference(array));
    PrintArray(array, rows, columns);
    return 0;
}

正如其中一个 cmets 所述,std::swap() 是一种执行交换的方法。我已将示例修改为在 main() 末尾使用 std:swap();

祝你好运。

【讨论】:

  • 优先使用std::swap,而不是简单的实现
  • @Fureeish 我已修改 main() 的结尾以调用 std::swap()。我以前从未尝试过对数组的引用。非常好!
猜你喜欢
  • 2015-04-06
  • 2015-02-01
  • 2017-03-16
  • 1970-01-01
  • 2018-07-05
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 2016-01-31
相关资源
最近更新 更多