d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Method To Find Sum Of Adjacent Integers 2d Array > Help Is Appreciated
Add Reply New Topic New Poll
Member
Posts: 3
Joined: Apr 25 2014
Gold: 0.00
Apr 25 2014 05:03pm
Hi there,

I have a method that takes in two parameters, a 2d array and an int sumToGet
The method first creates a new 2d array with same length as the one taken in

The method goes through row by row, and adds adjacent elements until it hits the sumToGet. If it doesn't reach the sumToGet exactly, then all elements in that row will be 0.
Say for example the row is 5 6 5 4 3 4, the new row for the new array would be 5 6 5 4 0 0.
The method would do this for every row until there are no rows left.

(Any help as to how to progress my code is appreciated)

I've worked a bit and my code so far is:
Code

public static int[][] horizontalSums(int[][] a, int sumToFind)
{

int[][] b= new int[a.length][a.length];
int row=0;
do
{
int sum = 0; //reset the sum for each row
int col = 0;
do
{
sum+=a[row][col];
b[row][col]=a[row][col];
if(sum >= sumToFind)
{
return b;
}
col++; //move to the next col
} while(col<a.length); //stop if we're at the end of the row (max col reached)
row++; //move to the next row
} while(row<a.length); //stop if there are no more rows left

return b;
//you got to the end and the sum was never reached... return some value telling you that!



}


This post was edited by chanceRaps on Apr 25 2014 05:05pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 25 2014 05:33pm
i dont understand what you're trying to do. can you go over that example again, step by step?

Quote
The method goes through row by row, and adds adjacent elements until it hits the sumToGet. If it doesn't reach the sumToGet exactly, then all elements in that row will be 0.
Say for example the row is 5 6 5 4 3 4, the new row for the new array would be 5 6 5 4 0 0.


1) what is the sumtoget in your example?
2) why are only the last two columns 0? i thought you said the entire row will be 0?
Member
Posts: 3
Joined: Apr 25 2014
Gold: 0.00
Apr 25 2014 05:38pm
Quote (carteblanche @ Apr 25 2014 07:33pm)
i dont understand what you're trying to do. can you go over that example again, step by step?



1) what is the sumtoget in your example? to sumtoget in my example was 20.
2) why are only the last two columns 0? i thought you said the entire row will be 0?

and Im sorry i didnt elaborate well. If there are elements adjacent in the row that equal exactly the sumtoget, then that new row in the new 2d array will equal those elements, all other elements in that row will be 0.

So again for example, if my row was 5 6 5 4 3 4 in my old 2d array,
the method would go through this row and see that 5 6 5 4 equal 20.
It would assign these elements to the same index as the old 2d array into the new 2d array.
the 3 and 4 would turn to 0 in the new array.

If none of the adjacent elements equal the sum to get, THEN the row will equal all 0.

And again, this would go row by row. Hopefully that helped, Thank you for looking and trying to help me.

This post was edited by chanceRaps on Apr 25 2014 05:39pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 25 2014 05:57pm
assuming you have all positive integers:

Code
int[] createRow(int[] row, int magicnum){
start = 0;
last = 0;
int[] newarray = new int[row.length]; // set to all zeros

// start with the first element
sum = row[0];
if (sum == magicnum){
// the sums meet our magic number. copy to new array
newarray[from start to last] = row[from start to last]
// move our start pointer over
} else if (sum > magicnum){
// we're too high. drop the left-most value by shifting over 1
} else if (sum < magicnum){
// we're not high enough. extend our right-most value by shifting over 1
}

// if our left pointer is passed our right pointer, increment our right pointer
// if right pointer is past the array, we're done
// recalculate sum
// repeat

return newarray;
}


i'm taking a greedy approach. idea is our sum spans from a left pointer to the right pointer. to increase our sum, we move the right pointer to have a larger span. to decrease our sum, we move the left pointer to have a shorter span. if you have zero/negatives, you'll have to adjust the algorithm.

This post was edited by carteblanche on Apr 25 2014 06:01pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll