constructing a skew symmetric matrix - java

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++)

Related

Is this a correct way to analyze a graph? Warshall algorithm

Here's an implementation of Warshall algorithm copied from this site
https://rosettacode.org/wiki/Floyd-Warshall_algorithm#Java
What I need is to analyze this algorithm.I already did but I want to make sure everything is correct.
for (int i = 0; i < weights.length; i++) { //O(E)?
dist[weights[i][0] - 1][weights[i][1] - 1] = weights[i][2];}
for (int i = 0; i < next.length; i++) {//O(v^2)
for (int j = 0; j < next.length; j++)
if (i != j)
next[i][j] = j + 1;
}
for (int k = 0; k < numVertices; k++)//O(v^3)
for (int i = 0; i < numVertices; i++)
for (int j = 0; j < numVertices; j++)
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
next[i][j] = next[i][k];
}
//Finally takes O(v^3)

Need to change all values in a specific row and specific column that intersect to zero

The array is intialized with random numbers ranging between 0-20.
And if any value in that array is 0, this method should change all the values in that row and column to zero.
Instead it wipes out the array and changes all values to zero.
Any thoughts?
for (int row = 0; row < size; row++){
for (int col = 0; col < size; col++){
if (matrix[row][col] == 0){
for (int k = 0; k < size; k++)
matrix[k][col] = 0;
for (int l = 0; l < size; l++)
matrix[row][l] = 0;
}
}
}
System.out.println("Result:");
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++)
System.out.printf("[%1$2s]", copy[i][j]);
System.out.println();
}
For anyone who still cares, here is the full code
Random random = new Random();
int[][] matrix = new int[size][size];
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++)
matrix[i][j] = random.nextInt(20);
}
for (int row = 0; row < size; row++){
for (int col = 0; col < size; col++){
if (matrix[row][col] == 0){
for (int k = 0; k < size; k++)
matrix[k][col] = 0;
for (int l = 0; l < size; l++)
matrix[row][l] = 0;
}
}
}
System.out.println("Result:");
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++)
System.out.printf("[%1$2s]", matrix[i][j]);
System.out.println();
}

Java weird out of bounds?

I have no idea why I'm getting out of bounds for this.
int[][] board = new int[3][3];
for (int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; i ++) {
board[i][j] = 0;
}
}
You are incrementing i in the j for loop. Change
for(int j = 0; j < 3; i ++){
to
for(int j = 0; j < 3; j++){

Multiplying Muti-Dimensional Arrays

Been having a bit of trouble finishing this program involving two 2D arrays and multiplying them together. Now I was able to construct these arrays with set lengths and then using a number generator to create each array. As for a third array I was able to establish the length of the array, but when placing the three arrays into a method I am still having out of bounds issues.
public class arrayTest1{
public static void main ( String [] args){
int matrix1[][] = new int [5][2];
for (int i = 0; i < matrix1.length; i++)
for (int j = 0; j < matrix1[i].length; j++)
matrix1[i][j] = (int)(Math.random() * 1000);
System.out.println("The array 1 is: ");
for (int i = 0; i < matrix1.length; i++){
for (int j = 0; j < matrix1[i].length; j++){
System.out.print(matrix1[i][j]+" ");
}
System.out.println();
}
int matrix2[][] = new int [2][5];
for (int i = 0; i < matrix2.length; i++)
for (int j = 0; j < matrix2[i].length; j++)
matrix2[i][j] = (int)(Math.random() * 1000);
System.out.println("The array 2 is: ");
for (int i = 0; i < matrix2.length; i++){
for (int j = 0; j < matrix2[i].length; j++){
System.out.print(matrix2[i][j]+" ");
}
System.out.println();
}
int matrixSum[][] = new int [matrix1.length][matrix2[0].length];
matrixMulti(matrix1,matrix2,matrixSum);
System.out.println("The array mutliplied is: ");
for (int i = 0; i < matrixSum.length; i++){
for (int j = 0; j < matrixSum[i].length; j++){
System.out.print(matrixSum[i][j]+" ");
}
System.out.println();
}
}
public static void matrixMutli(int [][] m1, int [][] m2,int [][] totalMatrix){
for(int i = 0; i < m1.length; i++)
for(int j = 0; j < m2[0].length; j++)
for(int k = 0; k < totalMatrix.length; k++)
totalMatrix [i][j] += m1[i][k] * m2[k][j];
}
}
The ArrayIndexOutOfBounds is caused by the fact that your k variable goes from 0 to 5. It needs to go from 0 to 2. The problem is because of the line :
for(int k = 0; k < totalMatrix.length; k++)
The matrix totalMatrix is initialized with int matrixSum[][] = new int [matrix1.length][matrix2[0].length]; and matrix1.length is 5. So totalMatrix.length is 5.
To correct this you need to make sure that k is bounded by either matrix1[i].length or by matrix2.length. These two values need to be the same and you can choose either to be the bound for k.
So this is the code:
for(int i = 0; i < m1.length; i++)
for(int j = 0; j < m2[0].length; j++)
for(int k = 0; k < m2.length; k++)
totalMatrix [i][j] += m1[i][k] * m2[k][j];
Also consider adding code that checks whether m1[i].length == m2.length and throws an IllegalArgumentException if not. If you are still in trouble have a look at matrix multiplication.

Display matrix data

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();
}

Categories