+ 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?
19 odpowiedzi
+ 3
I think for this you have to create new single dimensional array which stores this values as index no 0 1 2 ... 8.
+ 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
+ 1
! Bad Coder but if the array is 3x4?
+ 1
N * M = newArrayLength
+ 1
Diego Becerril 2*2 = 4
You can only stored 4 values in it.
+ 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);
+ 1
For NxM matrix,
x=input/N;
y=input%M;
System.out.println(arr[x] [y]);
+ 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!!
0
the array is NxM
0
or there is no other way like puting the N value and the M value and searching it automaticly?
0
3 x 4 = 12
If array is 3x4 then you have to create array length of 12.
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?
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];
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.
0
Send the code
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 ].
- 2
the output where the user input is the row and the column and that numer has to be printed
- 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
- 3
the output where the user input is the row and the column and that numer has to be printed