1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <array>
#define NUM_ROW 4
#define NUM_COLUMN 4
#define NUM_DEPTH 3
using namespace std;
/* (3) The location (i, j, k) of the element that has the maximum value difference with its neighboring elements
(1 point). Here (i, j, k) refers to the array indices of the element. The definition of neighboring elements is shown below:
You need to create a 3d array (like D[4][4][3]), and use it to store the maximum neighboring difference with respect to each element.
After the establishment of D array, you need to search this array to find the element with maximum value, and its indices (i, j k), which is the answer to this sub-question.
The key is to set up D matrix. For each D[i][j][k], you need calculate the difference between the center element X[i][j][k] and all the neighboring elements such
as X[i+1][j][k]. And then use the maximum one to be stored in D[i][j][k].
*/
float MaxDiff(float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH], float maxD[NUM_ROW][NUM_COLUMN][NUM_DEPTH])
{
float maxDifference;
float Difference;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
for (int k = 0; k < 3; k++)
{
maxD[i][j][k] = Value[i][j][k] - Value[i + 1][j+1][k+1];
}
for (int i = 3; i >= 0; i--) // this for loop print the array's elements
for (int j = 2; j >= 0; j--)
for (int k = 2; k >= 0; k--)
{
cout << maxD[i][j][k];
}
cout << endl;
return 0;
}
int main()
{
float maxD[4][4][3];
float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH] =
{ { { 1.1, 1.2, 1.3 }, { 1.4, 1.5, 1.6 }, { 1.7, 1.8, 1.9 }, { 2.0, 2.1, 2.2 } },
{ { 2.1, 2.2, 2.3 }, { 2.4, 2.5, 2.6 }, { 2.7, 2.8, 2.9 }, { 3.0, 3.1, 3.2 } },
{ { 3.1, 3.2, 3.3 }, { 3.4, 3.5, 3.6 }, { 3.7, 3.8, 3.9 }, { 4.0, 4.1, 4.2 } },
{ { 4.1, 4.2, 4.3 }, { 4.4, 2.5, 2.6 }, { 2.7, 2.8, 2.9 }, { 3.0, 3.1, 3.2 } } };
system("pause");
return 0;
}
|