Array Problem
Content Author: S. Vuong I need to write a method that takes a two-dimensional integer array as an argument an returns a two-dimensional integer array where the elements of each column are sorted into ascending order. The method is not to affect the two-dimensional argument array being passed as an actual argument. The method should be able to cope with any size For example: Argument Array 56 6 3 0 4 -1 3 9 -10 0 12 8 3 7 5 13 Answer Array -10 -1 3 0 3 0 3 8 4 6 5 9 56 7 12 13I would probably suggest using a for loop inside a while loop. I have written a rough psuedo code below but I haven't checked it yet. There will be errors in it. IMPORT: row, coloumn, arrayName
create a new integer variable called smallest and initialise
it to arrayName[1][1]
create 2 new integer variables, nextRow and nextCol and initialise to zero create a new boolean variable called false and set to false WHILE finish is false DO FOR i = 0; i = row; i++ FOR j=0; j = column; j++ IF arrayName[i][j] < smallest THEN smallest = arrayName[i][j] //this will set the variable smallest to the //smallest number ENDIF arrayName[nextRow][nextCol] = smallest //this will set the smallest number //to the first element ENDFOR ENDFOR IF nextCol NOT EQUAL column AND nextRow NOT EQUAL row THEN INCREMENT nextCol BY 1 ELSE IF nextCol EQUALS column and nextRow NOT EQUALS row THEN nextCol = 0 INCREMENT nextRow BY 1 ELSE nextCol EQUALS column and nextRow EQUALS row THEN set finish to true ENDIF ENDWHILEPS One errors in this psuedo code I realised.... making sure that you don't initialise the entire array to the smallest number... you could probably solve this by copying the initial array into a new one and as you use the smallest number, replace it with an extremely large number like a million or something like that so you can't use it again. Good Luck
Do you have a Java Problem?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|