programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Finding the row in a matrix with maximum sum of its elements

Khusbu Sinha
Ranch Hand
Posts: 117
1
I am writing the code to find the row in a matrix with maximum sum of its elements. I intend to solve this problem in the following manner:
Step 1: Finding the sum of each row
Step 2: Storing the above individual sum values in an array b
Step 3: Creating a copy`c` of array`b`
Step 4: Sorting c
Step 5: Iterating through b to find the element matching with the last (i.e. maximum) element of sorted array` c`
Step 6: Displaying the index of the above element of array `b`

My code could only solve this problem till Step 1. I am stuck in Step 2 and could not proceed further. I don't know how to implement Step 2 in my program. Please help.
Below is my code:

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
before you do that, i'd strongly suggest you refactor this code. It's really not a good idea to have everything in your main method.  You want to create methods that have a clearly defined responsibilities.  You'd want methods taht do things like:

get input from user
populate the array.
compute the sum of a row (so maybe you'd pass in an array, and a row number, and it returns the sum)
etc.

one thing I'd suggest...the copying and sorting is really overkill.  why not:

sum each row.
If the sum of the current row is greater than any sum you've seen yet, remember this new sum, and the index of the current row.

There's some other little things you need to handle, like making sure the first row is saved as the largest, but that's usually not too hard...

Khusbu Sinha
Ranch Hand
Posts: 117
1
I am having the following problems in breaking down this code into methods:
1. Scope of the user input values and user input array limited to a particular method makes it difficult for me to use their values inside another method.
2. I am not very sure how to pass an array as an argument in a method.

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Khusbu Sinha wrote:1. Scope of the user input values and user input array limited to a particular method makes it difficult for me to use their values inside another method.

Not if you do it right.  ;-)

you should make your array a member of the class.  Then, any method of the class can access it. You would then, of course, have to create an instance of your class, but this is the correct way to to it in any case. Try reading this and see if that helps.

Khusbu Sinha
Ranch Hand
Posts: 117
1
Thanks Fred.  I am still in the process of understanding the tutorial in the link posted by you. Meanwhile, I have updated my code according to the tutorial (from whatever part I think could understand properly). But I still have problem in passing array in methods. I get errors in Line 26 and Line 36 as follows:

CandidateCode.java  [line: 26] Error: '.class' expected
CandidateCode.java  [line: 36]Error: illegal start of expression

Kindly let me know where I am going wrong in this code.

Following are my class files:

CandidateCode.java

RowMax.java

Khusbu Sinha
Ranch Hand
Posts: 117
1
I re-checked this code. I corrected the following lines of CandidateCode.java:
Line 27: from to
Line 29: from to
Line 40: from to
I still get the same errors while compiling:
[line: 26]Error: '.class' expected
[line: 27]Error: '.class' expected
[line: 29]Error: '.class' expected
[line: 36]Error: illegal start of expression

Here is my current program:

CandidateCode.java

RowMax.java

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
• 1
proper formatting of your code is really critical to understanding what is going on.  I've reformatted it here from your earlier post

You can see here that your rowSum method is being delcared INSIDE your run method...that's not allowed.

I think you're trying to code too much at a time. NEVER write more than 2-3 lines of code before you recompile.  When you start a new method, just write the bare-bones skeleton - it may even be completely empty - and compile. Had you dont that, you'd have found this error much sooner, when your code was cleaner.

Also, this is generally a bad idea:

Even though they arene't requires, use the curly brackets anyway.  you are bound to someday want to put something else in the body. If you forget to then add the curlies, you'll be very confused...trust me...I speak from experience. It costs you nothing to put them in now, and helps you see what EXACTLY is included in the loop body.>

Khusbu Sinha
Ranch Hand
Posts: 117
1
Thanks Fred for formatting my badly formatted code. From now on, I will keep formatting in mind while coding.
However, I still don't understand why I am getting errors when I pass an array to a method as an argument. Are there some special rules for arrays when they are passed as arguments in a method?

I get errors in lines 27, 28 and 30 all of which requires the array to be passed to the method.

Knute Snortum
Sheriff
Posts: 4281
127
If you want to pass the whole array, pass just its name.  But your method is looking for an int.  To pass a specific element of an array, add the indices (indexes) you need.

Knute Snortum
Sheriff
Posts: 4281
127
Ah, I see it now.  This line is wrong too:

It should be:

Khusbu Sinha
Ranch Hand
Posts: 117
1
Thanks Knute, for the correction. My program now runs successfully.
Here is the final code:

CandidateCode.java

RowMax.java