Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following 10X 10 grid: 2 4 5 6 1 8 94 5 3 5 7 910 3 25 6 7 6 4 4 5

ID: 3719749 • Letter: C

Question

Consider the following 10X 10 grid: 2 4 5 6 1 8 94 5 3 5 7 910 3 25 6 7 6 4 4 5 10 8 5 6 7 8 4 7 7 93 27 14 9 0 5 6 8 8 8 77 5 5 7 8 8 3 7 8 44 4 6 3 10 8 16 737 8 25 9 2 4 8 3 8 2 46 7 10 4 4 3 6 814 37 3 4 The upper left hand comer is (1, 1). The upper right is (10, 1) and the lower right is (10, 10) The input is four sets of two non-adjacent comers of a rectangle, in no paricular order. Ihe output is the sum of the numbers within the defined rectangle. SAMPLE INPUT 2 2 3 3 1 10 4 10 6 6 4 8 10 10 10 8 SAMPLE OUTPUT 20 21 47 10 TEST INPUT 1 10 3 8 6 675 65 7 6 1 3 3 1 TEST OUTPUT 62 18 18 40

Explanation / Answer

import java.util.Scanner;


public class RectanglSum {
public static void main(String[] args) {
int arr[][] = {
{2,4,5,6,1,8,9,1,4,5},
{3,5,7,9,10,3,2,5,6,7},
{6,4,4,5,10,8,5,6,7,8},
{4,7,7,9,3,2,7,14,9,0},
{5,6,8,8,8,7,7,5,5,7},
{6,5,4,6,4,1,3,6,8,7},
{8,8,3,7,8,4,4,4,6,3},
{10,8,16,7,3,7,8,25,9,2},
{4,8,3,8,2,4,6,7,10,4},
{4,3,6,8,1,4,3,7,3,4}
};
int input = 4;
Scanner sc = new Scanner(System.in);
for (int i = 0; i < input; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int leftx = x;
int rightx = x2;
  
if (x > x2) {
leftx = x2;
rightx = x;
}
int lefty = y;
int righty = y2;
if (y > y2) {
lefty = y2;
righty = y;
}
  
int sum = 0;
for (int j = leftx-1; j < rightx; j++) {
for (int k = lefty-1; k < righty; k++) {
sum += arr[k][j];
}
}
System.out.println(sum);
}
sc.close();
}

}

Sample run

2 2 3 3
20
1 10 4 10
21
6 6 4 8
47
10 10 10 8
10

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote