-1

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.

Please help this is homework!

Milmo
  • 3
  • 1
  • Possible duplicate of [Java: Finding the highest value in an array](http://stackoverflow.com/questions/1806816/java-finding-the-highest-value-in-an-array) – Pinkie Swirl Oct 05 '16 at 02:36
  • Hello Milmo. It appears the teacher has given you a shell to work with. You could copy and paste some code that works, but then you won’t learn anything. To help, using the supplied program, run it a few times, see what is going on with each step, better yet trace it if you are using an IDE. Then think about what tools you have learned that could help you solve the problem. Most here are not going to do your homework and you are doing yourself a disservice if you don’t try. Try some stuff and see what happens. It will break/not work/crash YES… but that’s how you learn. – JohnG Oct 05 '16 at 02:51
  • Thank you, I know, unlike most people I do take my classes with the intent of learning. Your advice is much appreciated. – Milmo Oct 05 '16 at 03:18

3 Answers3

0

One possible strategy for finding a max value of an unsorted array is to go through the array (using a for loop, perhaps), and keeping track of the maximum value as you go through it.

I don't want to do your homework, but that might look like:

   max = Integer.MIN (the smallest possible integer value)

   for item in array
       if item is greater than max
            store item in max

This strategy should find the max value -- hope this helps you! Model the for loops and the print statements like the previous ones you've shown.

midwestcode
  • 23
  • 2
  • 7
  • Thank you for your answer however we have learned very little about class wrapping (I think that's what its called) and so I would prefer to accomplish this by using more a rudimentary approach. – Milmo Oct 05 '16 at 03:19
0

The key here is to remember that the sum of all sales and max number of sales can be calculated each time a new salesman's entry is inputted. On the first entry, for example, salesman 0 has N sales -- therefore the sum is N and the max is N. On each consecutive entry, you can update the sum by adding the value to it, and you can update the max if the entry's value is greater than the currently known maximum.

vase
  • 339
  • 1
  • 6
  • Thank you, this was a step in my thought process but your explanation made it a bit clearer in my mind. – Milmo Oct 05 '16 at 03:20
0

Because it's your homework, I won't give you the direct answer, but instead give you some hints. If you add two variables, say,

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

then you update the two variables based on the current sale value in one of the loops and print out them at the end.

Shiping
  • 1,203
  • 2
  • 11
  • 21
  • Thank you! I believe I can use this to make the program work in some way, thank you for helping. – Milmo Oct 05 '16 at 03:21