簡體   English   中英

如何將元素的索引從2D數組存儲到1D數組然后交換這些值

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

我在尋找如何將2D數組中一個元素的行和列索引存儲到1D數組中遇到麻煩。 一旦存儲了這些索引,我就需要相互交換元素。 另外,我完全理解使用“ using namespace std;” 雖然不是最佳實踐,但是這是本課程所要求的。 這是我到目前為止的內容:

#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的索引值為

{{2D數組的minVal行索引},{2D數組的minVal列索引},{2D數組的maxVal行索引},{2D數組的maxVal列索引}};

然后應交換2D數組中列出的最小值和最大值。

我將不勝感激地解釋如何找到這些東西,而不僅僅是解決方案。 謝謝!

由於二維數組將其元素存儲在連續內存中,因此您可以使用std :: minmax_elementstd :: 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

現場例子

請注意std::minmax_element的用法-這些參數基本上是2D數組中第一個元素的地址,以及2D數組中最后一個元素之后的地址。 這為我們提供了搜索范圍,並且就用於前兩個參數的迭代器而言,符合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)

因此,如果要使用此解決方案,則只需調整代碼,以便將行和列索引值保存在已聲明的一維數組中。

現場例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM