Code position in java two-dimensional array [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to code positions in a two-dimensional array in Java with dimension of [n][n]
we have n rows and n columns, so what i want to achieve from this perspective is
(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
(2,0) (2,1) (2,2)
I have to get this result ( code row and col to get the result in decimal):
(0) (1) (2)
(3) (4) (5)
(6) (7) (8)

For two-dimensional indices in matrix[i][j], and a linear index in vector[index] the following relation holds.
final int n = 3; // dimension of the columns
int i = ...
int j = ...
int index = i * n + j;
i = index / n;
j = index % n;
Integer division and modulo is used.

(n * row + column) would give you those indices. Is that what you're asking?
Here is the code for it:
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
matrix[i][j] = i*n + j;

Related

Putting numbers at random spots in 2D array (java) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
Improve this question
How would i do step 2
Create a Java application with 2 players. Imagine that two young pirates came to a squared island 10x10 to find a treasure. They were dropped down in random positions. Each player can move only in four directions(N,S,W,E) but not by diagonals. Objective of the game is to find treasures and then the boat. The player who first found at least one treasure and the boat is a winner. Players should also overcome obstacles and fight with some creatures.
Create a 2D array 10x10 and fill it with zeros. (K /1)
Put randomly 2 players (1 and 2), 4 gold treasures (3), one tunnel hole(4), one boat(5)(should be close to the ocean), 2 creatures (6), 2 monsters (7), 2 adventures (8) in the array. Be sure that all of them get different positions.
How do I continue?
import java.util.*;
public class methodreviewtsk1 {
public static void main(String[] args) {
int array1[][]= new int [10][10];
int player1count = 0;
int player2count = 0;
int goldtresurecount = 0;
int tunnelcount = 0;
int creaturecount = 0;
int monstercount = 0;
int adventurescount = 0;
for (int x = 0; x < 10; x++){
for (int y = 0; y < 10; y++){
array1[x][y]= 0;
System.out.print(array1[x][y] + " ");
}
System.out.println();
}
}
}

Why does my last index have a value when it shouldn't have one? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to shift all elements in my array to the left by 1, I am trying to remove the element 3 at index 0 from my array but the output I receive is completely different than what I expect. I don't understand why I receive this output.
The output I expect to receive is [2,2,3,0] I expect a zero on the last index because I shifted all elements to the left so there would be no value shifted to the last index. But I instead received [2,2,3,3]. I don't understand why a 3 is in the last index?
int [] nums = {3,2,2,3};
int length = nums.length;
for (int j = 1; j < length; j++) {
nums [j - 1] = nums [j];
}
return nums.length;
Try this, I just set the last one to 0:
int[] nums = {3,2,2,3};
int length = nums.length;
for (int j = 1; j < length; j++) {
nums [j - 1] = nums [j];
}
nums[nums.length - 1] = 0;
for (int number : nums) {
System.out.print(number);
}
I set the last number to 0 because you only changed the indices 0, 1 and 2 with your for-loop so that the last index, i.e. index 3, keeps the value 3.
So you get this: 2230.

How to create a 2D Array in Haskell? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm struggling to create a 2D list in Haskell with elements are formed by their indices of row and column. I searched but did not find any solutions that can track the indices and use them to calculate the values to push in the array. The recursion traversal x:xs is not suitable for this problem. Please help. Thank you.
Function in Java:
public static int[][] create2DArray(int r, int c) {
int[][] arr = new int[r][c];
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
arr[i][j] = i + j;
}
}
return arr;
}
Haskell:
create2DArray:: Int -> Int -> [[Int]]
...
Output:
0 1 2
1 2 3
2 3 4
create2DArray r c = [ [i+j | j<-[0..c-1]] | i<-[0..r-1] ]
In Haskell, if you really want an array similar to Java's counterpart, Data.Array is what you want. For your example, the array can be contructed as follows:
create2DArray :: Int -> Int -> Array (Int, Int) Int
create2DArray r c = array ((0,0), (r-1,c-1)) [((i,j), i+j) | i <- [0..r-1], j <- [0..c]]

Please help me understand 2D array flip [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I was trying to solve a problem in a coding trainer. But, I just could not figure this problem for the life of me.
Here is the problem:
You are given an m x n 2D image matrix where each integer represents a pixel. Flip it in-place along its horizontal axis.
Example:
Input image :
1 1
0 0
Modified to :
0 0
1 1
I tried swapping rows as I traversed 2d array down the row for test case:
1,2,3
4,5,6
7,8,9
But, I end up getting
4,5,6
7,8,9
1,2,3
instead of
{{7,8,9},
{4,5,6},
{1,2,3}}
Here is the answer code.
public static void flipHorizontalAxis(int[][] matrix) {
int r = matrix.length - 1, c = matrix[0].length - 1;
int temp = 0;
for(int i = 0; i <= r/2; i++){
for(int j = 0; j <= c; j++){
temp = matrix[i][j];
matrix[i][j] = matrix[r-i][j];
matrix[r-i][j] = temp;
}
}
}
I still do not understand the answer code. Specifically, why the outer loop has "i <= r/2" and the swap has "matrix[r-i]" in the index. Why r/2 and r-i? I really do not understand why and I am totally stuck.
Can someone explain those lines so I can understand the code?
Here is the expected output for test cases:
1
{{1}}
1,0,0
0,0,1
{{0,0,1},{1,0,0}}
1,0
{{1,0}}
1,2,3
4,5,6
7,8,9
{{7,8,9},{4,5,6},{1,2,3}}
1,0,1
1,0,1
{{1,0,1},{1,0,1}}
Focusing on only the double loop:
for (int i=0; i <= r/2; i++){
for (int j=0; j <= c; j++){
temp = matrix[i][j];
matrix[i][j] = matrix[r-i][j];
matrix[r-i][j] = temp;
}
}
The outer loop in i only ranges up to (and including) one half the height of the matrix because we want to swap each array with its "mirror" image on the other side of the median row. That is, for a 3x3 matrix we want to do the following:
1,2,3 i=0
4,5,6
7,8,9 r-i=matrix.length-1 = 3-1 = 2
(swap these rows, i=i+1)
7,8,9
4,5,6 i=1, r-i=1
1,2,3
(swap the median row with itself, nothing changes)
If we were to allow the outer loop to run the full height of the input matrix, then after the median row we would actually undo the swap already done, and would just end up the original input matrix.
The number of row swaps you need to do is matrix.length/2 - 1. You could have written:
for (int i = 0; i < matrix.length/2; i++)
instead of:
for(int i = 0; i <= r/2; i++)
for matrices with an odd number of rows, matrix.length/2 and r/2 are equal, which means that in the second form, because of the <=, you will swap the middle row with itself, which is useless, so I prefer the first form.
Now the index r-i will go downwards from the index of the last row (r = matrix.length-1). It's the index of the row that must be swapped with the one at index i.
Note that the the rows themselves are array, and it would be more efficient to swap the whole rows instead of each individual element, so here is a better solution:
public static void flipHorizontalAxis(int[][] matrix) {
int r = matrix.length-1;
for (int i = 0; i < matrix.length/2; i++) {
int[] temp = matrix[i];
matrix[i] = matrix[r - i];
matrix[r - i] = temp;
}
}
Or:
public static void flipHorizontalAxis(int[][] matrix) {
int r = matrix.length;
for (int i = 0; i < matrix.length/2; i++) {
--r;
int[] temp = matrix[i];
matrix[i] = matrix[r];
matrix[r] = temp;
}
}

How do I traverse an array and sum every n elements in java? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to traverse an int array of length 100 and sum the value of every n elements and store the sum in a different array.
This is what I have so far:
currentNum = 100;
int[] sumArray = new int[100/n+1]
while(currentNum) {
for (int j = currentNum; j < currentNum + n;j++) {
sum += intArr[j];
}
currentNum = currentNum - n;
}
int[] sum(int[] array, int n) {
int sums[] = new int[(array.length + n - 1) / n];// thanks to #popovitsj
for (int i = 0; i < array.length; i++) {
if (i % n == 0)
sums[i / n] = 0;// initialize
sums[i / n] += array[i];// summarize
}
return sums;
}
first sums elements will hold sum of n elements, last - array.length % n elements

Categories