Question

In: Computer Science

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

Solutions

Expert Solution

The required method is given below:

//method to return the row number having the largest sum
public static void largestSum(int arr[][])
{
//variable declaration and initialization
int sum = 0, largSum=0;
int idx = 0;
  
//find the index of largest sum row
for(int i = 0; i < arr.length; i++)
{
sum = 0;
for(int j = 0; j < arr[0].length; j++)
{
sum = sum + arr[i][j];
}
if(sum>largSum)
{
largSum = sum;
idx = i;
}
}
  
//display the result
System.out.println("The index of the row that has the largest sum: "+idx);
System.out.println("The sum of elements in that row: "+largSum);
}

The complete program source code including the above method for testing is given below:

public class Main
{
//method to return the row number having the largest sum
public static void largestSum(int arr[][])
{
//variable declaration and initialization
int sum = 0, largSum=0;
int idx = 0;
  
//find the index of largest sum row
for(int i = 0; i < arr.length; i++)
{
sum = 0;
for(int j = 0; j < arr[0].length; j++)
{
sum = sum + arr[i][j];
}
if(sum>largSum)
{
largSum = sum;
idx = i;
}
}
  
//display the result
System.out.println("The index of the row that has the largest sum: "+idx);
System.out.println("The sum of elements in that row: "+largSum);
}
  
   public static void main(String[] args)
   {
   //array declaration and initialization
       int arr[][] = {{1, 2, 3}, {4, 5, 6}, {2, 1, 3}};
      
       //method calling
       largestSum(arr);
   }
}

OUTPUT:

The index of the row that has the largest sum: 1
The sum of elements in that row: 15



Related Solutions

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
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...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
write a function declaration for a 2d array where each row size is 8 and the...
write a function declaration for a 2d array where each row size is 8 and the function does not return anything.
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. IN C Program.
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. Need it in 10 minutes, please.
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
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
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT