Milmo - 1 year ago 152
Java Question

# How to find the max value of an unsorted array

I am really new to java and I signed up for an AP class which is being taught very badly, and so I have no idea of how to do this part of the assignment. It prampts you to add code that will do the following

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.” Note that you don’t need another loop for this; you can do it in the same loop where the values are read and the sum is computed.

the code we are given is the following

import java.util.Scanner;
public class Sales {

``````public static void main(String[] args) {
final int salesPeople = 5;
int[] sales = new int[salesPeople];
int sum;

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("     " + "     " + sales[i]);
sum += sales[i];

}

System.out.println("\nTotal sales: " + sum);
System.out.println("Average sale " + sum/salesPeople);
}
``````

}

I really do not know what to do and would appreciate a nudge in the right direction as to how to find the max and the id of the person who produced the max.

``` int heighestSale = -1; // assume the sales are not negative or at least one sale is not negative int heighestPerson = -1; ```