# Maximum and Minimum Value in Arrays

Page 1 of 1

## 5 Replies - 8381 Views - Last Post: 11 November 2009 - 01:50 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=138126&amp;s=7c1594c5310dd4a46472ead7dc187c76&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 EGutierrez91

Reputation: 1
• Posts: 51
• Joined: 10-September 09

# Maximum and Minimum Value in Arrays

Posted 10 November 2009 - 01:35 PM

Trying to obtain the maximum and minimum value in the array but I'm unsure as to how to check each value and compare them to one another.

```/*
- Find and print the maximum sale. Print both the id of the salesperson with the max sale and the amount of the sale,
e.g., "Salesperson 3 had the highest sale with \$4500." [Use the 2nd for loop - don't create a new one.

- 	Do the same for the minimum sale.*/
// ****************************************************************
// Sales.java
//
// Reads in and stores sales for each of 5 salespeople.  Displays
// sales entered by salesperson id and total sales for all salespeople.
//
// ****************************************************************
import java.util.Scanner;

public class Sales
{
public static void main(String[] args)
{
final int SALESPEOPLE = 5;
int[] sales = new int[SALESPEOPLE];
int sum;
double avg;

Scanner scan = new Scanner(System.in);

for (int i=0; i < sales.length; i++)
{
System.out.print("Enter sales for salesperson " + i + ": ");
sales[i] = scan.nextInt();
}

System.out.println("\nSalesperson   Sales");
System.out.println("--------------------");
sum = 0;

for (int i=0; i<sales.length; i++)
{
System.out.println("	 " + i + "		 " + sales[i]);
sum += sales[i];
}

System.out.println("\nTotal sales: " + sum);

avg = sum/SALESPEOPLE;

System.out.println("Average sales: " + avg);

}
}

```

Is This A Good Question/Topic? 0

## Replies To: Maximum and Minimum Value in Arrays

### #2 japanir

• jaVanir

Reputation: 1014
• Posts: 3,025
• Joined: 20-August 09

## Re: Maximum and Minimum Value in Arrays

Posted 10 November 2009 - 01:43 PM

hi
lets take the max value for example.
declare a variable maxSale as
```int maxSale;

```

iterate your array, for each value, check if it is bigger than maxSale. if it is, replace the value of maxSale with the bigger value from the array.
as:
```maxSale = sales[0];
for(int i =1;i<sales.length;i++){
if(sales[i] > maxSale){
maxSale = sales[i];
}
}

```

Thats it. now max hods the biggest value.
same for the minSale, just check if sales[i] is smaller.

### #3 EGutierrez91

Reputation: 1
• Posts: 51
• Joined: 10-September 09

## Re: Maximum and Minimum Value in Arrays

Posted 10 November 2009 - 08:36 PM

That makes sense! Thanks!

Problem is, how do I get it to print out which seller had the highest sale.

i.e.. Salesman 0 had the most sales at 2323. I have to print. "Salesman 0 had the most sales at 2323." I would assume the same logic would apply to printing out the salesman with the least sales.

### #4 WaHooCrazy7

Reputation: 3
• Posts: 19
• Joined: 09-November 09

## Re: Maximum and Minimum Value in Arrays

Posted 10 November 2009 - 08:53 PM

EGutierrez91, on 10 Nov, 2009 - 07:36 PM, said:

That makes sense! Thanks!

Problem is, how do I get it to print out which seller had the highest sale.

i.e.. Salesman 0 had the most sales at 2323. I have to print. "Salesman 0 had the most sales at 2323." I would assume the same logic would apply to printing out the salesman with the least sales.

All you have to do is add one more tracker to things. Here is the code from japanir slightly modified:
```int manId;
maxSale = sales[0];
manId = 0;
for(int i =1;i<sales.length;i++){
if(sales[i] > maxSale){
maxSale = sales[i];
manId = i;
}
}

```

Then when you make your print statement you can simply add the "manId" to things to get what you want.

### #5 EGutierrez91

Reputation: 1
• Posts: 51
• Joined: 10-September 09

## Re: Maximum and Minimum Value in Arrays

Posted 11 November 2009 - 12:51 PM

THANKS! Works perfectly now! I appreciate the help!

### #6 WaHooCrazy7

Reputation: 3
• Posts: 19
• Joined: 09-November 09

## Re: Maximum and Minimum Value in Arrays

Posted 11 November 2009 - 01:50 PM

EGutierrez91, on 11 Nov, 2009 - 11:51 AM, said:

THANKS! Works perfectly now! I appreciate the help!

not a problem! glad I could help