I want to display matrix values just like a matrix
just like this
0 0 1 0 0
0 1 1 1 1
0 0 0 0 0
When I make this code, it appears vertically
for (int i = 0; i < mat.length; i++)
for (int j = 0; j < mat[i].length; j++)
System.out.println(mat[i][j]);
System.out.println prints a line and a break line. You would want to use System.out.print that only prints the data.
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[i].length; j++) {
System.out.print(mat[i][j]);
}
System.out.println();
}
But now the problem is that all the data in the same row is printed with no space. You can use System.out.print(mat[i][j] + " "); but that's a little clumsy. The best option would be to use System.printf to allow formatting of the text that will be printed on the console:
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[i].length; j++) {
//assuming mat[i][j] content is int...
System.out.printf("%3d", mat[i][j]);
}
System.out.println();
}
More info about formatting with System.out.printf: Format String syntax
Try this: Use print in inner loop which will print one row in a single line. Use println in outer loop which will separate each row.
for (int i = 0; i < mat.length; i++){
for (int j = 0; j < mat[i].length; j++)
System.out.print(mat[i][j]);
System.out.println();
}
Related
How can I find the total sum for each two dimensional array row? I'm completely stuck...
public static void main(String[] args) {
int [][] grid = new int [10][10];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = (int)(Math.random()*99);
}
}
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
System.out.print("1.");
System.out.printf("%5d ", grid[i][j]);
}
System.out.println();
}
}
My current output is:
How can I show the total sum for each row in the end of the row and show column numbers
For the sum of a row, this should do. In a similar way within the i loop, if you need to count the column as well;
for(int i = 0; i < 10; i++) {
int jSum = 0;
for(int j = 0; j < 10; j++) {
jSum += grid[i][j];
System.out.print("1.");
System.out.printf("%5d ", grid[i][j]);
}
System.out.printf(" %5d", jSum);
System.out.println();
}
On the column numbering:
Either you just pust put a static print in the beginning (like print "1 2 3 4..."), or you put the following with the j loop:
if (i == 0) System.out.printf("%5d ", j); // only prints in first loop / row - print 1,2,3,4,5....
I am trying to construct a skew symmetric matrix. I can print the negatives but I am unable to print the diagonal to be 0. Where am I incorrect logically?
public void SkewSymmetric() {
for (int i = 0; i < matrix.length; ++i) {
for (int j = 1; j < matrix.length; i++) {
matrix[i][j] = random.nextInt(BOUND);
}
}
for (int i = 9; i < matrix.length; ++j) {
for (int j = 0; j < i; ++j) {
matrix[j][j] = -matrix[i][i];
}
}
}
Your inner loop in the second block needs to run from i + 1.
Otherwise the trace will be, in general, non-zero.
You are setting all the values in the matrix to 0 and then trying over-write the non-diagonal values.
Instead set only the primary diagonal values to 0.
As pointed out by Jean, your loop values are incorrect.
For the first row random numbers are generated from column 1 to n
For the second row random numbers are generated from column 2 to n and so on.
Try this,
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix.length; j++){
if(i==j){
matrix[i][j] = 0;
}
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = i+1; j < matrix.length; j++) {
matrix[i][j] = random.nextInt();
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < i; j++) {
matrix[i][j] = -matrix[j][i];
}
}
The problem comes from for (int j = i; j < matrix.length; j++): when j == i at the first iteration, it erases the 0 that have been previsouly set.
Therefore you need to change it to for (int j = i+1; j < matrix.length; j++)
I'm trying to fill a matrix vertically, but 1 row is missing. Can you help me ? There is the code. Maybe there is an easier way to fill a matrix verically, but i cant find it.
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of matrix: ");
int n = input.nextInt();
int [][] matrix = new int [n][n];
for (int i = 1; i < matrix.length; i++) {
matrix[0][i] = matrix[0][i -1] + n;
}
for(int i = 1; i < matrix.length; i++){
for (int j = 0; j < matrix.length; j++){
matrix[i][j] = matrix[i -1][j] + 1;
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
input.close();
}
Output:
Enter the value of matrix: 4
1 5 9 13
2 6 10 14
3 7 11 15
Your row is missing because you never printed it in your first loop (the one that is initializing your first line) - you should have a row of 0 4 10 12 at the beginning. But you could do it much easier with only one nested loop.
Try
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of matrix: ");
int n = input.nextInt();
int [][] matrix = new int [n][n];
matrix[0][0]=0; //you have forgotten the first value
for (int i = 1; i < matrix.length; i++) {
matrix[0][i] = matrix[0][i -1] + n;
//initializing the first line
}
for(int i = 1; i < matrix.length; i++){
for (int j = 0; j < matrix.length; j++){
matrix[i][j] = matrix[i -1][j] + 1;
}
// re-loop to display but this time start with i=0
for(int i = 0; i < matrix.length; i++){
for (int j = 0; j < matrix.length; j++){
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
input.close();
}
To fill a matrix vertically, you must loop through columns in the outer loop and through rows in the inner(nested) loop. For example:
for(int j = 0; j < matrix.length; j++) {
for(int i = 0; i < matrix.length; i++) {
matrix[i][j] = /* The value you want to fill */;
.../* Other stuff you wanna do */
}
}
There is an easier way of doing this:
keep a variable like count and iterate the matrix on columns first then rows:
int count = 1; // or 0 if you start with 0
int[][] a = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
a[j][i] = count; // notice j first then i
count++;
}
After that you can easly print out the values:
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
System.out.println(a[i][j]);
Given an array of integers with rows of different lengths, is it possible to print the whole two-dimensional array but doing so column by column? I understand how to do it row by row but I am struggling with this.
int[][] a = new int[5][];
a[0] = new int[4];
a[1] = new int[2];
a[2] = new int[5];
a[3] = new int[3];
a[4] = new int[1];
int longestRowLength = a[0].length;
for(i = 1; i < a.length; i++)
{
if(a[i].length > longestRowLength)
longestRowLength = a[i].length;
}
for(i = 0; i < a.length; i++)
{
for(j = 0; j < a[i].length; j++)
{
a[i][j] = rand.nextInt(10);
System.out.print(a[i][j]);
}
System.out.println();
}
for(j = 0; j < longestRowLength; j++)
{
for(i = 0; i < a.length; i++)
{
if(a[i].length < longestRowLength)
continue;
System.out.print(a[i][j]);
}
}
}
I have done this but the issue is with how to recognize we are going out of bounds with one of the arrays. My if(a[i].length < longestRowLength doesn't work as it will not even print any numbers if its length is not the longest ones. How can I achieve this?
EDIT:
Ok I have changed that line to:
if(longestRowLength - a[i].length > 0 && (j+1) > a[i].length)
continue;
System.out.print(a[i][j]);
Now it works but it prints the columns as rows. Is there anyway to make it print column by column but to make it print just like it would with rows? (P.S. yeah the first condition of the if statement is unecessary).
Replace your last loop with:
for(j = 0; j < longestRowLength; j++)
{
for(i = 0; i < a.length; i++)
{
if(a[i].length <= j)
continue;
System.out.print(a[i][j]);
}
System.out.println();
}
Prints the columns one by one.
Instead of:
if(a[i].length <= j)
continue;
you can do:
if(a[i].length <= j) {
System.out.print(' ');
continue;
}
to leave a space for arrays which are too short. This way you print the transposed “jagged” matrix.
My professor wants me to print out the matrices side by side with the "+" between the two matrices and then a "=" sign. In the end he wants us to add the matrices together.
This is the work so far.
So the result would come out as:
1 2 3 9 8 7 10 10 10
4 5 6 + 6 5 4 = 10 10 10
7 8 9 3 2 1 10 10 10
enter code here public static void main(String[] args) {
int matrix1[][] = {{1,2,3},{4,5,6},{6,7,8}};
int matrix2[][] = {{9,8,7},{6,5,4},{3,2,1}};
int result1;
int[][] result2 = new int[2][3];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
System.out.print("");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
}
My problem is, how could I print it side by side with the solutions?
Consider the two printing loops for your matrices:
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
They print matrix 1, then 2 - and so the matrices will be below each other.
If you want the matrices side by side, you need to print line 1 of every matrix, then - after a new line - line 2 of every matrix, etc. By re-arranging how the loops go through the matrices, you could have your new layout.
You unfortunately cannot print them one at a time, you need to take it row by row. This solution requires both matrix1 and matrix2 to be of equal height. But here's a template that should get you started.
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
}
if (i == matrix1/2) {
} else { //One part of if handles when "+" is needed, other one doesn't
}
for (int j = 0; j < matrix2[i].length; j++) {
}
if (i == matrix1/2) {
}
for (int j = 0; j < ???; j++) {
}
}