Question: Suppose that you have a 2D array (matrix) of integer with size N*N with values of 0 (N > 2). Several elements of this matrix
- Suppose that you have a 2D array (matrix) of integer with size N*N with values of 0 (N > 2). Several elements of this matrix are inserted with another value, and the middle of this matrix is also guaranteed to have a value other than 0. Make a program to get where is (are) the nearest element(s) with same value as matrixs centroid. If more than 1 position exists near the centroid, print all the nearest elements. If no nearest element exists that has the same value with the centroid, print: No nearest element exists. The assumption is that position 0,0 start from top leftmost matrix. For example: Input:
-
0
0
2
0
0
0
1
0
0
0
0
0
1
0
5
0
0
0
0
0
0
0
0
0
1
(centroid at 2,2)
Output:
Nearest same element is at: 1,1
Another example:
Input:
0
0
2
0
0
0
1
0
0
0
0
1
1
1
5
0
0
0
0
0
0
0
0
0
7
(centroid at 2,2)
Output:
Nearest same elements is at: (2,3) and (2,1)
Input:
0
0
2
0
0
0
0
0
0
0
1
0
0
0
0
0
(centroid at 2,2)
Output:
Nearest same elements is at: no nearest element.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
