如何将二维数组中的元素索引存储到一维数组中,然后交换这些值

How to Store Index of an Element from 2D Array into a 1D Array then Swap those Values

我在查找如何将一个元素的行索引和列索引从二维数组存储到一维数组时遇到了问题。存储这些索引后,我需要将元素相互交换。另外,我完全理解使用 'using namespace std;' 并不是最好的练习方式,但这是 class 所需要的。这是我目前所拥有的:

#include <iostream>
#include <iomanip>
using namespace std;

//function prototypes
void minVal(int array2D[4][4], int array1D[], int numElements);
void maxVal(int array2D[4][4], int array1D[], int numElements);
void swapValues(int array2D[4][4], int array1D[], int numElements);


int main() {
    //begin program
    cout << "Array Swap Program" << endl;
    cout << "---------------------------" << endl;

    //initialize 2D array
    int twoDimensionalArray[4][4] = {
            {9, 8, 16, 7},
            {11, 6, 3, 14},
            {13, 4, 5, 12},
            {15, 1, 2, 10}
    };

    //display 2D array to user
    cout << "Below is the two dimensional array: " << endl;
    int row = 4;
    int column = 4;
    for (int i = 0; i < column; i++){
        for (int j = 0; j < row; j++){
            cout << twoDimensionalArray[i][j] << ' ';
        }//end inner for loop
        cout << endl;
    }//end outer for loop

    //initialize 1D array
    int oneDimensionalArray[4] = {{}, {}, {}, {}};


    //find minimum value using minVal function prototype
    minVal(twoDimensionalArray, oneDimensionalArray, 16);

    //find maximum value using maxVal function prototype
    maxVal(twoDimensionalArray, oneDimensionalArray, 16);
    return 0;
}

//function descriptions

//Minimum Value Void Function
void minVal(int array2D[4][4], int array1D[], int numElements){
    cout << "Searching array for minimum vale." << endl;
    cout << "Please wait..." << endl;

    //assign first element to the high variable
    int min = array2D[0][0];
    int row;
    int column;

    //begin search with second element
    for (int sub = 1; sub < numElements; sub += 1){
        if (array2D[0][sub] < min){
            min = array2D[0][sub];
            array1D[0] = array2D[0][sub];
        }//end if
    }//end for
    cout << "The minimum value of the 2D array is: " << min << endl;



    //assign row index to 1D array's first element
    cout << "It's located at row: " << array1D[0] << endl;


}//end of minVal

//Maximum Value Void Function
void maxVal(int array2D[4][4], int array1D[], int numElements){
    cout << "Searching array for maximum value." << endl;
    cout << "Please wait..." << endl;

    //assign first element to the high variable
    int max = array2D[0][0];

    //begin search with second element
    for (int sub = 1; sub < numElements; sub += 1){
        if (array2D[0][sub] > max){
            max = array2D[0][sub];
        }//end if
    }//end for
    cout << "The maximum value of the 2D array is: " << max << endl;
}//end of maxVal

我希望输出是 oneDimensionalArray 的索引值

{{二维数组的minVal行索引},{二维数组的minVal列索引},{二维数组的maxVal行索引},{二维数组的maxVal列索引}};

那么应该交换二维数组中列出的最小值和最大值。

我也希望能解释如何找到这些东西,而不仅仅是解决方案。谢谢!

由于二维数组将其元素存储在连续的内存中,因此您可以使用 std::minmax_element and std::distance.

获取最小值和最大值,加上距数组开头的距离

这是一个小例子:

#include <iostream>
#include <algorithm>

int main() 
{
    //initialize 2D array
    int twoDimensionalArray[4][4] = {
            {9, 8, 16, 7},
            {11, 6, 3, 14},
            {13, 4, 5, 12},
            {15, 1, 2, 10}
    };

    // get both the minimum and maximum element in the 2D array
    auto pr = std::minmax_element(&twoDimensionalArray[0][0], &twoDimensionalArray[3][4]);

    // get the distances 
    auto dist_min = std::distance(&twoDimensionalArray[0][0], pr.first);
    auto dist_max = std::distance(&twoDimensionalArray[0][0], pr.second);
    std::cout << "Min Value: " << *(pr.first) <<  "  Distance: " << dist_min << "\n";
    std::cout << "Max Value: " << *(pr.second) << "  Distance: " << dist_max;
}

输出:

Min Value: 1  Distance: 13
Max Value: 16  Distance: 2

Live Example

注意std::minmax_element的用法——参数基本上是二维数组中第一个元素的地址,以及二维数组中最后一个元素的地址。这为我们提供了搜索范围,并且符合 minmax_element 关于用于前两个参数的迭代器的要求。


如果矩阵保证是正方形的,那么我们可以使用模数和除法的一点点数学得到最小值和最大值的行和列:

#include <iostream>
#include <algorithm>

int main() 
{
    //initialize 2D array
    int twoDimensionalArray[4][4] = {
            {9, 8, 16, 7},
            {11, 6, 3, 14},
            {13, 4, 5, 12},
            {15, 1, 2, 10}
    };

    auto pr = std::minmax_element(&twoDimensionalArray[0][0], &twoDimensionalArray[3][4]);
    auto dist_min = std::distance(&twoDimensionalArray[0][0], pr.first);
    auto dist_max = std::distance(&twoDimensionalArray[0][0], pr.second);
    int row_min = dist_min / 4;
    int col_min = dist_min % 4;
    int row_max = dist_max / 4;
    int col_max = dist_max % 4;
    std::cout << "Min Value: " << *(pr.first) << "\n" << "Min Location: (" << row_min << "," << col_min << ")\n\n";
    std::cout << "Max Value: " << *(pr.second) << "\n" << "Max Location: (" << row_max << "," << col_max << ")\n";
}

输出:

Min Value: 1
Min Location: (3,1)

Max Value: 16
Max Location: (0,2)

因此,如果您要使用此解决方案,只需调整您的代码,以便将行和列索引值保存在您声明的一维数组中。

Live Example