# For loops and Arrays

Page 1 of 1

## 2 Replies - 553 Views - Last Post: 03 December 2009 - 03:44 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=143175&amp;s=51c4128b16a34c7f1a55fb809eef881c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tricket_7

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

# For loops and Arrays

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

• g++ jameson.cpp -o beverage

Reputation: 3145
• Posts: 19,185
• 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

### #3 tricket_7

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

## Re: For loops and Arrays

Posted 03 December 2009 - 03:44 PM

KYA, 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]);
}
}
}
}

```