java program to compute the greatest sum of four consecutive integers in the fol
ID: 3551067 • Letter: J
Question
java program to compute the greatest sum of four consecutive integers in the following four directions:
(1) Horizontally
(2) Vertically
(3) In the main diagonal direction
(4) In the off diagonal direction
After I run the programe. It need to ask for inputs (for any grid size. dont need to ask for the grid size, just input only)
Here's the input for above grid
50 47 1 67 50 70 72 26 20 19 80 87 25 98 30 36 94 61 42 65 33 28 88 10 67 81 95 91 36 29 97 97 88 32 34 61 30 5 19 19 63 69 13 72 79 77 28 42 79 39 43 6 29 24 88 18 55 94 84 92 22 60 16 37 85 23 29 57 4 39 47 0 74 73 35 3 9 23 69 62 83 51 89 56 27 64 73 99 83 38 53 65 34 96 0 97 48 80 22 14 76 84 22 11 72 4 81 19 88 31 90 99 68 91 9 67 50 11 73 7 44 55 55 27 10 13 34 52 7 25 8 15 39 24 88 49 59 98 94 18 14 91 68 88 35 77 80 0 21 42 41 87 37 68 85 68 29 34 1 53 46 30 28 46 0 54 46 70 1 82 19 18 12 19 2 50 64 45 76 81 92 53 73 1 48 4 23 64 54 45 67 87 97 50 14 44 44 84 72 49 52 41 79 31 76 7 73 59 90 56 64 8 20 25 63 23 51 89 39 39 91 21 27 24 49 5 12 88 26 49 80 32 32 48 31 61 23 21 98 98 43 98 57 47 56 69 27 73 82 47 68 76 12 26 54 91 54 52 34 10 58 56 80 69 47 32 79 88 87 15 16 54 32 2 38 88 56 45 50 43 81 7 30 38 21 64 4 96 49 86 92 92 59 47 53 35 25 94 62 65 69 7 50 78 16 67 19 10 76 67 24 67 51 73 12 16 68 80 84 71 1 48 41 47 57 32 30 7 14 89 28 17 63 83 29 45 52 85 89 89 64 53 31 40 85 44 35 80 63 3 70 60 82 73 51 71 61 80 30 10 51 91 44 55 48 57 15 60 40 27 14 2 39 67 1 78 1 45 83 30 19 14 12 3 86 36 65 74 31 11 66 21 86 55 69 79 60 14 11 20
answers should be
horizontal: 272
vertical: 185
main diagonal: 224
off diagonal: 222
Explanation / Answer
import java.util.Scanner;import java.util.ArrayList;import java.lang.*;class PerfectSquare { private ArrayList a; //abitrary size, use ArrayList instead of fix-sized array private int n; //dimension public PerfectSquare() { a = null; n = 0; } // Constructor use a Scanner public PerfectSquare(Scanner scanner) { a = new ArrayList(); while (scanner.hasNextInt()) a.add(scanner.nextInt()); n = (int) (Math.sqrt(a.size()) + 0.5); if (n*n != a.size()) throw new RuntimeException("Invalid dimension"); } public int dimension() { return n; } // Horizontal sum // Return sum of a[r][c], a[r][c+1], a[r][c+2], ..., a[r][c+count-1] // If c+count-1 >= n, or c > n-count, throw an IndexOutOfBoundsException // We use 1-d array instead of 2d-arry, so: // a[r][c+k] == a[i+k] private int hzSum(int r, int c, int count) { if (count < 1) throw new IllegalArgumentException(); if (c > n-count) throw new IndexOutOfBoundsException(); int id = r*n + c; int sum = 0; for (int i = 0; i = n, or r > n-count, throw an IndexOutOfBoundsException // a[r+k][c] == a[i+kn] private int vtSum(int r, int c, int count) { if (count < 1) throw new IllegalArgumentException(); if (r > n-count) throw new IndexOutOfBoundsException(); int id = r*n + c; int sum = 0; for (int i = 0; i = n, throw an IndexOutOfBoundsException // a[r+k][c+k] == a[i+kn+k] private int mdSum(int r, int c, int count) { if (count < 1) throw new IllegalArgumentException(); if (r > n-count || c > n-count) throw new IndexOutOfBoundsException(); int id = r*n + c; int sum = 0; for (int i = 0; i = n or c-count+1 < 0, throw an IndexOutOfBoundsException // a[r+k][c-k] == a[i+kn-k] private int odSum(int r, int c, int count) { if (count < 1) throw new IllegalArgumentException(); if (r > n-count || cRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.