What's wrong with this code? It is supposed to take in an array along with a sum to find. then it searches that array to find consecutive vertical or horizontal numbers that sum up to the sum you are looking for.
Tester class.public class FindTheSums { public static String arrayToString(int[][] a) { String string = ""; for (int row = 0; row < a.length; row++) { for (int col = 0; col < a[0].length; col++) { System.out.print(a[row][col] + " "); } System.out.print("\n"); } return string; } public static int[][] horizontalSums(int[][] a, int sumToFind) { int currentSum = 0; int goalSum = 0; int[][] outputArray = new int[a.length][a[0].length]; for (int row = 0; row < a.length; row++) { for (int col = 0; col < a[row].length; col++) { int currentPosition = col; currentSum = 0; int start = col; while (currentSum < goalSum) { currentSum = a[row][currentPosition] + currentSum; currentPosition++; } if (currentSum == goalSum) { for (int i = start; i < currentPosition; i++) { outputArray[row][i] = a[row][i]; } } } } return outputArray; } public static int[][] verticalSums(int[][] a, int sumToFind) { int currentSum = 0; int goalSum = 0; int[][] outputArray = new int[a.length][a[0].length]; for (int row = 0; row < a.length; row++) { for (int col = 0; col < a[row].length; col++) { int currentPosition = col; currentSum = 0; int start = col; while (currentSum < goalSum) { currentSum = a[row][currentPosition] + currentSum; currentPosition++; } if (currentSum == goalSum) { for (int i = start; i < currentPosition; i++) { outputArray[row][i] = a[row][i]; } } } } return outputArray; } }
public class FindTheSumsTester { private static int[][] array1 = { {3, 2, 1, 1}, {2, 5, 6, 2}, {1, 2, 9, 8} }; public static void main(String[] args){ arrayToStringTest(); horizontalSumsTest(); verticalSumsTest(); } private static void arrayToStringTest() { String methodCallResult, correctAnswer; System.out.println("Testing arrayToString method:"); methodCallResult = FindTheSums.arrayToString(array1); correctAnswer = "3 2 1 1\n2 5 6 2\n1 2 9 8"; if(methodCallResult.equals(correctAnswer)){ System.out.println("arrayToString(array1) test passed"); } else{ System.out.println("arrayToString(array1) test failed"); } } private static void horizontalSumsTest(){ int[][] horizontalSums; String arrayAsString; System.out.println("Testing horizontalSums method:"); System.out.println("array1:"); arrayAsString = FindTheSums.arrayToString(array1); System.out.println(arrayAsString); System.out.println("horizontalSums(array1, 7):"); horizontalSums = FindTheSums.horizontalSums(array1, 7); arrayAsString = FindTheSums.arrayToString(horizontalSums); System.out.println(arrayAsString); } private static void verticalSumsTest(){ int[][] verticalSums; String arrayAsString; System.out.println("Testing verticalSums method:"); System.out.println("array1:"); arrayAsString = FindTheSums.arrayToString(array1); System.out.println(arrayAsString); System.out.println("verticalSums(array1, 22):"); verticalSums = FindTheSums.verticalSums(array1, 22); arrayAsString = FindTheSums.arrayToString(verticalSums); System.out.println(arrayAsString); } }
Desired output:
25ae6dce537335dccf8aa5c5ecfb64ca.png3201391f593dba55e71541d15001fa85.jpg
My output:
kk.jpg