For loops and Arrays

determining and displaying an name based off the salary

Page 1 of 1

2 Replies - 433 Views - Last Post: 03 December 2009 - 03:44 PM Rate Topic: -----

#1 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

For loops and Arrays

Post icon  Posted 03 December 2009 - 02:26 PM

I am stuck. I am supposed to use a loop to determine and display the name of the person with the highest salary. I have gotten the highest salary, but don't know how to use a loop to reference the persons name.
import java.text.*;
public class Ch2_1
{
	public static void main (String [] args)
	{
		DecimalFormat currency = new DecimalFormat("$#,##0.00");
		
		
		double [] empSalaries = {45000.00, 27500.00, 61500.00, 32200.00, 51800.00};
		String [] empNames = {"John Smith", "Mary King", "Chris Kennedy", "Angela Jones", "Mark Smith"};
		//a
		System.out.println(empNames[3] + " has a salary of " + currency.format(empSalaries[3]));
		
		//b
		for(int i = 0; i < empNames.length; i++)
		{
		System.out.println(empNames[i]);	//b.
		}
		
		//c
		double total = 0;
		double average;
		
		for(int i = 0; i < empSalaries.length; i++)
			total += empSalaries[i];
			average = total / empSalaries.length;
		System.out.println("The average salary is " + currency.format(average));
		
		//d
		double highest = empSalaries[0];
		for(int i = 0; i < empSalaries.length; i++)
		{
			if(empSalaries[i] > highest)
			highest = empSalaries[i];
		}
		System.out.println("The highest salary is " + currency.format(highest));
		
		//e
		
	
		
   		System.out.println("The employee with the highest salary is " + empNames[i]);		
	}
	}
}



Do I use row and col?

Is This A Good Question/Topic? 0
  • +

Replies To: For loops and Arrays

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3102
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: For loops and Arrays

Posted 03 December 2009 - 02:35 PM

There's several options all of which are dependent of how much you want to rewrite/rework your solution.

1. Sort the data, then print out the lowest/highest index depending on whether you sorted ascending/descending.

2. Initialize an integer that will hold the index of the highest salary. Throw its assignment inside the if statement in the condition:

 double highest = empSalaries[0];
 int index = 0;
		for(int i = 0; i < empSalaries.length; i++)
		{
			if(empSalaries[i] > highest){
				  highest = empSalaries[i];
				  index = i;
			}
		}
//use empName[index] later



3. Use a class/structure instead of parallel arrays
Was This Post Helpful? 0
  • +
  • -

#3 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: For loops and Arrays

Posted 03 December 2009 - 03:44 PM

View PostKYA, on 3 Dec, 2009 - 01:35 PM, said:

There's several options all of which are dependent of how much you want to rewrite/rework your solution.

1. Sort the data, then print out the lowest/highest index depending on whether you sorted ascending/descending.

2. Initialize an integer that will hold the index of the highest salary. Throw its assignment inside the if statement in the condition:

 double highest = empSalaries[0];
 int index = 0;
		for(int i = 0; i < empSalaries.length; i++)
		{
			if(empSalaries[i] > highest){
				  highest = empSalaries[i];
				  index = i;
			}
		}
//use empName[index] later



3. Use a class/structure instead of parallel arrays

I changed my code as follows , but still doesnt print anything out
import java.text.*;
public class Ch2_1
{
	public static void main (String [] args)
	{
		DecimalFormat currency = new DecimalFormat("$#,##0.00");
		
		
		double [] empSalaries = {45000.00, 27500.00, 61500.00, 32200.00, 51800.00};
		String [] empNames = {"John Smith", "Mary King", "Chris Kennedy", "Angela Jones", "Mark Smith"};
		//a
		System.out.println(empNames[3] + " has a salary of " + currency.format(empSalaries[3]));
		
		//b
		for(int i = 0; i < empNames.length; i++)
		{
		System.out.println(empNames[i]);	//b.
		}
		
		//c
		double total = 0;
		double average;
		
		for(int i = 0; i < empSalaries.length; i++)
			total += empSalaries[i];
			average = total / empSalaries.length;
		System.out.println("The average salary is " + currency.format(average));
		
		//d
		double highest = empSalaries[0];
		for(int i = 0; i < empSalaries.length; i++)
		{
			if(empSalaries[i] > highest)
			highest = empSalaries[i];
		}
		System.out.println("The highest salary is " + currency.format(highest));
		
		//e
		
 		int index = 0;
		for(int i = 0; i < empSalaries.length; i++)
		{
			if(empSalaries[i] > highest){
				  highest = empSalaries[i];
				  index = i;
		  
			System.out.println("The employee with the highest salary is " + empNames[index]);	
	}
	}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1