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
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