Created
January 3, 2016 20:21
-
-
Save 3355844/1a11351419dfc07fdca7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package HomeWork; | |
/** | |
* Created by Andrey on 02.01.2016. | |
*/ | |
public class ArraysTask10IdentityMatrixChecker { | |
public static void main(String[] args) { | |
int[][] matrix = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; | |
// int[][] matrix = {{1, 0, 2}, {0, 1, 0}, {2, 0, 1}}; | |
// int[][] matrix = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {0, 0, 0}}; | |
System.out.println(isIndetity(matrix)); | |
} | |
public static boolean isIndetity(int[][] matrix) { | |
checkRowsCols(matrix); | |
int check = 0; | |
int elemIdentity = 1; | |
int count = 0; | |
for (int i = 0; i < matrix.length; i++) { | |
if (matrix[i][i] == elemIdentity) count++; | |
for (int j = 0; j <matrix[i].length ; j++) { | |
if (matrix[i][j]>0)check++; | |
} | |
} | |
boolean identity = (matrix.length == count && count == check); | |
return identity; | |
} | |
public static void checkRowsCols(int[][] matrix) { | |
int rows = 0; | |
int cols = 0; | |
for (int i = 0; i < matrix.length; i++) { | |
rows++; | |
} | |
for (int i = 0; i < matrix[0].length; i++) { | |
cols++; | |
} | |
if (rows != cols) { | |
throw new IllegalArgumentException( "Your Array isn't cubic " ); | |
} | |
return; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package HomeWork; | |
/** | |
* Created by Andrey on 27.12.2015. | |
*/ | |
public class ArraysTask3ArrayPositiveFinder { | |
public static void main(String[] args) { | |
int[] array = {-3, -5, 1, -4, 2, -5}; | |
System.out.println(findFirstPositive(array)); | |
System.out.println(findLastPositive(array)); | |
} | |
public static int findFirstPositive(int[] array) { | |
int result = 0; | |
for (int i : array) { | |
if (i > 0) break; | |
result++; | |
if (result == array.length) return -1; | |
} | |
return result; | |
} | |
public static int findLastPositive(int[] array) { | |
int result = array.length - 1; | |
for (int i = result; i > 0; i--) { | |
if (array[i] > 0) break; | |
result--; | |
if (result == 0) return -1; | |
} | |
return result; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package HomeWork; | |
/** | |
* Created by Andrey on 30.12.2015. | |
*/ | |
public class ArraysTask8MatrixMaxSumRowFinder { | |
public static void main(String[] args) { | |
int[][] matrix = {{0, 3, -2}, {2, 1, 3}, {-1, 5, 2}}; | |
System.out.println(findMaxSumRow(matrix)); | |
} | |
public static int findMaxSumRow(int[][] matrix) { | |
checkArray(matrix); | |
int indexRow = -1; | |
int count = 0; | |
int maxSum = count; | |
for (int i = 0; i < matrix.length; i++) { | |
int sumRow = 0; | |
for (int j = 0; j < matrix.length; j++) { | |
sumRow = sumRow + matrix[i][j]; | |
} | |
if (sumRow > maxSum) { | |
maxSum = sumRow; | |
indexRow++; | |
} | |
} | |
return indexRow; | |
} | |
public static void checkArray(int[][] matrix) { | |
if (matrix.length == 0) { | |
throw new IllegalArgumentException(" Your Array has lenght 0 "); | |
}return; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package HomeWork; | |
/** | |
* Created by Andrey on 01.01.2016. | |
*/ | |
public class ArraysTask9MatrixTransposer { | |
public static void main(String[] args) { | |
int[][] sourceMatrix = {{1, 2, 3, 4, 9}, {5, 6, 7, 8,10}}; | |
transpose(sourceMatrix); | |
} | |
public static int[][] transpose(int[][] sourceMatrix) { | |
int[][] transposeMatrix = new int[sourceMatrix[0].length][sourceMatrix.length]; | |
for (int i = 0; i < sourceMatrix[0].length; i++) { | |
for (int j = 0; j < sourceMatrix.length; j++) { | |
transposeMatrix[i][j] = sourceMatrix[j][i]; | |
System.out.print(transposeMatrix[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
return transposeMatrix; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment