Results 1 to 6 of 6
  1. #1
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Java array matrix problem.. please help!

    I am working on an assignment that compares two numbers from adjacent columns and swaps them if the one on the right is smaller than the one on the left. Basically, it's two columns being compared(i and j) and i is the smallest. At first, i is the first column and j is the second column. Then, i is the second column and j is the third column. Then i is the third column and j is the fourth column. So far, I have typed up this code(I think it's too long!):

    Java Code:
    public class MatrixProgram
    {
    	public static void main(String args)
    	{
    		int[][] matrix = new matrix [4][4];
    
    		matrix[1][1] = 1;
    		matrix[1][2] = 2;
    		matrix[1][3] = 1;
    		matrix[1][4] = 4;
    
    		matrix[2][1] = -4;
    		matrix[2][2] = 1;
    		matrix[2][3] = -2;
    		matrix[2][4] = 2;
    
    		matrix[3][1] = 0;
    		matrix[3][2] = -3;
    		matrix[3][3] = 1;
    		matrix[3][4] = -9;
    
    		matrix[4][1] = 7;
    		matrix[4][2] = 5;
    		matrix[4][3] = 0;
    		matrix[4][4] = 2;
    
    		swapMatrix();
    		swapMatrix1();
    		swapMatrix2();
    		swapMatrix3();
    		swapMatrix4();
    		swapMatrix5();
    		swapMatrix6();
    		swapMatrix7();
    		swapMatrix8();
    		swapMatrix9();
    		swapMatrix10();
    		swapMatrix11();
    	}
    
    	public void swapMatrix()
    	{
    		if(matrix[1][1] > matrix[1][2])
    		{
    			int[][] temp = matrix[1][1];
    			matrix[1][1] = matrix[1][2];
    			matrix[1][2] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.println(matrix[1][1]);
    		}
    
    	}
    
    	public void swapMatrix1()
    	{
    		if(matrix[2][1] > matrix[2][2])
    		{
    			int[][] temp = matrix[2][1];
    			matrix[2][1] = matrix[2][2];
    			matrix[2][2] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.println(matrix[2][1]);
    		}
    	}
    
    	public void swapMatrix2()
    	{
    		if(matrix[3][1] > matrix[3][2])
    		{
    			int[][] temp = matrix[3][1];
    			matrix[3][1] = matrix[3][2];
    			matrix[3][2] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.printnln(matrix[3][1]);
    		}
    	}
    
    	public void swapMatrix3()
    	{
    		if(matrix[4][1] > matrix[4][2])
    		{
    			int[][] temp = matrix[4][1];
    			matrix[4][1] = matrix[4][2];
    			matrix[4][2] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.println(matrix[4][1]);
    		}
    	}
    
    	public void swapMatrix4()
    	{
    		if(matrix[1][2] > matrix[1][3])
    		{
    			int[][] temp = matrix[1][2];
    			matrix[1][2] = matrix[1][3];
    			matrix[1][3] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.println(matrix[1][2]);
    		}
    	}
    
    	public void swapMatrix5()
    	{
    		if(matrix[2][2] > matrix[2][3])
    		{
    			int[][] temp = matrix[2][2];
    			matrix[2][2] = matrix[2][3];
    			matrix[2][3] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.println(matrix[2][2]);
    		}
    	}
    
    	public void swapMatrix6()
    	{
    		if(matrix[3][2] > matrix[3][3])
    		{
    			int[][] temp = matrix[3][2];
    			matrix[3][2] = matrix[3][3];
    			matrix[3][3] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.println(matrix[3][2]);
    		}
    	}
    
    	public void swapMatrix7()
    	{
    		if(matrix[4][2] > matrix[4][3])
    		{
    			int[][] temp = matrix[4][2];
    			matrix[4][2] = matrix[4][3];
    			matrix[4][3] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.println(matrix[4][2]);
    		}
    	}
    
    	public void swapMatrix8()
    	{
    		if(matrix[1][3] > matrix[1][4])
    		{
    			int[][] temp = matrix[1][3];
    			matrix[1][3] = matrix[1][4];
    			matrix[1][4] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.println(matrix[1][3]);
    		}
    	}
    
    	public void swapMatrix9()
    	{
    		if(matrix[2][3] > matrix[2][4])
    		{
    			int[][] temp = matrix[2][3];
    			matrix[2][3] = matrix[2][4];
    			matrix[2][4] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.println(matrix[2][3]);
    		}
    	}
    
    	public void swapMatrix10()
    	{
    		if(matrix[3][3] > matrix[3][4])
    		{
    			int[][] temp = matrix[3][3];
    			matrix[3][3] = matrix[3][4];
    			matrix[3][4] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.println(matrix[3][3]);
    		}
    	}
    
    	public void swapMatrix11()
    	{
    		if(matrix[4][3] > matrix[4][4])
    		{
    			int[][] temp = matrix[4][3];
    			matrix[4][3] = matrix[4][4];
    			matrix[4][4] = temp;
    
    			System.out.println(temp);
    		}
    
    		else
    		{
    			System.out.println(matrix[4][3]);
    		}
    	}
    }
    When I compile it, I get 97! errors saying that it can't find the variable "matrix"
    Is there a way to fix the problem and is there any other, shorter way to accomplish this task without all these lines of code? Thanks in advance!

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Asvin View Post
    Is there a way to fix the problem and is there any other, shorter way to accomplish this task without all these lines of code? Thanks in advance!

    there are conceptual errors in your code. but lets begin with the simple one: your matrix declaration is wrong. so read this tutorial and correct the matrix declaration. quite a few of errors are related to your invalid assignments like int[][] temp = matrix[1][1]; a lot of other errors are caused because you try to call non-static methods from a static method. and some other are simple syntax errors like System.out.printnln. so try to correct these errors.

  3. #3
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Oh wow.. I feel so stupid.. thanks for the link... I will post the updated code later.. thanks for replying!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Arrrgh!

    Duplicate code. Kill it! Kill it!

    How about having only one method and passing parameters to it?

  5. #5
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    I replaced my old code with some new ones.. Here is what I have so far:

    Java Code:
    import java.util.Arrays;
    
    
    public class MatrixProgram
    {
    	public static void main(String[] args)
    	{
    		int[][] arrayData = {{ 1,   2,  1,  4},
    					    {-4,   1, -2,  2},
    					    { 0,  -3,  1, -9},
    					    { 7,   5,  0,  2}};
    	}
    	
    	public static int arrayManipulator(int[][] arrayData, int[][] arrayData)
    	{
    				
    }
    I don't know what to put in the "arrayManipulator" method! How would I go about sending parameters from "arrayData" and how would I compare values?

  6. #6
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Now I am up to here:

    Java Code:
    import java.util.Arrays;
    
    
    public class MatrixProgram
    {
    	public static void main(String[] args)
    	{
    		int[][] arrayData = {{ 1,   2,  1,  4},
    						     {-4,   1, -2,  2},
    						     { 0,  -3,  1, -9},
    						     { 7,   5,  0,  2}};
    
    		while(i < j)
    		{
    			swap(arrayData, i, j)
    		}
    	}
    
    	public static void swap(int[][] arrayData, int i, int j)
    	{
    		int temp = arrayData[i];
    		arrayData[i] = arrayData[j];
    		arrayData[j] = temp;
    	}
    PLEASE HELP!!

Similar Threads

  1. Sparse matrix in java
    By Zerpol in forum New To Java
    Replies: 2
    Last Post: 12-15-2010, 12:35 PM
  2. Java Webservice problem (Array)
    By Gadge in forum New To Java
    Replies: 0
    Last Post: 01-10-2010, 04:55 PM
  3. Java Array object problem
    By kozik in forum New To Java
    Replies: 13
    Last Post: 04-20-2009, 10:37 PM
  4. Java array problem
    By cassysumandak in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 03:21 AM
  5. Linearizing a 2D Array/Matrix
    By dalangley in forum New To Java
    Replies: 13
    Last Post: 03-03-2009, 12:26 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
  •