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

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2011
Posts
4
Rep Power
0

#### 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:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package companyproject;

/**
*
* @author HP
*/
public class company {

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

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 max =0;
int total=0;

for (int aaa=0; aaa<arrayname.length; aaa++)

{total=total+arrayname[aaa];}
if (total > max)
{max=total;}
return max;

}

}

Thats the code for the second class in which I should display the first class:

package companyproject;

/**
*
* @author HP
*/
public class companytext {
public static void main (String [] args)

{

company totally = new company ();

totally.Output();

}

}
2. First, please use code tags when posting code.

the max is declare inside the totalx function, which means it will be reset to 0 each you call the function.
Instead, you should declare the max value as an instance variable, similar to what you have with arrayname. Doing this should give you the correct max value.

It works almost the same to get the minimum, but instead you will check if total is lower than min. (total < min).
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2011
Posts
4
Rep Power
0
Hi dear MrFujin
I thank you a lot for your kind help, yet forgive me that I couldnt implements it, this is my modification
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;
}
}```
Thannkyou sooo much for your kind help, I really appreciate it
4. One advice when posting code, copy/paste it into the textbox before you use the code button.

Why couldn't you implement it? Do you get any error message? unexpected result? what did you expect?
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2011
Posts
4
Rep Power
0
Im really sorry for getting you in a lot of trouble for me dear MrFujin
I cant insert an image into the reply unfortunately because Im a new user
But what I get is
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
6. I think you should rename the variable total to for example row_sum or row_total.
In totalx you have specified it to print out the max and min values.

Regarding the min value, you have assigned it with the value of 0 from the beginning. This will make only negative number to be lower than min.
Take a look at the Integer MAX and MIN value.
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2011
Posts
4
Rep Power
0
I got the maximum value, thank you soo much for your kind help