I'm having trouble on finding how to store the row and column index of one element from a 2D array into a 1D array. Once those indexes are stored, I need to swap the elements with one another. Also, I totally understand that the use of 'using namespace std;' is not the best for practice, however that is what is required in this class. Here is what I have so far:
#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
I expect the output to be that the index values of oneDimensionalArray will be
{{minVal row index of 2D array}, {minVal column index of 2D array}, {maxVal row index of 2D array}, {maxVal column index of 2D array}};
Then the minimum and maximum values listed in the 2D array should be swapped.
I would appreciate the explanation of how to find these things too, not just the solution. Thank you!
Since a two-dimensional array stores its elements in contiguous memory, you can get both the minimum and maximum, plus the distance away from the start of the array using std::minmax_element and std::distance.
Here is a small example:
#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;
}
Output:
Min Value: 1 Distance: 13
Max Value: 16 Distance: 2
Note the usage of std::minmax_element
-- the parameters are basically the address of the first element in the 2D array, and the address of one past the last element in the 2D-array. This gives us the range to search, and conforms to the requirements for minmax_element
with respect to the iterators that are used for the first two parameters.
If the matrix is guaranteed to be square, then we can get the row and column of the minimum and maximum using a small bit of math using modulus and division:
#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";
}
Output:
Min Value: 1
Min Location: (3,1)
Max Value: 16
Max Location: (0,2)
So if you are to use this solution, you just need to adjust your code so that you hold the row and column index values in the one-dimensional arrays you've declared.