Question

In: Computer Science

Given a 2D array a, sum up ALL the edges of the array. Ex. int a[...

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

Solutions

Expert Solution

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:


Related Solutions

Which row has the largest sum? Write a method that takes a 2D int array and...
Which row has the largest sum? Write a method that takes a 2D int array and prints: of The index of the row that has the largest sum The sum of elements in that row java
Print all subset sums of a given array recursively and iteratively. ex) if the input is...
Print all subset sums of a given array recursively and iteratively. ex) if the input is {1, 2, 3}, it should return {0,1,2,3,3,4,5,6}. public int[] subsetSum1(int[] arr) { *recursive*} public int[] subsetSum2(int[] arr) {*iterative*}
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
PYTHON Given a 2D array with the layout of the floor of a concert hall and...
PYTHON Given a 2D array with the layout of the floor of a concert hall and the height (in dwillyinches, a logarithmic measurement of height) of each concert attendee, write a program that determines if every attendee can see the front stage. An attendee can see the front stage if they are strickly taller than all of the attendees in front of them. Everyone can see the front-stage in the example below: # FRONT STAGE [[1, 2, 3, 2, 1,...
Please use Java eclipse Find pair in an array with given sum Given an array of...
Please use Java eclipse Find pair in an array with given sum Given an array of integers A and an integer S, determines whether there exist two elements in the array whose sum is exactly equal to S or not. Display 1 a pair is found in an array with matching sum S else 0. Input     6     5     1 -2 3 8 7     Where, First line represents integer S. Second line represents the size of an array. Third line represents...
Write a method that will accept a 2D character array. The 2D array represents a grid...
Write a method that will accept a 2D character array. The 2D array represents a grid (table) of characters in which a triple may occur. A triple is 3 matching characters. This method will search through the array to determine whether or not it contains a set of 3 matching characters. Specifically, it will check to see if the 3 matching characters appear somewhere in the array as three adjacent characters either horizontally (left to right) or vertically (top to...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size 3x3 as private member.        There should be two public methods within the class definition namely: void setMatrixValue(int i, int j); that should set m[i][j] with user defined values int getMatrixValue(int i, int j); that should return m[i][j] Make a global function named ‘CrossProduct(Matrix m1, Matrix m2)’ that should compute the marix multiplication
Implement a method/function in java that produces running sum runningSum2DArray(int[][] array, int dir) across rows (left...
Implement a method/function in java that produces running sum runningSum2DArray(int[][] array, int dir) across rows (left to right or right to left) or columns (top to bottom or bottom to top) Input to the method: A 4x4 two dimensional int array and an integer (1, 2, 3 or 4 for left, right, up,down respectively). Output: The modified array after producing the running sums according to the direction. For example: If the input to the method is the same as the...
This is C++ programming Given an int variable k, an int array incompletes that has been...
This is C++ programming Given an int variable k, an int array incompletes that has been declared and initialized, an int variable nIncompletes that contains the number of elements in the array, an int variable studentID that has been initialized, and an int variable numberOfIncompletes, Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes. I tried this code...
Write a fortran 90 program that sets up a 4x4 2D real array A and associate...
Write a fortran 90 program that sets up a 4x4 2D real array A and associate a single value pointer AP with element (2,1) of that array. Set all elements in A equal to 0. and print the value of AP to the screen. Next set all elements in A equal to 1.23 and print the value AP to the screen.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT