Reputation: 173
I am confused on how to access the array temp in order to compare the current array element temp[i] to max sales which is 0 in order to determine which is bigger,each time i try i cannot access temp IN STEP 8 and STEP 7, i do not want to change the visibility of the classes
public class Sales {
public static void main(String[] args) {
int[] sales;
sales = getSales();
printSales(sales);
printSummary(sales);
}
private static int[] getSales() {
Scanner input = new Scanner(System.in);
int[] temp;
System.out.print("Enter the number of salespeople: ");
temp = new int[input.nextInt()]; // Step 1
for (int i = 0; i < temp.length; i++) // Step 2
{
System.out.print("Enter sales for salesperson " + (i + 1) + ": ");
temp[i] = input.nextInt(); // Step 3
}
return temp; // Step 4
}
private static void printSales(int[] s) {
System.out.println();
System.out.println("Salesperson Sales");
System.out.println("----------- -----");
for (int i = 0; i < 5; i++) // Step 5
{
System.out.printf("%6d%12d\n", i + 1, s[i]); // Step 6
}
}
private static void printSummary(int[] s) {
int sum = 0;
int max_sale = 0; // Salesperson with the most sales
int min_sale = 0; // Salesperson with the least sales
for (int i = 0; i < ________; i++) // Step 7
{
____________ // Step 8
}
System.out.println();
System.out.println("Total sales: " + sum);
System.out.println("Average sales: " + (double) sum / s.length);
System.out.println("Salesperson " + (max_sale + 1) + " had the maximum sale with " + s[max_sale]);
System.out.println("Salesperson " + (min_sale + 1) + " had the minimum sale with " + s[min_sale]);
}
}
Upvotes: 1
Views: 96
Reputation: 173
This works at step 7/8 thanks to @andrew
int max = 0;
int min = Integer.MAX_VALUE;
for (int i= 0; i < s.length; i++) {
sum += s[i];
if (s[i] > max) {
max_sale = i;
max = s[i];
}
if (s[i] < min) {
min_sale = i;
min = s[i];
}
}
Upvotes: 0
Reputation: 60
Took Andrew Tobilko code and changed it to save the index to the person that has highest sale value. Implementing this replacing your loop will work.
for (int i = 0; i < s.length; i++) { // STEP 7
if (s[i] > s[max_sale]) max_sale = i; // STEP 8
if (s[i] < s[min_sale]) min_sale = i;
sum += s[i];
}
Upvotes: 2
Reputation: 49656
temp
is a local variable that had been created in the main
and passed to your printSummary(int[] s)
method, so there you can access it by using s
.
for (int i = 0; i < s.length; i++) { // STEP 7
if (s[i] > max_sale) max_sale = s[i]; // STEP 8
if (s[i] < min_sale) min_sale = s[i];
sum += s[i];
}
Upvotes: 3