+ 1

Access to an element array

How can i access to an element that the user wants to access, example: 11 12 13 14 15 16 17 18 19 The useres input is 5 Then my program has to print the number 15 How can i do that?

29th Apr 2020, 8:48 PM
Diego Becerril
Diego Becerril - avatar
19 Réponses
+ 3
I think for this you have to create new single dimensional array which stores this values as index no 0 1 2 ... 8.
29th Apr 2020, 8:55 PM
! Bad Coder
! Bad Coder - avatar
+ 2
https://code.sololearn.com/c7RBVO16W7lv/?ref=app Pls why is the code not running efficiently. I want the user to enter the pin 1994 after which access will be granted to the usee
30th Apr 2020, 8:26 AM
David Felix
David Felix - avatar
+ 1
! Bad Coder but if the array is 3x4?
29th Apr 2020, 8:56 PM
Diego Becerril
Diego Becerril - avatar
+ 1
N * M = newArrayLength
29th Apr 2020, 9:01 PM
! Bad Coder
! Bad Coder - avatar
+ 1
Diego Becerril 2*2 = 4 You can only stored 4 values in it.
29th Apr 2020, 9:09 PM
! Bad Coder
! Bad Coder - avatar
+ 1
int[][] arr = { { 11, 12, 13 }, { 14, 15, 16 }, { 17, 18, 19 }, { 20, 21, 22 } }; // User Input minus 1 var index = (5 - 1); var x = index % arr[0].length; var y = index / arr[0].length; System.out.println(arr[y][x] + " / x: " + x + " / y: " + y);
29th Apr 2020, 9:10 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 1
For NxM matrix, x=input/N; y=input%M; System.out.println(arr[x] [y]);
29th Apr 2020, 9:37 PM
Jayakrishna 🇮🇳
+ 1
Dear Diego Becerril , Accessing elements in a double dimensional array or a multi-dimensional is quite simple. Just ask the user to input the dimensions, I mean for example if you have a 2-d array which has following elements. 11 12 13 14 15 16 17 18 19 Then user must enter 2 dimensions I mean user must give 2 inputs One input for first dimension which is row index here and other input for second dimension which is column index here. Thus, if user enters 2 then 1 Then it accesses the element at row index 2 and column index 1 Therefore output will be 18 Remember indexing starts with 0. Hope u understand!!! Keep coding!!
30th Apr 2020, 2:02 PM
Reca Resu
Reca Resu - avatar
0
the array is NxM
29th Apr 2020, 8:56 PM
Diego Becerril
Diego Becerril - avatar
0
or there is no other way like puting the N value and the M value and searching it automaticly?
29th Apr 2020, 8:58 PM
Diego Becerril
Diego Becerril - avatar
0
3 x 4 = 12 If array is 3x4 then you have to create array length of 12.
29th Apr 2020, 8:59 PM
! Bad Coder
! Bad Coder - avatar
0
or the other way 11 12 13 14 15 16 17 18 19 number of row = 2 number of column = 2 the output is = 19 how can i do that, can you give me an example in code please?
29th Apr 2020, 9:07 PM
Diego Becerril
Diego Becerril - avatar
0
Both multi-dimensional and single-dimensional array will do, the former being easier... single dimensional will require you to do this while creating the array: int i = (curRow*columns)+j; arr[i] = ....; where j is the less than columns and represent the current column, curRow represent the row you are currently working on. you'd retrieve item 5 with no stress. return arr[5];
30th Apr 2020, 3:33 AM
MegaZeus
MegaZeus - avatar
0
I don't remember well java but it could work something like this. Define array Array=["..."] Define variable input X=value input Define index i= x; Define variable element Element=array[i] >Print element I guess that could work if is an 1d array. If not, should create variables to relate the value input with the index.
30th Apr 2020, 4:46 AM
David Jaramillo
David Jaramillo - avatar
0
Send the code
30th Apr 2020, 6:34 AM
Sai Nadh
Sai Nadh - avatar
0
They way you arranged the numbers it looks like a two-dimensional array. The Indexes to access such an array would look like this: [ 0,0 ] [ 0,1 ] [ 0,2 ] [ 1,0 ] [ 1,1 ] [ 1,2 ] [ 2,0 ] [ 2,1 ] [ 2,2 ] To access for example "15" you have to take the index [ 1, 1 ], for "17" it would be [ 2,0 ].
1st May 2020, 1:23 PM
Martin Rech
Martin Rech - avatar
- 2
the output where the user input is the row and the column and that numer has to be printed
29th Apr 2020, 9:15 PM
Diego Becerril
Diego Becerril - avatar
- 2
thank you all guys i did it like this //acutal array /*11 12 13 14 15 16 17 18 19*/ int n,m; sout(“column”); n = s.nextInt(); //assuming n = 2 sout(“raw”); //assuming m = 2 m = s.nextInt(); sout(arr[n][m]); output 19
30th Apr 2020, 3:59 AM
Diego Becerril
Diego Becerril - avatar
- 3
the output where the user input is the row and the column and that numer has to be printed
29th Apr 2020, 9:15 PM
Diego Becerril
Diego Becerril - avatar