I am looking for a more elegant solution to the following scenario (language doesn't matter, Java is fine, but I am currently in C#). Suppose a linear array is coming in and being displayed as a table with X items per row (For instance, 9 items coming in at 3 items per row, so 3 rows of 3). If the array is being indexed 0 through 8, it is currently displayed as such:
6 7 8
3 4 5
0 1 2
So, the elements are displayed left to right, bottom to top. I would like to rearrange this to be displayed top to bottom, like this:
0 1 2
3 4 5
6 7 8
This requires reordering the array so that the new array's indexes are [6,7,8,3,4,5,0,1,2], with respect to the original's indexes. My current (untested) solution is the following: assume the array to be returned is 'array' and a temporary copy is 'temp', and the 'cols' variable is already the number of items per row.
int rows = (array.Length + cols - 1) / cols; //ceiling function to determine rows needed
int pos = array.Length - cols; //starting position in index transfer
int offset = 0; //needed when negative index reached
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (pos + j >= 0)
array[cols * i + j - offset] = temp[pos + j]; //assign values of temp to array
else
++offset; //takes care of negative indeces
}
pos -= cols;
}
return array;
The problem is this code is quite unreadable and possibly inefficient because of the double loop, though I don't expect many more than 9 items to come through. Is there a more elegant solution to this using slicing up the array, reversing, or anything that isn't so difficult to read? It's just a fun little problem I've been thinking about for an issue at work. Anybody's input is appreciated, thanks!
It is worth noting that potentially uneven tables can be created (For instance, 9 elements with 4 items per row creates 2 rows of 4, one row of 1. The 'offset' variable is used to protect negative array indexing if this is the case).
This is what I came up with: it doesn't sort your original array at all, but every inner loop starts at a computed starting-index based on the row you're printing. Let me know if you have any questions. This isn't specific to you, more specific to your situation, but the concept and functionality is there.
int[] array = new int[9] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
int rows = 3;
int cols = 3;
int count = 0;
for (int x = 1; x <= rows; x++)
{
int startingPos = array.Length - (x * rows);
for (int y = cols; y > 0; y--)
{
Console.Write(array[startingPos] +", ");
count++;
startingPos++;
}
Console.WriteLine();
}
Related
i am trying to formulate a for loop that will take an array, for instance of 5 elements, and will allow me to treat a[0] as if it is after a[4], and a[4] as if it was before a[0].
I cannot change the array, and it stores a thread in each element, so i would rather make it as simple as possible to not corrupt the contents of the thread(threads are synchornized and using reentrantlock - so the question is only about array).
I want to make this simple for loop:
for (int i = 0; i < ARRAYSIZE; i++)
to allow me to treat it as if it was a cyclic array. I thought of using the mudolo operation to achieve that, but that doesn't work either. here's what i tried:
for (int i = i+1 % n; i < ARRAYSIZE; i++)
but that doesn't work as well. What I am trying to do is basically check if array[i] is larger than array[i+1] or array[i-1].
would appreciate your assistance.
Use the modulo operator on the loop variable i by the size of the array:
public static void main(String [] args) {
int [] arr = {1, 5, 4, 3, 3, 4, 3, 1};
int ARRAYSIZE = arr.length;
for (int i = 0; i < ARRAYSIZE; i++) {
int index = i % ARRAYSIZE;
int indexUpper = (i + 1) % ARRAYSIZE;
//access array using index
if (arr[index] == arr[indexUpper]) {
System.out.format("Elements %d and %d are equals.\n", index, indexUpper);
}
}
}
Note how for the upper value you want to cycle through, you need to do (i + 1) % ARRAYSIZE to ensure you get the next element. To get the element two places over, add 2 instead, or whatever modifier you choose.
This test shows how elements 7 and 0 are equal because it is cyclical.
Output:
Elements 3 and 4 are equals.
Elements 7 and 0 are equals.
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;
}
}
Hello I have searched for a simple way to check ,
if any number of elements up to 6 in the array add up to seven. I have yet to find one my array is this,
private int[] diceRoll = new int[6];
The question is a bit vague, however, here's my attempt at an answer:
If what you're trying to do is take two indices x and y in diceRoll[] and see if they add up to 7, the simplest thing to do is
if(diceRoll[x] + diceRoll[y] ==7){
return true;}
If you're trying to see if ANY item with any other item adds up to 7, use a double for-loop (these are weird, but very helpful)
for(int i = 0; i < diceRoll.length; i++){
for(int j = 0; i < diceRoll.length; i++){
if(diceRoll[i] + diceRoll[j] != 7){
return false;
}
}
}
Hope this helps!
-katie
It sounds like what you need to do is take every subset of the diceRoll array and see which ones add up to 7. This is how it can be done.
Assuming you know that 1 & 1 = 1, and that 1 & 0 = 0, imagine each element of the array having a number in 0 0 0 0 0, if the element is selected, say element 5, the subset representation in binary form would be 0 0 0 0 1. If element 2 and 3 are selected, the subset representation would be 0 0 1 1 0. If you take a binary one, keep track of its index, and move it from right to left in the array computing index&1 each time, you can get which indexes of the array are in the current subset (if the index&1 computation results in a 1 for that index). Translating this to a smaller array called currSubset, you can sum it up and check if it is equal to 7.
The termination of the outer for loop comes from the maximum value of a 5 digit binary number, which is 11111 = 31 = 2^5-1, hence the use of the less than sign.
int sum = 0;
int index = 0;
ArrayList<ArrayList<Integer>> subsetsThatAddTo7 = new ArrayList<ArrayList<Integer>>();
for(int subsetRep = 0b00001; i < Math.pow(2,5); i++){
ArrayList<Integer> currSubset = new ArrayList<Integer>
for(index = 0; index < 5; index++){
if(subsetRep & (1 << index))
currSubset.add(diceRoll[5-index]);
}
int sum = 0;
for(int num : currSubset)
sum += num;
if(sum == 7)
subsetsThatAddTo7.add(currSubset);
}
I have two java 2D arrays(array1 and array2), both of type Double which I would like to join together.
array1 and array2 are n-by-d matrices. the n-rows for each array can vary from eachother but the d-columns are always the same.
The question is: i have to concatenate array2 after the first one.(at best into the new outputMatrix)
Example:
array1:
2 3
6 5
4 7
array2:
1 5
3 7
Output Array (n-array1 + n-array2)-by-d:
2 3
6 5
4 7
1 5
3 7
My idea was to copy array1 into the outputMatrix, and then fill the continuation of it with array2.
My Java code for merging the two arrays(that are already implemented) looks like:
private static double[][] outputMatrix ;
outputMatrix = new double[array1.length+array2.length][array1[0].length];
for (int column = 0; column < array1[0].length; column++) {
for (int row = 0; row < (array1.length); row++) {
outputMatrix[row][column] = array1[row][column];
}
}
for (int column = 0; column < array1[0].length; column++) {
for (int row = array1.length; row < (array1.length+ array2.length); row++) {
outputMatrix[row][column] = array2[row][column]; //error here as array2 starts iterating at field [4][0] which isnt defined in the example
}
}
Your indices are wrong.
Should be
for (int column = 0; column < array1[0].length; column++) {
for (int row = 0; row < array2.length; row++) {
outputMatrix[row+array1.length][column] = array2[row][column];
}
}
since array2 has indices from 0 to array2.length-1.
So what's happening is that when you are trying to transfer the data from the second array into the outputMatric variable, you go out of bounds in the second array (array2). This is due to the fact that you're outputMatrix is trying to put a value at [array1.length + row][array1.length + column] and the second array isn't as big as that one. On top of that, your row index is wrong, you need to add the array1 length to make up for it or start the for loop's row variable at that spot. I elected just to add the length into the storing of the variable. So, you should make your code:
outputMatrix[row + array1.length][column] = array2[row - array1.length][column - array1[0].length];
I'm trying to create a method that will search through a 2d array of numbers. If the numbers add up to a certain sum, those numbers should remain and all of the other numbers should be changed to a 0. For example, if the desired sum is 7 and a row contains 2 5 1 2, the result should be 2 5 0 0 after the method is implemented. I have everything functioning but instead of keeping all of the numbers that add up to the sum, only the last number is retained. So, I am left with 0 5 0 0 . I think I need another array somewhere but not sure exactly how to go about implementing it. Any ideas?
public static int[][] horizontalSums(int[][] a, int sumToFind) {
int[][] b = new int[a.length][a[0].length];
int columnStart = 0;
while (columnStart < a[0].length) {
for (int row = 0; row < a.length; row++) {
int sum = 0;
for (int column = columnStart; column < a[row].length; column++) {
sum += a[row][column];
if (sum == sumToFind) {
b[row][column] = a[row][column];
}
}
}
columnStart++;
}
return b;
}
In your example you use 2 5 1 1, would 0 5 1 1 also be a valid response? Or do you just need to find any combination? A recursive function may be the best solution.
If you just need to scan through the array and add up the numbers until the sum is reached then just add a for loop to copy the previous values from the array to the new array when the sum is found. Something like:
if (sum == sumToFind)
{
for (int i= 0; i<= columnStart; i++)
{
b[row][i] = a[row][i];
}
}
if (sum == sumToFind)
{
for (int i= columnStart; i<= column; i++)
{
b[row][i] = a[row][i];
}
}
A minor tweak was all it needed. If you have columnStart and column like in the other answer, it only finds the first number of the series.