Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Diagonal of a matrix usind a 2d int array

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Diagonal of a matrix usind a 2d int array

    Hello!

    Implement a method that finds the digonal of a matrix.Use the method as given;

    static int[] diagonal(int[][] a) {..} the method should return the diagonal a in the form of an int array.

    So what I've tried is this

     
    public class test {
     
    	static int[] diagonal (int[][] a) {
    		for(int i = 0; i<a.length; i++) {
    			for(int j = 0; i<a.length; j++) {
     
    				if( i = j) {
    					a =[i][j];
    				}
    			}
    		}
    		return a;
    	}
    public static void main(String[] args){
    	int[][] a = {{1,2,3,4}};
     
    	diagonal(a);
    }
    }
    test.java:8: error: illegal start of expression
    a =[i][j];

    is the error I get when I try to compile.I think I know how to get the diagonal,we need 2 for loops to go through the array and we check the value if they match we found it. The part that is bugging me is I dont know how to transfer that into an int array and return it like I'm susposed to.

    Any tips?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Diagonal of a matrix usind a 2d int array

    a =[i][j];
    That expression looks like it is trying to assign some value to the variable named a. a is declared as a 2 dimensional array.
    What is the value [i][j] supposed to be?
    What is that loop supposed to be doing? I don't see any comments describing its task.

    A valid assignment statement would be something like this
      	a = new int[1][1];

    Can you post the contents for an input matrix
    and the desired output?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Diagonal of a matrix usind a 2d int array

    Okay so I'll try to explain what I want to do exacly. We need to able to find the diagonal of a quadratic matrix (the matrix which has the same number of rows and coloumns).To do that I'm giving a static method int[] diagonal(int[][] a) {..} The method is of type int arrray and the parameter is a 2d int array. What I had in mind.

    What we need to achive is this.Lets assume our matrix looks like this. { 1 2 3
    4 5 6
    3 4 5}

    The goal is that the diagonal of the matrix a is returend as an int array. in this case that would be {1 5 5},

    So here is my though process (a bit revised). We need 2 for loops that will go through the matrix a.Meaning from the first to the last element in the array.Than after moving through the array we need to check if the values are in a diagonal and than we need to save those values(the diagonal) in an int array and return it.I've realised my first attempt was kind of nonsence.

    import java.util.Arrays;
    public class test {
     
     
    	static int[] getDiagonal(int[][] a) {
    		int diagonal[] = new int[a.length];
    		for(int i = 0; i<diagonal.length; i++) {
    			diagonal[i] = a[i][i];
    		}
    		Out.println(Arrays.toString(diagonal));
    		return diagonal;
     
    	}
     
    	public static void main(String[] args) {
    		int[][] rnd_array = {{1,2,4},{6,7,3}};
     
    		getDiagonal(rnd_array);
    	}
    }

    The code compiles fine,but it isnt working properly.It only prints out 1 and 7.What am I missing here.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Diagonal of a matrix usind a 2d int array

    It only prints out 1 and 7
    What did you expect it to print for that array with 2 rows?
    Try using an array with 3 rows.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Diagonal of a matrix usind a 2d int array

    Oh wow,that is embarassing,quite a silly mistake.It works just fine now thank you!

Similar Threads

  1. Replies: 4
    Last Post: February 22nd, 2013, 03:51 PM
  2. Game of Life 2-D matrix random seed boolean array PLEASE
    By Eriosblood in forum Collections and Generics
    Replies: 20
    Last Post: September 3rd, 2012, 06:10 PM
  3. Generating array (or arraylist) matrix from txt file
    By benjalizana in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 14th, 2011, 08:09 PM
  4. java Loop program to place distinct value in a row, column and diagonal
    By javanovoice in forum Loops & Control Statements
    Replies: 3
    Last Post: September 8th, 2011, 08:08 PM