5 Replies - 308 Views - Last Post: 19 October 2012 - 03:10 PM Rate Topic: -----

#1 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 10-August 12

Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 12:49 PM

I used an array to find the max and min without much trouble but i need to find which index of the max is and same for the min (hopefully i described that right) here's my code:

public class Sales
{
    public static void main(String[] args)
    {
	 
		final int SALESPEOPLE = 5;
		int[] sales = new int[SALESPEOPLE];
		int sum;
		int max;
		int min;
		int bestSeller = 0;
		int worstSeller = 0;
	
		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;
		min = sales[0];
		max = sales[0];
		
		
		for (int i=0; i<sales.length; i++)
		    {
			 
			System.out.println("     " + i + "         " + sales[i]);
			sum += sales[i];
	
			
			if (max < sales[i])
				{
				max = sales[i];
				}
				
			if (min > sales[i])
				{
				min = sales[i];
				}
	
			
				
			
				
				 
		    }
			 

		System.out.println(min);
		System.out.println(max);
		System.out.println("Average sales: " + sum / SALESPEOPLE);
		System.out.println("\nTotal sales: " + sum);
	   
		 }
}




basically i would need to declare to variables (bestSeller, worstSeller) and then print out which seller it is like:

System.out.println("Salesman " + bestSeller + " has the greatest sales at " + max);

Example print: Salesman 4 has the greatest sales at 1000.

try not to just give the answer, and rather just a clue at where to start if possible. thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Finding the max and min with an array and getting the index of each.

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 12:52 PM

Easiest would probably be to just keep to extra variables min/maxIndex and update them when you update max and min.
Was This Post Helpful? 0
  • +
  • -

#3 Dirnol  Icon User is offline

  • New D.I.C Head

Reputation: 15
  • View blog
  • Posts: 31
  • Joined: 15-October 12

Re: Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 12:56 PM

Look at how the value max and min are set. If only there were some way to save the index those values use...
Was This Post Helpful? 0
  • +
  • -

#4 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 10-August 12

Re: Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 02:42 PM

View PostKakerergodt, on 19 October 2012 - 12:52 PM, said:

Easiest would probably be to just keep to extra variables min/maxIndex and update them when you update max and min.


this is what i was implying by having the variable bestSeller and worstSeller


View PostDirnol, on 19 October 2012 - 12:56 PM, said:

Look at how the value max and min are set. If only there were some way to save the index those values use...


ya that is basically what i can't figure out with what i have been taught already...i thought you could use something like

bestSeller = max[index]

but that's trying to access an array that isn't there...basically just me trying to make up my own java syntax lol
Was This Post Helpful? 0
  • +
  • -

#5 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 02:50 PM

So what you want is to store the index of where the worst and best sellers are located in the array. Well, we need to store the index, but we also need to determine what the maximum value is then.

In pseudocode, this looks like:

// Set the initial values for the max and minumum values
min = sales[0];
max = Parse(char at userInput(0)); //Same here
indexMin = 0;
indexMax = 0;

// Traverse the array
for i = 1 to length sales
	currentNumber = sales(i); //Acces the entry i of the array

	//If the current number is higher than max, set max to that. Also set the index
	if(currentNumber > max)
		max = currentNumber; //Sets the maximum value
		indexMax = i; //Sets the index of the maximum number to current index
	endif
	
	//If the current number is higher than max, set min to that. Also set the index
	if(currentNumber < min)
		min = currentNumber; //Sets the minimum value
		indexMin = i; //Sets the index of the minimum value to current index
	endif
endfor

//Do whatever you want with your variables



Now it's up to you to translate this into Java ;)
Was This Post Helpful? 0
  • +
  • -

#6 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 10-August 12

Re: Finding the max and min with an array and getting the index of each.

Posted 19 October 2012 - 03:10 PM

View Postkarabasf, on 19 October 2012 - 02:50 PM, said:

So what you want is to store the index of where the worst and best sellers are located in the array. Well, we need to store the index, but we also need to determine what the maximum value is then.

In pseudocode, this looks like:

// Set the initial values for the max and minumum values
min = sales[0];
max = Parse(char at userInput(0)); //Same here
indexMin = 0;
indexMax = 0;

// Traverse the array
for i = 1 to length sales
	currentNumber = sales(i); //Acces the entry i of the array

	//If the current number is higher than max, set max to that. Also set the index
	if(currentNumber > max)
		max = currentNumber; //Sets the maximum value
		indexMax = i; //Sets the index of the maximum number to current index
	endif
	
	//If the current number is higher than max, set min to that. Also set the index
	if(currentNumber < min)
		min = currentNumber; //Sets the minimum value
		indexMin = i; //Sets the index of the minimum value to current index
	endif
endfor

//Do whatever you want with your variables



Now it's up to you to translate this into Java ;)



Thanks so much i got it now and i believe it is working properly...

public class Sales
{
    public static void main(String[] args)
    {
	 
		final int SALESPEOPLE = 5;
		int[] sales = new int[SALESPEOPLE];
		int sum;
		int max;
		int min;
		int bestSeller = 0;
		int worstSeller = 0;
		int currentSeller = 0;
	
		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;
		min = sales[0];
		max = sales[0];
		
		
		for (int i=0; i<sales.length; i++)
		    {
			 
			System.out.println("     " + i + "         " + sales[i]);
			sum += sales[i];
			currentSeller = sales[i];
	
			
			if (max < sales[i])
				{
				max = sales[i];
				bestSeller = i;
				}
				
			if (min > sales[i])
				{
				min = sales[i];
				worstSeller = i;
				}
	
			
				
				 
		    }
			 
		System.out.println("The lowest seller is Salesman " + worstSeller + " with sales at " + min);
		System.out.println("The top seller is Salesman " + bestSeller + " with sales at " + max);
		System.out.println("Average sales: " + sum / SALESPEOPLE);
		System.out.println("\nTotal sales: " + sum);
	   
		 }
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1