Thread: Transpose()

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    39
    Rep Power
    6

    Transpose()


    I'm trying to write a program that takes the transpose of a matrix by function call, and I think I've made a couple of syntax errors, but I can't spot them. Can anybody help?

    Code:
    import java.util.Scanner;
    class Transpose
    {
       public static int transpose(int matrix[][])
       {
          int a, b, c, d;
    
    
          
          int transpose[][] = new int[b][a];
    
          for ( c = 0 ; c < a ; c++ )
          {
             for ( d = 0 ; d < n ; d++ )               
                transpose[d][c] = matrix[c][d];         
    
          }
    
          System.out.println("Transpose of entered matrix:-");
    
          for ( c = 0 ; c < n ; c++ )
          {
             for ( d = 0 ; d < a ; d++ )
                   System.out.print(transpose[c][d]+"\t");
    
                System.out.print("\n");
          }
       }
    	
    	public static void main(String args[])
       {
          int a, b, c, d;
    
          Scanner in = new Scanner(System.in);
          System.out.println("Enter the number of rows and columns of matrix");
          a = in.nextInt();
          b = in.nextInt();
    
          int matrix[][] = new int[a][b];
    
          System.out.println("Enter the elements of matrix");
    
          for ( c = 0 ; c < m ; c++ )
             for ( d = 0 ; d < n ; d++ )
                matrix[c][d] = in.nextInt();
    
    	transpose(matrix[][]);
        
        }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    couple of syntax errors,
    Can you copy the full text of the error messages and paste it here?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    39
    Rep Power
    6
    Originally Posted by NormR
    Can you copy the full text of the error messages and paste it here?
    Sure.

    error: variable b might not have been initialized
    int transpose[][]=new int[b][a]
    ^

    error: variable a might not have been initialized
    int transpose[][]=new int[b][a]
    ^

    error: missing return statement

    }
    ^
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    variable b might not have been initialized
    The compiler can't see where the variables: a and b have been assigned a value before they are used.
    What values are they supposed to have and where are those values assigned?

    missing return statement
    The method is define to return a value but the compiler can not find the return statement. What value should the method return? Add a return statement that returns that value or change the method's definition so that it does not need to return a value.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    39
    Rep Power
    6
    Originally Posted by NormR
    The compiler can't see where the variables: a and b have been assigned a value before they are used.
    What values are they supposed to have and where are those values assigned?

    The method is define to return a value but the compiler can not find the return statement. What value should the method return? Add a return statement that returns that value or change the method's definition so that it does not need to return a value.
    Now that I added a 'return 1' to the end of transpose, I'm only getting one error message. It says:
    error: '.class' expected
    transpose(matrix[][]);
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    1. The number of rows and columns are stored in variables "a" and "b". But you're refering to these components using variables "m" and "n".

    2. The following invocation is wrong:
    transpose(matrix[][])

    Remember that the variable here is "matrix" which refers to an object that is a multi-dimensional int array. The variable is not "matrix[][]". The correct invocation would be transpose(matrix)

    3. You have defined a method "transpose" that returns an int. But there is no return statement in the "transpose" method.

    4. Variables "a" and "b" are defined in the scope of the main method. But you are trying to access them in the "transpose" method. This will not work. You have to define the method to accept a and b as arguments and send a and b while invocation.
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Please post the new source code that is causing the error.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    39
    Rep Power
    6
    Originally Posted by noobie1000
    1. The number of rows and columns are stored in variables "a" and "b". But you're refering to these components using variables "m" and "n".

    2. The following invocation is wrong:
    transpose(matrix[][])

    Remember that the variable here is "matrix" which refers to an object that is a multi-dimensional int array. The variable is not "matrix[][]". The correct invocation would be transpose(matrix)

    3. You have defined a method "transpose" that returns an int. But there is no return statement in the "transpose" method.

    4. Variables "a" and "b" are defined in the scope of the main method. But you are trying to access them in the "transpose" method. This will not work. You have to define the method to accept a and b as arguments and send a and b while invocation.
    1) Well, one typo I just made above was to write m for 'a' and 'n' or 'b.' Those were older variable names that I changed.

    2) Since 'a' and 'b' represent the dimensions of the matrix, how should I invoke them? transpose(matrix[a][b])?

    3) I added a return 1 now.

    4) This is where I'm stuck.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Read point #2 and #4
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    39
    Rep Power
    6
    Originally Posted by noobie1000
    Read point #2 and #4
    I've made a few changes based on that. This is what I have now:

    Code:
    import java.util.Scanner;
    class Transpose
    {
       public static int transpose(matrix, int a, int b)
       {
          int a, b, c, d;
    
    
          
          int transpose[][] = new int[b][a];
    
          for ( c = 0 ; c < m ; c++ )
          {
             for ( d = 0 ; d < n ; d++ )               
                transpose[d][c] = matrix[c][d];         
    
          }
    
          System.out.println("Transpose of entered matrix:-");
    
          for ( c = 0 ; c < b ; c++ )
          {
             for ( d = 0 ; d < a ; d++ )
                   System.out.print(transpose[c][d]+"\t");
    
                System.out.print("\n");
          }
    	return 1;
       }
    	
    	public static void main(String args[])
       {
          int a, b, c, d;
    
          Scanner in = new Scanner(System.in);
          System.out.println("Enter the number of rows and columns of matrix");
          a = in.nextInt();
          b = in.nextInt();
    
          int matrix[][] = new int[a][b];
    
          System.out.println("Enter the elements of matrix");
    
          for ( c = 0 ; c < m ; c++ )
             for ( d = 0 ; d < n ; d++ )
                matrix[c][d] = in.nextInt();
    
    	transpose(matrix, c, d);
        
        }
    }
    I'm still getting an "identifier expected" error. When I call it transpose(int matrix, int c, int d), I get even more errors.
    Last edited by Astrodude; July 3rd, 2013 at 10:24 AM.
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    get even more errors.
    Please copy the full text of the error messages and paste it here.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    39
    Rep Power
    6
    Originally Posted by NormR
    Please copy the full text of the error messages and paste it here.
    Transpose.java:6:error:variable a is already defined in method transpose(int, int, int).

    int a, b, c, d
    ^


    Transpose.java:6:error:variable b is already defined in method transpose(int, int, int).

    int a, b, c, d
    ^

    Transpose.java:15:error: array required but int found.
    transpose[d][c]=matrix[c][d]

    Transpose.java:48:error: method transpose in class Transpose cannot be applied to given types.

    transpose(matrix, c, d);
    ^
    required: int, int, int;
    found: int[][], int, int;
    reason: actual argument cannot be converted to int by method invocation
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    variable a is already defined in method transpose(int, int, int).
    a and b are already defined as arguments to the method. If you need extra variables, rename one of them so they have different names.
    Code:
       public static int transpose(matrix, int a, int b)
    matrix is coded wrongly here. It is the name of the variable, but the type is missing. There needs to be a data type for the first arg to transpose. Perhaps an int array? Like a and b both have the type of int.


    Comment on the names of variables:
    a,b,c,d etc are poor names to use as they do not describe what kind of data they hold. Better would be names like: row, rows, etc
    Last edited by NormR; July 3rd, 2013 at 09:57 AM.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    39
    Rep Power
    6
    Originally Posted by NormR
    a and b are already defined as arguments to the method. If you need extra variables, rename one of them so they have different names.
    Code:
       public static int transpose(matrix, int a, int b)
    matrix is coded wrongly here. It is the name of the variable, but the type is missing. There needs to be a data type for the first arg to transpose. Perhaps an int array? Like a and b both have the type of int.


    Comment on the names of variables:
    a,b,c,d etc are poor names to use as they do not describe what kind of data they hold. Better would be names like: row, rows, etc
    Well, I have updated the code. Now I have


    Code:
    import java.util.Scanner;
    class Transpose
    {
       public static int transpose(int[][] matrix, int a, int b)
       {
          int a, b, c, d;
    
    
          
          int transpose[][] = new int[b][a];
    
          for ( c = 0 ; c < m ; c++ )
          {
             for ( d = 0 ; d < n ; d++ )               
                transpose[d][c] = matrix[c][d];         
    
          }
    
          System.out.println("Transpose of entered matrix:-");
    
          for ( c = 0 ; c < b ; c++ )
          {
             for ( d = 0 ; d < a ; d++ )
                   System.out.print(transpose[c][d]+"\t");
    
                System.out.print("\n");
          }
    	return 1;
       }
    	
    	public static void main(String args[])
       {
          int a, b, c, d;
    
          Scanner in = new Scanner(System.in);
          System.out.println("Enter the number of rows and columns of matrix");
          a = in.nextInt();
          b = in.nextInt();
    
          int matrix[][] = new int[a][b];
    
          System.out.println("Enter the elements of matrix");
    
          for ( c = 0 ; c < m ; c++ )
             for ( d = 0 ; d < n ; d++ )
                matrix[c][d] = in.nextInt();
    
    	transpose(matrix, c, d);
        
        }
    }
    I'm still getting two errors that say a and b are already defined in transpose method, but where are they already defined?
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Code:
     public static int transpose(int[][] matrix, int a, int b)
          // The above defines the variables: matrix, a and b
    a,b,c,d etc are poor variable names. What data do those variables hold?
    Rename the variables to show what they hold.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo