Question

Write a method that takes in a two-dimensional array of integers, do some calculation for each...

Write a method that takes in a two-dimensional array of integers, do some calculation for each row in the two-dimensional array, and returns a one-dimensional array of sums for each row. Your code must work any array dimensions, including jagged arrays. The calculations are: • Sum of each row • Maximum value of each row • Minimum value of each row • Average of each row Here is an example an array passed and returns an array of sums for each row.

Sample array passed in: { { 3, 0, 7, 9 },

{ 2, 1, 0, 1 }

, { 9, 3, 4, 6 } };

IN java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class TwoDimensionalArrayOperations {
   public static void main(String[] args) {
       int arr[][] = { { 3, 0, 7, 9 },

               { 2, 1, 0, 1 }

               , { 9, 3, 4, 6 } };
       int sumOfRow[] = getSumOfRow(arr);
       int maxOfRow[] = getMaxOfRow(arr);
       int minOfRow[] = getMinOfRow(arr);
       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 rows: ");
       for(int i=0;i<sumOfRow.length;i++)
           System.out.print(sumOfRow[i]+" ");
       System.out.println("\nAverage of rows: ");
       for(int i=0;i<sumOfRow.length;i++)
           System.out.print(sumOfRow[i]/(double)arr[i].length);
      
       System.out.println("\nMax of rows: ");
       for(int i=0;i<maxOfRow.length;i++)
           System.out.print(maxOfRow[i]+" ");
       System.out.println("\nMin of rows: ");
       for(int i=0;i<minOfRow.length;i++)
           System.out.print(minOfRow[i]+" ");
      
   }

   private static int[] getSumOfRow(int[][] arr) {
       int res[] = new int[arr.length];
       for (int i = 0; i < arr.length; i++) {
           int sum = 0;
           for (int j = 0; j < arr[i].length; j++)
               sum = sum + arr[i][j];
           res[i] = sum;
       }
       return res;
   }

   private static int[] getMaxOfRow(int[][] arr) {
       int res[] = new int[arr.length];
       for (int i = 0; i < arr.length; i++) {
           int m = arr[i][0];
           for (int j = 0; j < arr[i].length; j++) {
               if (m < arr[i][j])
                   m = arr[i][j];
           }
           res[i] = m;
       }
       return res;
   }

   private static int[] getMinOfRow(int[][] arr) {
       int res[] = new int[arr.length];
       for (int i = 0; i < arr.length; i++) {
           int m = arr[i][0];
           for (int j = 0; j < arr[i].length; j++) {
               if (m > arr[i][j])
                   m = arr[i][j];
           }
           res[i] = m;
       }
       return res;
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Write a method that takes in a two-dimensional array of integers, do some calculation for each...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • how do i do this in basic java without using hashmat or anything complicated Two-Dimensional Arrays....

    how do i do this in basic java without using hashmat or anything complicated Two-Dimensional Arrays. Write the three functions described below. Demonstrate that your code works by calling each function and printing out the results. All of your code should be in one file. Upload your java code and a file containing the output of running your program to show that it works. Write a function called create2DIntArray that creates a two-dimensional array of integers between 0 and 10....

  • Write a function that takes an array of integers as an argument and returns a value...

    Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y The signature of the function is: int f(int[ ] a) Examples if input array is return {1} 1 {1, 2} -1 {1,...

  • JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers,...

    JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers, and returns the sum of all the odd numbers in the array. The method should return 0 if the array contains no odd numbers. For example, if the given array is [12, 10, 5, 8, 13, 9] then the method should return 27 (5+13+9=27). Starter Code: public class Odds { public static int sumOdds(int[] numbers) { //TODO: complete this method    } }

  • Write a method that takes two arrays of integers and return and array that is the...

    Write a method that takes two arrays of integers and return and array that is the union of both arrays. For instance, the first array contains (1,2,3), the second array(4,5,6), the returned array should contain (1,2,3,4,5,6) (3.5 points) Write a method that takes and an array and return the reverse of that array. For instance, if the array is (1,2,3), the returned array should be (3,2,1) (3.5 points)

  • Write a piece of code that constructs a two-dimensional array of integers with 5 rows and...

    Write a piece of code that constructs a two-dimensional array of integers with 5 rows and 10 columns. Fill the array with a multiplication table, so that array element [i][j] contains the value i * j. Use nested for loops to build the array. java Program

  • C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

    C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • java code Write a method called reverse that takes an array of integers as an input...

    java code Write a method called reverse that takes an array of integers as an input and returns the same values in reverse order as the output. Test your method in the main.

  • Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT