I need help in finding the max and min value of total of 2d array if possible please

• May 23rd, 2011, 06:30 PM
AliAliAli
I need help in finding the max and min value of total of 2d array if possible please
Hi everyone
I thank you a lot for taking the time to read my code
I need to display a two dimensional array, using two classes, calculate the total of each row, and display the maximum total and the minumum total( I was able to do everything except for the maximum and minimum) and I have to submit my project on wednesday, tried my best and thats what I could formulate, please help
This is my code for the first class:
Code :

```package companyproject; public class company { private int [][] arrayname={{88,45,73},{33,45,58},{58,45,27},{28,45,55}}; private int max=0; private int min=0; public void Output () { for (int x=0; x<3; x++) {System.out.printf("\tProduct %d \t", x+1); } System.out.printf("Total"); for (int row=0; row<arrayname.length; row++) {System.out.println(""); System.out.printf("Salesperson %d\t", row+1); for (int column=0; column<arrayname[row].length; column++) System.out.printf("%d\t\t",arrayname[row][column]); int total = totalx (arrayname[row]); System.out.printf("%d", total); } } public int totalx (int [] arrayname) { int total=0; for (int aaa=0; aaa<arrayname.length; aaa++) {total=total+arrayname[aaa];} if (total > max) {max=total; } System.out.printf("The maximum number is %d", max); if (total < min) {min=total;} System.out.printf("The minimum number is %d", min); return 0; } }```
Thats the code for the second class in which I should display the first class:
Code :

```package companyproject;   public class companytext { public static void main (String [] args)   {   company totally = new company ();   totally.Output();   }     }```
Unfortunately thats what Im getting as an output
http://imageshack.us/m/121/3959/outputhx.png
The maximum number is 206The minimum number is 00
after each row (Under total), while I should get the sum of the row

I should get the maximum number and minimum number under the 2d array once
I thank you a lot again for dedicating your time to help me :)
• May 23rd, 2011, 08:10 PM
Norm
Re: I need help in finding the max and min value of total of 2d array if possible ple
You should try debugging your code by adding println statement to show the values of variables as they change and to show logic flow.

Can you show what the output should look like? I don't understand your comments.

Cross posted at
I need help in finding the max and min value of total of 2d array if possible please - Dev Shed
• May 24th, 2011, 04:52 AM
AliAliAli
Re: I need help in finding the max and min value of total of 2d array if possible ple
Yes dear, I posted this post in another java forum but MrFujin was not able to help because that other forum didn't allow the attachment of images
• May 24th, 2011, 05:04 AM
AliAliAli
Re: I need help in finding the max and min value of total of 2d array if possible ple
• May 24th, 2011, 07:18 AM
Norm
Re: I need help in finding the max and min value of total of 2d array if possible ple
One trick that is useful in finding the max and the min of a value is to initialize the starting value for the max to a number smaller than the smallest data value and the starting value for the min to a number BIGGER than the biggest data value.

If you change this variable so that the totals are changed:

private int [][] arrayname={{88,45,73},{33,45,58},{158,45,27},{28,4 5,55}};

and look at the output, you'll get some info on how your program is working.

You need to step thru your logic and see why the output is being printed the way it is.
• May 24th, 2011, 10:39 AM
JavaPF
Re: I need help in finding the max and min value of total of 2d array if possible ple
Quote:

Originally Posted by Norm
You should try debugging your code by adding println statement to show the values of variables as they change and to show logic flow.

Debugging With System.out.println()