# Finding the max and min with an array and getting the index of each.

Page 1 of 1

## 5 Replies - 762 Views - Last Post: 19 October 2012 - 03:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=296319&amp;s=8d10dee57d4ca6388cfa7b6a9461e505&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 graFight

• D.I.C Head

Reputation: 1
• Posts: 125
• Joined: 10-August 12

# Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 12:49 PM

I used an array to find the max and min without much trouble but i need to find which index of the max is and same for the min (hopefully i described that right) here's my code:

```public class Sales
{
public static void main(String[] args)
{

final int SALESPEOPLE = 5;
int[] sales = new int[SALESPEOPLE];
int sum;
int max;
int min;
int bestSeller = 0;
int worstSeller = 0;

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;
min = sales[0];
max = sales[0];

for (int i=0; i<sales.length; i++)
{

System.out.println("     " + i + "         " + sales[i]);
sum += sales[i];

if (max < sales[i])
{
max = sales[i];
}

if (min > sales[i])
{
min = sales[i];
}

}

System.out.println(min);
System.out.println(max);
System.out.println("Average sales: " + sum / SALESPEOPLE);
System.out.println("\nTotal sales: " + sum);

}
}

```

basically i would need to declare to variables (bestSeller, worstSeller) and then print out which seller it is like:

System.out.println("Salesman " + bestSeller + " has the greatest sales at " + max);

Example print: Salesman 4 has the greatest sales at 1000.

try not to just give the answer, and rather just a clue at where to start if possible. thanks

Is This A Good Question/Topic? 0

## Replies To: Finding the max and min with an array and getting the index of each.

### #2 Kakerergodt

• D.I.C Head

Reputation: 89
• Posts: 201
• Joined: 01-May 12

## Re: Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 12:52 PM

Easiest would probably be to just keep to extra variables min/maxIndex and update them when you update max and min.
Was This Post Helpful? 0

### #3 Dirnol

• New D.I.C Head

Reputation: 15
• Posts: 31
• Joined: 15-October 12

## Re: Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 12:56 PM

Look at how the value max and min are set. If only there were some way to save the index those values use...
Was This Post Helpful? 0

### #4 graFight

• D.I.C Head

Reputation: 1
• Posts: 125
• Joined: 10-August 12

## Re: Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 02:42 PM

Kakerergodt, on 19 October 2012 - 12:52 PM, said:

Easiest would probably be to just keep to extra variables min/maxIndex and update them when you update max and min.

this is what i was implying by having the variable bestSeller and worstSeller

Dirnol, on 19 October 2012 - 12:56 PM, said:

Look at how the value max and min are set. If only there were some way to save the index those values use...

ya that is basically what i can't figure out with what i have been taught already...i thought you could use something like

bestSeller = max[index]

but that's trying to access an array that isn't there...basically just me trying to make up my own java syntax lol
Was This Post Helpful? 0

### #5 karabasf

• D.I.C Regular

Reputation: 202
• Posts: 417
• Joined: 29-August 10

## Re: Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 02:50 PM

So what you want is to store the index of where the worst and best sellers are located in the array. Well, we need to store the index, but we also need to determine what the maximum value is then.

In pseudocode, this looks like:

```// Set the initial values for the max and minumum values
min = sales[0];
max = Parse(char at userInput(0)); //Same here
indexMin = 0;
indexMax = 0;

// Traverse the array
for i = 1 to length sales
currentNumber = sales(i); //Acces the entry i of the array

//If the current number is higher than max, set max to that. Also set the index
if(currentNumber > max)
max = currentNumber; //Sets the maximum value
indexMax = i; //Sets the index of the maximum number to current index
endif

//If the current number is higher than max, set min to that. Also set the index
if(currentNumber < min)
min = currentNumber; //Sets the minimum value
indexMin = i; //Sets the index of the minimum value to current index
endif
endfor

//Do whatever you want with your variables

```

Now it's up to you to translate this into Java
Was This Post Helpful? 0

### #6 graFight

• D.I.C Head

Reputation: 1
• Posts: 125
• Joined: 10-August 12

## Re: Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 03:10 PM

karabasf, on 19 October 2012 - 02:50 PM, said:

So what you want is to store the index of where the worst and best sellers are located in the array. Well, we need to store the index, but we also need to determine what the maximum value is then.

In pseudocode, this looks like:

```// Set the initial values for the max and minumum values
min = sales[0];
max = Parse(char at userInput(0)); //Same here
indexMin = 0;
indexMax = 0;

// Traverse the array
for i = 1 to length sales
currentNumber = sales(i); //Acces the entry i of the array

//If the current number is higher than max, set max to that. Also set the index
if(currentNumber > max)
max = currentNumber; //Sets the maximum value
indexMax = i; //Sets the index of the maximum number to current index
endif

//If the current number is higher than max, set min to that. Also set the index
if(currentNumber < min)
min = currentNumber; //Sets the minimum value
indexMin = i; //Sets the index of the minimum value to current index
endif
endfor

//Do whatever you want with your variables

```

Now it's up to you to translate this into Java

Thanks so much i got it now and i believe it is working properly...

```public class Sales
{
public static void main(String[] args)
{

final int SALESPEOPLE = 5;
int[] sales = new int[SALESPEOPLE];
int sum;
int max;
int min;
int bestSeller = 0;
int worstSeller = 0;
int currentSeller = 0;

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;
min = sales[0];
max = sales[0];

for (int i=0; i<sales.length; i++)
{

System.out.println("     " + i + "         " + sales[i]);
sum += sales[i];
currentSeller = sales[i];

if (max < sales[i])
{
max = sales[i];
bestSeller = i;
}

if (min > sales[i])
{
min = sales[i];
worstSeller = i;
}

}

System.out.println("The lowest seller is Salesman " + worstSeller + " with sales at " + min);
System.out.println("The top seller is Salesman " + bestSeller + " with sales at " + max);
System.out.println("Average sales: " + sum / SALESPEOPLE);
System.out.println("\nTotal sales: " + sum);

}
}

```

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }