在二维数组中找到最小值和最大值?

时间:2018-10-26 18:41:56

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

运行此代码后,它将生成所需行和列的随机数组。然后循环将按其对角线将数组划分为上侧和下侧。在上侧,循环将寻找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;
}

2 个答案:

答案 0 :(得分:1)

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

顺便说一句:您需要分别用maxmin初始化INT_MININT_MAX,而不是相反。 因此必须是

int max = INT_MIN;
int min = INT_MAX;

否则,如果您写int max = INT_MAX,则不会有像if(array[i][j] > max)这样的比较结果为true,因为没有整数值大于INT_MAX

答案 1 :(得分:0)

我相信这会满足您的要求。交换是在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;
}

如其中一条评论中所述,std :: swap()是执行交换的一种方法。我修改了示例,在main();的末尾使用std:swap();

祝你好运。