5 Replies - 6523 Views - Last Post: 11 November 2009 - 01:50 PM Rate Topic: -----

#1 EGutierrez91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 10-September 09

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.

/*
- 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);
	
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Maximum and Minimum Value in Arrays

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

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
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.
Was This Post Helpful? 1
  • +
  • -

#3 EGutierrez91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 10-September 09

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.
Was This Post Helpful? 0
  • +
  • -

#4 WaHooCrazy7  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 19
  • Joined: 09-November 09

Re: Maximum and Minimum Value in Arrays

Posted 10 November 2009 - 08:53 PM

View PostEGutierrez91, 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.


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.
Was This Post Helpful? 1
  • +
  • -

#5 EGutierrez91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 10-September 09

Re: Maximum and Minimum Value in Arrays

Posted 11 November 2009 - 12:51 PM

THANKS! Works perfectly now! I appreciate the help!
Was This Post Helpful? 0
  • +
  • -

#6 WaHooCrazy7  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 19
  • Joined: 09-November 09

Re: Maximum and Minimum Value in Arrays

Posted 11 November 2009 - 01:50 PM

View PostEGutierrez91, on 11 Nov, 2009 - 11:51 AM, said:

THANKS! Works perfectly now! I appreciate the help!


not a problem! glad I could help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1