Reputation: 992
I've attached an example of what I am tasked with creating. For now I am just focusing on the lower triangular part of the matrix. For some reason the output is completely blank when I run the program. Any help here is much appreciated.
while (rowsAndColumns < 4 || rowsAndColumns > 20) {
System.out.print("How many rows and columns (min 4 & max 20)? ");
rowsAndColumns = input_scanner.nextInt();
}
int[][] intMatrix = new int[rowsAndColumns][rowsAndColumns];
System.out.print("What is the starting number for your integer filled square (between 0 and 50 inclusive): ");
startingNumber = input_scanner.nextInt();
for (x = 0; x <= (rowsAndColumns - 1); x++) {
for (y = 0; y <= (rowsAndColumns - 1); y++) {
intMatrix[y][x] = startingNumber;
startingNumber++;
}
}
for (x = 0; x < intMatrix.length; x++) {
for (y = 0; y < intMatrix[x].length; y++) {
System.out.print(intMatrix[x][y] + " ");
}
System.out.println("");
}
Upvotes: 2
Views: 484
Reputation: 418
Here , I have made a solution according to your task.
In your task there is First level challange is to calculating last element of matrix based on starting number which entered by user that will be anything and according to your task there is starting number is 1 so it's easy to calculate but here I made an formula which will calculate last element of matrix.
Let's suppose you have entered your matrix size is X*X and starting number is M and if I am denoting lastElement of matrix using K than :
K = totalMatrixElement(X*X) + (M-1);
Now, I have lower and upper element of matrix which are :
lowerConter = M and upperCounter = K
Now I am ready to fill my matrix and print out my matrix in various format like displayCompleteMatrix or displayLowerTrangleMatrix or displayUpperTrangleMatrix.
Have a Look of My Program :
package com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LowerTrangleMatrix {
public static void main(String...strings) throws NumberFormatException, IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int rowsAndColumns = 0;
boolean flag = true;
do {
try {
System.out.println("How many rows and columns (min 4 & max 20)? ");
rowsAndColumns = Integer.parseInt(read.readLine());
if ((rowsAndColumns > 3 && rowsAndColumns < 21)) {
System.out.println("Your row * column is " + rowsAndColumns + "*" + rowsAndColumns);
flag = false;
} else {
System.out.println("Your Input is Not Acceptable try again...");
}
} catch (NumberFormatException ex) {
System.out.println("Input must be Integer.");
}
} while (flag);
int[][] matrix = new int[rowsAndColumns][rowsAndColumns];
int startingNumber = 0;
flag = true;
do {
try {
System.out.print("What is the starting number for your integer filled square (between 0 and 50 inclusive): ");
startingNumber = Integer.parseInt(read.readLine());
if ((startingNumber > 0 && startingNumber < 50)) {
System.out.println("Your matrix will be start fillup with : " + startingNumber);
flag = false;
} else {
System.out.println("Your Input is Not Acceptable try again...");
}
} catch (NumberFormatException ex) {
System.out.println("Input must be Integer.");
}
} while (flag);
matrixFill(matrix, rowsAndColumns, startingNumber);
displayCompleteMatrix(matrix, rowsAndColumns);
displayLowerTrangleMatrix(matrix, rowsAndColumns);
displayUpperTrangleMatrix(matrix, rowsAndColumns);
}
public static void matrixFill(int[][] matrix, int rowsAndColumns, int startingNumber) {
int lowerCounter = startingNumber;
int totalMatrixElement = rowsAndColumns * rowsAndColumns;
int upperCounter = totalMatrixElement + (lowerCounter - 1);
for (int i = 0; i < rowsAndColumns; i++) {
for (int j = 0; j < rowsAndColumns; j++) {
if (j >= i) {
matrix[j][i] = lowerCounter++;
} else {
matrix[j][i] = upperCounter--;
}
}
}
}
public static void displayCompleteMatrix(int[][] matrix, int rowsAndColumns) {
System.out.println("Filled Matrix :::::::::::");
for (int i = 0; i < rowsAndColumns; i++) {
for (int j = 0; j < rowsAndColumns; j++) {
System.out.print(" " + matrix[i][j]);
}
System.out.println();
}
}
public static void displayLowerTrangleMatrix(int[][] matrix, int rowsAndColumns) {
System.out.println("Lower Trangle Matrix :::::::::::");
for (int i = 0; i < rowsAndColumns; i++) {
for (int j = 0; j < rowsAndColumns; j++) {
if (i >= j)
System.out.print(" " + matrix[i][j]);
else
System.out.print(" 0");
}
System.out.println();
}
}
public static void displayUpperTrangleMatrix(int[][] matrix, int rowsAndColumns) {
System.out.println("Upper Trangle Matrix :::::::::::");
for (int i = 0; i < rowsAndColumns; i++) {
for (int j = 0; j < rowsAndColumns; j++) {
if (i <= j)
System.out.print(" " + matrix[i][j]);
else
System.out.print(" 0");
}
System.out.println();
}
}
}
Upvotes: 2
Reputation: 521437
This answer is based on the inspiration that, while your required output is difficult to formulate in Java terms, the transpose of that matrix is actually quite straightforward:
1 2 3 4 5
25 6 7 8 9
24 23 10 11 12
22 21 20 13 14
19 18 17 16 15
It should be fairly obvious that to generate the above matrix, we can just keep two counters, starting at 1 and 25 respectively, and then fill in each row. Once we generate this matrix, we can print the transpose to get the actual output you want.
int size = 5;
int[][] matrix = new int[size][size];
int start = 1;
int end = size*size;
for (int r=0; r < size; ++r) {
int col=0;
while (col < r) {
matrix[r][col++] = end--;
}
while (col < size) {
matrix[r][col++] = start++;
}
}
// now print the transpose:
for (int r=0; r < size; ++r) {
for (int c=0; c < size; ++c) {
System.out.print(matrix[c][r] + " ");
}
System.out.println("");
}
This generates the following output:
1 25 24 22 19
2 6 23 21 18
3 7 10 20 17
4 8 11 13 16
5 9 12 14 15
Upvotes: 2
Reputation: 10184
For now I am just focusing on the lower triangular part of the matrix.
There's a very small error in your program. In your inner loop with y, you need to start with y=x, not y=0. Here's the working sample that will print the lower triangular:
Integer rowsAndColumns = 0;
Integer startingNumber = 0;
Scanner input_scanner = new Scanner(System.in);
System.out.print("How many rows and columns (min 4 & max 20)? ");
rowsAndColumns = input_scanner.nextInt();
System.out.print("What is the starting number for your integer filled square (between 0 and 50 inclusive): ");
startingNumber = input_scanner.nextInt();
int[][] intMatrix = new int[rowsAndColumns][rowsAndColumns];
for (int x = 0; x <= (rowsAndColumns - 1); x++) {
for (int y = x; y <= (rowsAndColumns - 1); y++) {
intMatrix[y][x] = startingNumber;
startingNumber++;
}
}
for (int x = 0; x < intMatrix.length; x++) {
for (int y = 0; y < intMatrix[x].length; y++) {
System.out.print(intMatrix[x][y] + " ");
}
System.out.println("");
}
Upvotes: 1