Results 1 to 10 of 10
  1. #1
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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
    Code so far:
    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.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:
    Java Code:
    41 hours
    37 hours
    34 hours
    32 hours
    31 hours
    28 hours
    28 hours
    20 hours

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Suggestions on homework

    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.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

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

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

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

  5. #5
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

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

  6. #6
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

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

  8. #8
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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"}
    		};
    Then I pull out the numbers for each row and sum them together. So the sums are stored in int array[]:
    Java 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:
    Java 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

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Suggestions on homework

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

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


    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.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Suggestions on homework

    Alright thank you I'll will try this.

Similar Threads

  1. Any Suggestions???
    By Coulterce in forum Forum Lobby
    Replies: 1
    Last Post: 06-29-2012, 06:38 PM
  2. [suggestions] minesweeper
    By temyong in forum New To Java
    Replies: 7
    Last Post: 12-30-2010, 03:10 AM
  3. looks good looking for suggestions..
    By search4survival in forum New To Java
    Replies: 10
    Last Post: 11-30-2010, 12:59 PM
  4. Looking for suggestions
    By tonino in forum New To Java
    Replies: 5
    Last Post: 09-26-2010, 07:17 PM
  5. any suggestions?
    By PureAwesomeness in forum New To Java
    Replies: 4
    Last Post: 01-19-2009, 07:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •