/*
- 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);
}
}
Maximum and Minimum Value in Arrays
Page 1 of 15 Replies - 3360 Views - Last Post: 11 November 2009 - 01:50 PM
#1
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.
Replies To: Maximum and Minimum Value in Arrays
#2
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
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:
Thats it. now max hods the biggest value.
same for the minSale, just check if sales[i] is smaller.
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
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.
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
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.
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
Re: Maximum and Minimum Value in Arrays
Posted 11 November 2009 - 12:51 PM
THANKS! Works perfectly now! I appreciate the help!
#6
Re: Maximum and Minimum Value in Arrays
Posted 11 November 2009 - 01:50 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|