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
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
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...
// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
In all cases we have an array of int or char of some fixed size. The...
In all cases we have an array of int or char of some fixed size. The program will prompt the user to enter some values, such as: Enter 7 integers to be stored in the array: 5 13 8 5 1 2    The questions that could then be asked of this data might be: Count how many numbers are < the last element Count how many numbers are odd Similarly we might prompt for character input: Enter 7 characters...
A.) myNums is an array of 50 elements of type int and k is an int...
A.) myNums is an array of 50 elements of type int and k is an int variable. For which one we get the index of out of bounds? a. for (k = 0; k <= 49; k++)     cout << myNums[k] << " "; b. for (k = 1; k < 50; k++)     cout << myNums[k] << " "; c. for (k = 0; k <= 50; k++)     cout << myNums[k] << " "; d. for (k =...
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ).
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ). Given a sorted array of distinct integers and a single integer value, find all pairs of indexes in the list whose product is equal to the value. Return the index pairs on the same row of a two-dimensional array. Note, m will be the number of rows and total number of pairs found, while 2 will be the number of columns along every row...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT