Results 1 to 2 of 2
  1. #1
    Krauger is offline Member
    Join Date
    Feb 2012
    Location
    Finland
    Posts
    2
    Rep Power
    0

    Default Sorting a 2-dimensional array

    EDIT: As I mentioned in the second post, I found the correct bubblesort method for sorting my array. I updated the code of this post to what I currently have. It seems to work but I don't mind someone taking a look whether I've done everything correctly.

    Hello, first time poster here. I have recently started learning Java and it's been pretty interesting so far. I just tend to get stuck on (usually) small problems, which is getting pretty time consuming. I have been googling for tips, but figured it might be a good idea to start posting on some dedicated forum.

    Anyway, to the problem: I have a program that asks the user how many rows and columns he wants to print. Then the program prints the corresponding matrix and counts the average of all elements. After that it counts the average of each row and then the average of rows (basically the same number as the previous average). Then the same operation is done for columns.

    I think I have gotten it correct this far, but the last step is to sort each row in an ascending order. I'm not quite sure how I should approach this. I was thinking about adding every row into a normal array and then sort it. But it feels sort of complicated, so I was thinking if this is the correct way...

    Here's the code, feel free to check if the previous tasks are implemented correctly, I'm not 100% sure about them:

    Java Code:
    import java.util.Arrays;
    import java.util.Random;
    import java.util.Scanner;
    
    public class Matrix {
    	
    	private static final Scanner reader = new Scanner(System.in);
    	private static Random rand = new Random();
    	public static final int MAX = 20;
    	public static final int MIN = -20;
    	
    	public static void main(String[] args) {
    		
    		System.out.print("Number of rows: ");
    		int row = reader.nextInt();
    		System.out.print("Number of columns: ");
    		int col = reader.nextInt();
    		
    		// Create a matrix and print it.
    		int[][] matrix = createMatrix(row, col);
    		System.out.println("Formed matrix: ");
    		for (int i = 0; i < matrix.length; i++) {
    			for (int j = 0; j < matrix[i].length; j++)
    				System.out.print(matrix[i][j] + "\t");
    			System.out.println();
    		}
    		
    		// Count averages.
    		double normalAverage = matrixAverage(matrix, row, col);
    		double rowAverage = rowAverage(matrix, row, col);
    		double colAverage = colAverage(matrix, row, col);
    		
    		// Print averages.
    		System.out.printf("----------------------------------------------\n");
    		System.out.printf("Average of every element: %.2f\n", normalAverage);
    		System.out.printf("Average of row averages: %.2f\n", rowAverage);
    		System.out.printf("Average of column averages: %.2f\n", colAverage);
    		System.out.printf("----------------------------------------------\n");
    		
    		// Sort rows in an ascending order.
    		System.out.println("Rows in an ascending order: ");
    		sortMatrix(matrix);
    		
    	}
    	public static int[][] createMatrix(int row, int col) {
    		int[][] matrix = new int[row][col];
    		for (int i = 0; i < matrix.length; i++) {
    			for (int j = 0; j < matrix[i].length; j++)
    			matrix[i][j] = MIN + rand.nextInt(MAX + 1 - MIN);
    		}
    		return matrix;		
    	}
    	public static double matrixAverage(int[][] matrix, int row, int col) {
    		double sum = 0.0;
    		double average = 0.0;
    		for (int i = 0; i < matrix.length; i++) {
    			for (int j = 0; j < matrix[row-1].length; j++)
    				sum += matrix[i][j];
    		}
    		average = sum / (row * col);
    		return average;
    	}
    	public static double rowAverage(int[][] matrix, int row, int col) {
    		double sum = 0.0;
    		double rowAverage = 0.0;
    		double average = 0.0;
    		for (int i = 0; i < matrix.length; i++) {
    			sum = 0.0;
    			for (int j = 0; j < matrix[row-1].length; j++) {
    				sum += matrix[i][j];
    			}
    			rowAverage += sum / col;
    	}
    		average = rowAverage / row;
    		return average;
    }
    	public static double colAverage(int[][] matrix, int row, int col) {
    		double sum = 0.0;
    		double colAverage = 0.0;
    		double average = 0.0;
    		for (int j = 0; j < matrix[row-1].length; j++) {
    			sum = 0.0;
    			for (int i = 0; i < matrix.length; i++) {
    				sum += matrix[i][j];
    			}
    			colAverage += sum / row;
    		}
    		average = colAverage / col;
    		return average;
    	}
    	public static void swap(int[][] array, int row, int col, int colTwo) {
    		int temp = array[row][col];
    		array[row][col] = array[row][colTwo];
    		array[row][colTwo] = temp;
    	}
    	public static void sortMatrix(int[][] array) {
    		for (int row = 0; row < array.length; row++) {
    			for (int col = 0; col < array[0].length; col++) {
    				for (int colTwo = col + 1; colTwo < array[0].length; colTwo++) {
    					if (array[row][colTwo] < array[row][col])
    						swap(array, row, col, colTwo);
    				}
    			}
    		}
    	for (int row = 0; row < array.length; row++) {
    		for (int col = 0; col < array[0].length; col++) {
    			System.out.print(array[row][col] + "\t");
    		}
    		System.out.println();
    	}
    }
    }
    Last edited by Krauger; 02-27-2012 at 04:24 PM.

  2. #2
    Krauger is offline Member
    Join Date
    Feb 2012
    Location
    Finland
    Posts
    2
    Rep Power
    0

    Default Re: Sorting a 2-dimensional array

    Alright... Spent some more time googling and it seems like normal sorting methods can be used for 2-dimensional arrays with some modifications.

    Java Code:
    public static void sortMatrix(int[][] array) {
    		for (int row = 0; row < array.length; row++) {
    			for (int col = 0; col < array[0].length; col++) {
    				for (int colTwo = col + 1; colTwo < array[0].length; colTwo++) {
    					if(array[row][colTwo] > array[row][col])
    					{
    						int temp = array[row][col];
    						array[row][col] = array[row][colTwo];
    						array[row][colTwo] = temp;
    					}
    				}
    			}
    		}
    		for (int row = 0; row < array.length; row++) {
    			for (int col = 0; col < array[0].length; col++) {
    				System.out.print(array[row][col] + "\t");
    			}
    			System.out.println();
    		}
    	}
    This seems to do the trick, in the wrong order (descending) but I'll try to fix it now.

Similar Threads

  1. two dimensional array
    By shahnawazzzzzz in forum Java Software
    Replies: 2
    Last Post: 10-05-2011, 04:47 AM
  2. 2 dimensional array
    By sehudson in forum New To Java
    Replies: 5
    Last Post: 02-20-2011, 11:56 PM
  3. 2 dimensional array help!
    By ber1023 in forum New To Java
    Replies: 9
    Last Post: 01-02-2011, 12:29 AM
  4. Sorting Two Dimensional arrays
    By anfielder in forum New To Java
    Replies: 5
    Last Post: 12-14-2010, 07:12 AM
  5. Help with java sorting two dimensional array
    By Joycey in forum New To Java
    Replies: 2
    Last Post: 03-27-2010, 02:36 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
  •