how to get all near and same elements in 2-dimension array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to get all near and same elements in 2-dimension array?

for a 2d array like arr[100][100]={…},arr[i][j] is a element in it. for k in range(i-1,i+2) and l in range(j-1,j+2), arr[k][l] is "NEAR" arr[i][j]. if arr[ii][jj]==arr[i][j],they are "same" and "NEAR". for arr[x][y],if arr[x][y]==arr[i][j] and it "NEAR" and "same" sth(short for something) and sth "NEAR" and "same" sth and sth "NEAR" and "same" sth and……until sth "NEAR" and "same" a[i][j], arr[x][y] is near and same arr[i][j]. for a arr[i][j],how to get all arr[x][y]? i tried it by iteration,but overstack…

4th Aug 2017, 9:37 PM
Bluebear
Bluebear - avatar
20 Answers
+ 7
what kind of language is this one؟؟؟ edited: I mean what programming language?
4th Aug 2017, 9:51 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 7
Assuming int a[100][100], b[100][100] : // I have no idea what you want rly. for (int i = 0; i < 100; i++) for (int j = 0; j < 100; j++) for (int k = 0; k < 100; k++) for (int l = 0; l < 100; l++) { if (abs(i - k) < 2 && abs(j - l) < 2) cout << a[i][j] << " and " << b[k][l] << " is near "; if (a[i][j] == b[k][l]) cout << "and same."; else cout << "."; }
5th Aug 2017, 2:46 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
To cryptic to understand? Does he want the value of the array elements to be 'near', or the position of the array elements?
5th Aug 2017, 3:16 AM
Fermi
Fermi - avatar
+ 1
Poor language is not a problem, but what programming language is this ?
4th Aug 2017, 10:13 PM
sneeze
sneeze - avatar
0
maybe my english a little poor,i ever learned eglish,but now forgot
4th Aug 2017, 9:54 PM
Bluebear
Bluebear - avatar
0
programming language is not a problem,i just wana a way to achieve,i lack others thoughts except iterator. for a elements in a two-dimension array,i wana get all elements,their value equals with given elements and they are next to given elements. eg. if a[5][5]==a[4][6],a[4][6]==a[3][6],i thought a[3][6] is same and near a[5][5] though 3 and 6 is not near 5. @sneeze
4th Aug 2017, 10:35 PM
Bluebear
Bluebear - avatar
0
# eg. def printarr(a): for i in range(5): for j in range(5): tmp="
quot; if a[i*5+j]==0: tmp=" " print(tmp,end=" ") print("\n") arr2=[] for i in range(5): for j in range(5): if(i*j%5==0): arr2.append(1) elif(j==2): arr2.append(1) else: arr2.append(0) print("if (row,col) is (2,2),result as follows,$ represent all near and same with(2,2),their vulue are 1") printarr(arr2) # @sayan chandra
5th Aug 2017, 8:05 AM
Bluebear
Bluebear - avatar
0
yeah,i wana near and same,not just near. by iterator,i can get all elements near and same with given elements,but for a huge array,stack overflow @sayan chandra
5th Aug 2017, 8:46 AM
Bluebear
Bluebear - avatar
0
https://code.sololearn.com/c6ERTAlyee9W/?ref=app this is a funcion,but i dont want to use iterator,it too slow and easy stack overflow. @sayan chandra
5th Aug 2017, 10:29 AM
Bluebear
Bluebear - avatar
0
random array for test,it can be any array. just like draw,when you want to stuff something: 1st,you selected a coordinate. 2nd,program help you get all coordinate near and color same with given coordinate. 3rd,program reveal color of these coordinate. @sayan chandra
5th Aug 2017, 3:48 PM
Bluebear
Bluebear - avatar
- 1
what do you mean by arr[ii][jj] Bluebear... explain your question... i did not get... just say what u want to do.. like what input given and what output u want...##
5th Aug 2017, 3:04 AM
sayan chandra
sayan chandra - avatar
- 1
and 1st thing is.. you have writen k in range(i-1,i+2) l in tange(j-1,j+2) now see IF i and j are 0,0 then k and l will be -1,-1 at first so how is that possible...
5th Aug 2017, 3:08 AM
sayan chandra
sayan chandra - avatar
- 1
TELL ME WHAT IN LANGUAGE YOU ARE DOING I GOT YOUR QUEATION'S MEANING... SENDING YOU THE CODE FOR NEAR ELEMENTS...WAIT A BIT.. @BLUEBEAR
5th Aug 2017, 3:23 AM
sayan chandra
sayan chandra - avatar
- 1
Bluebear HERE IS THE SOLUTION EXPLAINED give row and column input seperated by an enter eg: 2 3 https://code.sololearn.com/ctlgT9ztZNDc/?ref=app
5th Aug 2017, 4:43 AM
sayan chandra
sayan chandra - avatar
- 1
did you check my code...# blulebear...???
5th Aug 2017, 8:15 AM
sayan chandra
sayan chandra - avatar
- 1
the code i gave to you...is a general logic... it will work on any bigger array like arr[ 500000][500000] any array...
5th Aug 2017, 8:52 AM
sayan chandra
sayan chandra - avatar
- 1
for same elements... just give two itertion... for a user input of row and coloum suppose 3 3 and a[k][l] will search k in range 0 to 99 l in range 0 to 99 and whenever a[row][column] = a[k][l] it will print that....
5th Aug 2017, 8:55 AM
sayan chandra
sayan chandra - avatar
- 1
just add this logic....
5th Aug 2017, 8:55 AM
sayan chandra
sayan chandra - avatar
- 1
tell me what you have written... like so much big... u just need to understand the logic... why generating radom 2D array.. just make a array of 4×4 or 5×5 then use 2 logic one for near one for same.. i think you are overthinking may be.. think casually.... see my code explanation...
5th Aug 2017, 11:32 AM
sayan chandra
sayan chandra - avatar
- 1
remember for any size of a matrix/2D array--> ##4 corner elements have 3 near elements each.. ##all side elements...up-down -right-left have 5 near elements each ##rest all middle elements have 8 near elements each...
5th Aug 2017, 11:34 AM
sayan chandra
sayan chandra - avatar