Array question

B

bellsb

i'm trying to write a program that computes the totals from a
two-dimensional array then sorts by the largest total. however, I'm
loosing the original record when i do the sort, becuase i can't figure
out how to pass the index[key] when the swap takes place.

public class WeeklyHours {
public static void main(String[] args) {

//declare and define the array
int[][] employeeHours = {
{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}};

//display hours total hours worked by employee
int[] totals = sumHours(employeeHours);

int[] index = sort(totals);
//for (int i = 0; i <= index.length; i++)
// System.out.println(index);

printArray(index, totals);

}

//define method which totals each row
public static int[] sumHours(int[][] array) {
//declare array and set values to 0
int[] total = new int[array.length];

//for loop used to tally array values
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array.length; j++)
total += array[j];
}

return total;

}

//define method to sort array in desending order
public static int[] sort(int[] array) {

//setup array to keep track of employees
int[] index = new int[array.length];
for (int i = 0; i < array.length; i++)
index = i;

//start for loop from bottom of array
for (int i = array.length - 1; i >= 1; i--) {
int currentMin = array[0];
int currentMinIndex = 0;

for (int j = 1; j <= i; j++) {
if (currentMin > array[j]) {
currentMin = array[j];
currentMinIndex = j;
}
}

if (currentMinIndex != i) {
array[currentMinIndex] = array;
array = currentMin;

}
}
return index;
}

public static void printArray(int[] array1, int[] array2) {
for (int i = 0, j = 0; i < array1.length; i++, j++)
System.out.println("Employee" + array1[j] + ": " + array2 + "
hours");
}

}



I'd like this to output in this format:

employee7: 41
employee6: 37
employee5: 34
etc.
etc.

Any advice would be great, I think i'm close, just can't get the last
piece.

-sb
 
B

Bjorn Abelli

i'm trying to write a program that computes the totals from a
two-dimensional array then sorts by the largest total. however,
I'm loosing the original record when i do the sort, becuase
i can't figure out how to pass the index[key] when the swap
takes place.

There are several solution to this, so I'll give you the two that comes up
on the top of my head.

1 Swap the elements of the array index at
the same time as you make the swap in totals.

...
for (int i = array.length - 1; i >= 1; i--)
{
int currentMin = array[0];
int currentMinIndex = 0;

int currentIndex = 0; // <-

for (int j = 1; j <= i; j++)
{
if (currentMin > array[j])
{
currentMin = array[j];
currentMinIndex = j;

currentIndex = index[j]; // <-
}
}

if (currentMinIndex != i)
{
array[currentMinIndex] = array;
array = currentMin;

index[currentIndex] = index; // <-
index = currentIndex; // <-
}
}
...


2 [My preferred solution]
Encapsulate the employees working hours as properties
of an Employee class. Create an array (or another
collection) of Employees which you sort by the
total hours they've made...

// Bjorn A





Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top