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,...
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...
3. Write a program which divides a given array (int array []) into two parts: -...
3. Write a program which divides a given array (int array []) into two parts: - Left part with all elements with values <= (less than or equal) pivot (int pivot) and the - Right part with all elements with values > (larger than) pivot. You should ask the user to enter the values of: int pivot, int size and the array
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.
Project: Given an array numbers. We define a running sum of an array as runningSum[i] =...
Project: Given an array numbers. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of numbers. Example: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. You need to do: Create a class called RunningSumArray to perform the main method. Create a class called ArrayOperationto hold the actions for array. In this project, you will need 4methods: 1. Public static Int[] getArray() --- inside ArrayOperationclass,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT