Created
December 30, 2015 15:48
-
-
Save 3355844/ab19c7abd2387bb5bdf2 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 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 = 0; i < array.length; i++) { | |
if (array[i] > 0) { | |
return result; | |
} | |
result++; | |
} | |
if (array[result - 1] > 0) { | |
return result; | |
} else { | |
return -1; | |
} | |
} | |
public static int findLastPositive(int[] array) { | |
int result = 5; | |
for (int i = array.length - 1; i > 0; i--) { | |
if (array[i] > 0) { | |
return result; | |
} | |
result--; | |
} | |
if (array[result] > 0) { | |
return result; | |
} else { | |
return -1; | |
} | |
} | |
} | |
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 ArraysTask6MatrixPrinter { | |
public static void main(String[] args) { | |
int height = 8; | |
int length = height; | |
double[][] matrix = new double[height][length]; | |
printMatrix(matrix); | |
} | |
public static double[][] printMatrix(double[][] matrix) { | |
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(); | |
} | |
return matrix; | |
} | |
} |
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 ArraysTask7MatrixAverageCalculator { | |
public static void main(String[] args) { | |
double[][] matrix = {{0, 3, -2}, {2, 1, 3}, {-1, 5, 2}}; | |
System.out.println(calculateAverage(matrix)); | |
} | |
public static double calculateAverage(double[][] matrix) { | |
double sum = 0; | |
int n = 0; | |
for (int i = 0; i < matrix.length; i++) { | |
for (int j = 0; j < matrix.length; j++) { | |
sum += matrix[i][j]; | |
n++; | |
} | |
} | |
return sum / n; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment