Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Need help with sums of rows and columns of two dimensional arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with sums of rows and columns of two dimensional arrays" Watch "Need help with sums of rows and columns of two dimensional arrays" New topic
Author

Need help with sums of rows and columns of two dimensional arrays

Andrew Harry
Greenhorn

Joined: Nov 23, 2012
Posts: 2
I have a two dimensional array that contains integers ranging from 1 to 9. What I'm trying to do is in each row go through it and add up each number to 20 and print out a new array with those numbers. If a number used makes it so that it does not equal 20 that number is now 0. for an example:

Input Array:
8 4 5 4 3 7 1 2 1 8
6 8 6 7 8 6 2 4 6 2

Horizontal sum output would be:
0 0 5 4 3 7 1 0 0 0
6 8 6 0 8 6 2 4 6 2

8 6 2 4 6 2 contains two overlapping sums that equal 20: 8 + 6 + 2 + 4 and 6 + 2 + 4 + 6 + 2

Input Array:
9 5
9 6
6 2
9 6
2 1
9 7
1 8
6 2
8 5
5 8

Vertical sum output would be:
0 5
0 6
0 2
9 6
2 1
9 0
1 0
6 0
8 0
5 0

I can't for the life of me think of an algorithm that would come up with a correct answer.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2169
    
  47
Write down in words exactly how you solved it in the examples you showed - there's your algorithm.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37945
    
  22
And welcome to the Ranch
There is a mistake in the first thing you wrote. You don’t have a 2D array; there ain’t no such critter in Java. You have an array of arrays, (which is actually better than a 2D array).
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Andrew Harry wrote:
I can't for the life of me think of an algorithm that would come up with a correct answer.


Having read this topic three times, I still can't figure out what you are trying to say. Perhaps if you can better describe the algorithm, so that we can understand it, then they can be translated to pseudo code, which can be used to translate to actual code.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Henry Wong wrote:
Andrew Harry wrote:
I can't for the life of me think of an algorithm that would come up with a correct answer.


Having read this topic three times, I still can't figure out what you are trying to say. Perhaps if you can better describe the algorithm, so that we can understand it, then they can be translated to pseudo code, which can be used to translate to actual code.



Ohhh.... I see.

The algorithm looks pretty straightforward. Start at index zero. At the current index, count forward until you either you get 20, or exceed 20. If you get 20, then mark the whole range (of all the involved numbers). And here is the trick, even though a number is marked, you don't get to skip it. You still need to only go one index at a time, as any number can be marked more than once, as part of different ranges.

Henry
Andrew Harry
Greenhorn

Joined: Nov 23, 2012
Posts: 2
Figured it out, thanks guys!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Need help with sums of rows and columns of two dimensional arrays
 
Similar Threads
Arrays in Java
Forcing horizontal display
error using 3d array
Something i'm extremely confused on
Sorting arrays while retaining original index