# Suggestions on homework

• 11-04-2013, 12:23 AM
codyjava
Suggestions on homework
Hey all so I've made a matrix that gets all the values on each row summed. Then those sums get sorted in descending order. All of this works but I want to somehow keep the employee with the total number of hours they worked. So when it gets sorted they need to stay together somehow. I really have no ideas how to do this does anyone have any suggestions.

Thanks

So something like this for an output:
Code:

```Employee0 41 hours Employee1 37 hours Employee2 34 hours Employee3 32 hours Employee4 31 hours Employee5 28 hours Employee6 28 hours Employee7 20 hours```
Code so far:
Code:

```public class Hw11 {         //pass a string matrix and sum each row inside the matrix then return it in an array         public static int[] sumMatrix(String[][] matrix){                                 int array[] = new int[matrix.length-1];                 int index = 0;                                 for(int i=1;i<matrix.length;i++){                         int sum = 0;                         for(int j=1;j<matrix[i].length;j++){                                                                 String temp = matrix[i][j];                                 sum += Integer.parseInt(temp);                                                        }                         array[index++] = sum;                 }                 return array;         }                         public static void sortArray(int[] array){                                 for(int i=0;i<array.length-1;i++){                         int currentMax = array[i];                         int currentMaxIndex = i;                                                 for(int j = i+1;j<array.length;j++){                                 if(currentMax < array[j]){                                         currentMax = array[j];                                         currentMaxIndex = j;                                                                        }                         }                                                 if(currentMaxIndex != i){                                 array[currentMaxIndex] = array[i];                                 array[i] = currentMax;                         }                 }                         }                         public static void main(String [] args){                                 String[][] matrix = {                                 {" ","SU","M","T","W","R","F","SA"},                                 {"Employee0","2","4","3","4","5","8","8"},                                 {"Employee1","7","3","4","3","3","4","4"},                                 {"Employee2","3","3","4","3","3","2","2"},                                 {"Employee3","9","3","4","7","3","4","1"},                                 {"Employee4","3","5","4","3","6","3","8"},                                 {"Employee5","3","4","4","6","3","4","4"},                                 {"Employee6","3","7","4","8","3","8","4"},                                 {"Employee7","6","3","5","9","2","7","9"}                 };                                                 int array[] = sumMatrix(matrix);                                 sortArray(array);                                 for(int i=0;i<array.length;i++){                         System.out.println(array[i]+" hours");                 }                                 }         }```
Output:
Code:

```41 hours 37 hours 34 hours 32 hours 31 hours 28 hours 28 hours 20 hours```
• 11-04-2013, 01:42 AM
Norm
Re: Suggestions on homework
Quote:

they need to stay together
Make a class that holds all the data for an employee. Put the objects of the class in an ArrayList and sort its contents.
If all the data is together in a class, there won't be a problem.
• 11-04-2013, 01:54 AM
codyjava
Re: Suggestions on homework
Yeah that sounds like it would be easy and work great I think the point of my assignment is to only use arrays and 2D arrays though. Is there some way I could store both the employee and amount of hours in one index slot to be sorted. Or make a 2D array and sort it so the employee and hours are tied together.
• 11-04-2013, 02:14 AM
Norm
Re: Suggestions on homework
Two dim arrays are really a one dim array of one dim arrays. You can assign the second dim to a variable and to a slot in the 2 dim array just like with one dim arrays. In other words one row of a 2 dim array can be assigned to a one dim array and a one dim array can be assigned to be a row in a 2 dim array.
• 11-04-2013, 03:35 AM
codyjava
Re: Suggestions on homework
Alright thanks for the advice I'll mess around with this and see if I can get it to work for me.
• 11-05-2013, 12:23 AM
codyjava
Re: Suggestions on homework
Hey Norm I was doing some thinking is there any way I could use binary search or linear search to match the Employee with the amount of hours?

Thanks
• 11-05-2013, 01:28 AM
Norm
Re: Suggestions on homework
Can you show where the data that you want to search is stored now? The best storage technique would be to have the name and hours in the same class object or on the same row in a 2 dim array.

If the rows are sorted, then either type of search should work.
• 11-05-2013, 01:53 AM
codyjava
Re: Suggestions on homework
Hi Norm the rows are sorted in descending order.

Originally the values are in this 2D array:
Code:

```                String[][] matrix = {                                 {" ","SU","M","T","W","R","F","SA"},                                 {"Employee0","2","4","3","4","5","8","8"},                                 {"Employee1","7","3","4","3","3","4","4"},                                 {"Employee2","3","3","4","3","3","2","2"},                                 {"Employee3","9","3","4","7","3","4","1"},                                 {"Employee4","3","5","4","3","6","3","8"},                                 {"Employee5","3","4","4","6","3","4","4"},                                 {"Employee6","3","7","4","8","3","8","4"},                                 {"Employee7","6","3","5","9","2","7","9"}                 };```
Then I pull out the numbers for each row and sum them together. So the sums are stored in int array[]:
Code:

```int array[] = sumMatrix(matrix);         public static int[] sumMatrix(String[][] matrix){                                 int array[] = new int[matrix.length-1];                 int index = 0;                                 for(int i=1;i<matrix.length;i++){                         int sum = 0;                         for(int j=1;j<matrix[i].length;j++){                                                                 String temp = matrix[i][j];                                 sum += Integer.parseInt(temp);                                                        }                         array[index++] = sum;                 }                 return array;         }```
Then I sort the array. So it should still be in int array[] just in descending order:
Code:

```        public static void sortArray(int[] array){                                 for(int i=0;i<array.length-1;i++){                         int currentMax = array[i];                         int currentMaxIndex = i;                                                 for(int j = i+1;j<array.length;j++){                                 if(currentMax < array[j]){                                         currentMax = array[j];                                         currentMaxIndex = j;                                                                        }                         }                                                 if(currentMaxIndex != i){                                 array[currentMaxIndex] = array[i];                                 array[i] = currentMax;                         }                 }                         }```
So how would I but the employees in a 2D array and then sort them? Sorry I'm kinda lost
• 11-05-2013, 02:11 AM
Norm
Re: Suggestions on homework
Quote:

how would I but the employees in a 2D array
It looks like they are now in a 2d array: matrix

Quote:

the rows are sorted in descending order.
Why do you need to sort them if they are already sorted?

Quote:

So the sums are stored in int array[]:
A better solution would be to add a column to matrix to hold the sums.

If you MUST work with parallel arrays, then any time you swap two rows in one array you must swap the same two rows in the other array(s) to keep the rows in synch.
• 11-05-2013, 03:18 AM
codyjava
Re: Suggestions on homework
Alright thank you I'll will try this.