Question

Write a method called sumDiagonal() that takes a 2D array of int as an argument returns...

Write a method called sumDiagonal() that takes a 2D array of int as an argument returns the sum of the first element in the first row, the second element of the second row, the third element of the third row, etc. You may assume that the array is not jagged and has at least as many columns as rows.

Homework Answers

Answer #1
public class SumDiagonal {

    public static int sumDiagonal(int[][] m) {
        int sum = 0;
        for (int i = 0; i < m.length; i++) {
            sum += m[i][i];
        }
        return sum;
    }

    public static void main(String[] args) {
        int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        System.out.println("Array is:");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("Sum of elements of diagonal is " + sumDiagonal(arr));
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Java Program Write a method called “fillMatrix” that takes two integers rows and cols and returns...
Java Program Write a method called “fillMatrix” that takes two integers rows and cols and returns a 2D array of integers of size rows x cols where the value of element [row][col] is row + col. Write a short program to test your method.
(In Java) 1. Create a 6X6 2D Array called numCourses 2. Sum the rows and print...
(In Java) 1. Create a 6X6 2D Array called numCourses 2. Sum the rows and print out the results with text identifying what you are outputting. 3. Sum the columns and print out the results with text identifying what you are outputting. 4. Compare the rows output and calculate and output the minimum value and maximum value. */ public class TwoDArrayExamplesHW{ public static void main(String[] args){ int[][] numCourses = {{2, 3, 2, 0, 0},    {2, 3, 2, 3, 1},...
Write a method called findNums that takes a two-dimension array of integers and an int as...
Write a method called findNums that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class Question2 {    public static void...
Write a c++ function which takes two parameters: an array of ints and an int size...
Write a c++ 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. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void printSome(const int array[], int size);
In Java: The maximum-valued element of an int-valued array can be recursively calculated as follows: •...
In Java: The maximum-valued element of an int-valued array can be recursively calculated as follows: • If the array has a single element, that is its maximum (note that a zero-sized array has no maximum • Otherwise, compare the first element with the maximum of the rest of the array-- whichever is larger is the maximum value. Write an int method named max that accepts an int array, and the number of elements in the array and returns the largest...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an unknown number of cells filled with data. Your goal is to iterate through the 2D array and keep a count of how many cells are full. You will be given the dimensions of the array. INPUT: All input has been handled for you: Two integers denoting the dimensions of the array. These are the integer variables rows and cols. A partially filled 2-D array....
Write a method name maxElement, which returns the largest value in an array that is passed...
Write a method name maxElement, which returns the largest value in an array that is passed as an argument. The method must use recursion to find the largest element. Demonstrate the method in a program. Write the JAVA CODE on the paper that is provided. (Put your name on every piece of paper) Use the following array to test the method. Int [] numbers = {2,12,1999,99,100,4,7,300} PROGRAMMING LANGUAGE- JAVA. Please answer the question ASAP. Thanks in advance!
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering...
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering “Scanner in = new Scanner (System.in);”, input a value into element 5 of one-dimensional double array nums. c) Initialize each of the four elements of the one-dimensional integer array test to 7. d) Declare and create an array called table as a float array that has four rows and three columns. e) Considering the following ArrayList declaration, insert “test” at the fourth position (index...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT