-1

I'm trying to write a program that prints a randomly generated matrix and a transposed matrix, but I can't quite figure it out. When I attempt to compile I get the error on line 41 "error: cannot find symbol printMatrix(transposedMatrix); ^ I've worked on this for a few hours now and can't figure it out, thanks in advance for the help.

I apologize if the formatting for this code is a little off, it looks fine in Sublime and I'm not not used to this site.

import java.util.Scanner;

public class Matrix {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

int rows = 0;
int cols = 0;

while (rows < 1 || rows > 10) {
System.out.print("Enter the number of rows (1-10): ");
int userRows = input.nextInt();

if (userRows < 1 || userRows > 10) {
  System.out.println("ERROR! The number of rows cannot be outside the specified range of 1-10!");
}
else userRows += rows;
}

while (cols < 1 || cols > 10) {
System.out.print("Enter the number of columns (1-10): ");
int userCols = input.nextInt();

if (userCols < 1 || userCols > 10) {
  System.out.println("ERROR! The number of columns cannot be outside the speified range of 1-10!");
}
else userCols += cols;
}

int[][] originalMatrix = new int[rows][cols];

for (int row = 0; row < originalMatrix.length; row++)
  for (int col = 0; col < originalMatrix[row].length; col++) {
    originalMatrix[row][col] = (int) (Math.random() * 1000);
  }

System.out.println("\nOriginal matrix:");
printMatrix(originalMatrix);

System.out.println("\nTransposed matrix:");
printMatrix(transposedMatrix);
}

public static void printMatrix(int[][] matrix) {
for (int row = 0; row < matrix.length; row++) {
  for (int col = 0; col < matrix[row].length; col++) {
    System.out.print(matrix[row][col] + "  ");
  }
  System.out.println();
} 
} 

public static int[][] transposedMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int[][] transposedMatrix = new int[n][m];
for(int x = 0; x < n; x++) {
    for(int y = 0; y < m; y++) {
        transposedMatrix[x][y] = matrix[y][x];
    }
}
return transposedMatrix;
}
}

2 Answers2

2

Change your print statement to call the method you created. Make sure to also pass in the originalMatrix.

printMatrix(transposedMatrix(originalMatrix));

Also off topic but you have a memory leak in your code. You never close Scanner after using it. It is best practice to close your scanner after you are done using it. Add this to the end of your main method to stop the memory leak.

input.close()

gonzo
  • 2,103
  • 1
  • 15
  • 27
  • 1
    This worked perfectly, thank you! I'm just a few weeks into java programming and haven't come across input.close() yet. I'll make sure to use that from now on, thanks again. – javastudent111 Oct 01 '15 at 19:01
0
System.out.println("\nTransposed matrix:");
printMatrix(transposedMatrix);

this should be

System.out.println("\nTransposed matrix:");
printMatrix(transposedMatrix(originalMatrix));
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80