In: Computer Science
Given a 2D array a, sum up ALL the edges of the array.
Ex. int a[ ][ ] = { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12} };
OUTPUT: Sum of the edges = 65
CODE IN JAVA:
import java.util.Scanner;
public class CellPhoneService {
public static void main(String[] args) {
int arr[][] = { {1, 2, 3, 4},{5, 6,
7, 8},{9, 10, 11, 12}};
int rows = arr.length ;
int cols = arr[0].length ;
int sum = 0 ;
for(int i = 0 ; i < cols; i++)
{
sum += arr[0][i]
;
sum +=
arr[rows-1][i];
}
for(int j = 1; j < rows-1; j++)
{
sum +=
arr[j][0];
sum +=
arr[j][cols-1];
}
System.out.println("Sum of edges :
" + sum);
}
}
OUTPUT: