Results 1 to 10 of 10
Thread: Suggestions on homework
 11032013, 11:23 PM #1Member
 Join Date
 Dec 2012
 Posts
 42
 Rep Power
 0
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:
Java 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
Java 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.length1]; 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.length1;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"); } } }
Java Code:41 hours 37 hours 34 hours 32 hours 31 hours 28 hours 28 hours 20 hours
 11042013, 12:42 AM #2
Re: Suggestions on homework
they need to stay together
If all the data is together in a class, there won't be a problem.If you don't understand my response, don't ignore it, ask a question.
 11042013, 12:54 AM #3Member
 Join Date
 Dec 2012
 Posts
 42
 Rep Power
 0
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.
 11042013, 01:14 AM #4
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.
If you don't understand my response, don't ignore it, ask a question.
 11042013, 02:35 AM #5Member
 Join Date
 Dec 2012
 Posts
 42
 Rep Power
 0
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.
 11042013, 11:23 PM #6Member
 Join Date
 Dec 2012
 Posts
 42
 Rep Power
 0
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
 11052013, 12:28 AM #7
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.If you don't understand my response, don't ignore it, ask a question.
 11052013, 12:53 AM #8Member
 Join Date
 Dec 2012
 Posts
 42
 Rep Power
 0
Re: Suggestions on homework
Hi Norm the rows are sorted in descending order.
Originally the values are in this 2D array:
Java 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"} };
Java Code:int array[] = sumMatrix(matrix); public static int[] sumMatrix(String[][] matrix){ int array[] = new int[matrix.length1]; 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; }
Java Code:public static void sortArray(int[] array){ for(int i=0;i<array.length1;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; } } }
 11052013, 01:11 AM #9
Re: Suggestions on homework
how would I but the employees in a 2D array
the rows are sorted in descending order.
So the sums are stored in int array[]:
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.If you don't understand my response, don't ignore it, ask a question.
 11052013, 02:18 AM #10Member
 Join Date
 Dec 2012
 Posts
 42
 Rep Power
 0
Similar Threads

Any Suggestions???
By Coulterce in forum Forum LobbyReplies: 1Last Post: 06292012, 06:38 PM 
[suggestions] minesweeper
By temyong in forum New To JavaReplies: 7Last Post: 12302010, 03:10 AM 
looks good looking for suggestions..
By search4survival in forum New To JavaReplies: 10Last Post: 11302010, 12:59 PM 
Looking for suggestions
By tonino in forum New To JavaReplies: 5Last Post: 09262010, 07:17 PM 
any suggestions?
By PureAwesomeness in forum New To JavaReplies: 4Last Post: 01192009, 07:34 AM
Bookmarks