Question

Assume arr2 is declared as a two-dimensional array of integers. Which of the following segments of...

Assume arr2 is declared as a two-dimensional array of integers. Which of the following segments of code successfully calculates the sum of all elements arr2?

  1. int sum = 0;
    for(int j = 0; j < arr2.length; j++)
    {
       for(int k = 0; k < arr2[j].length; k++)
       {
          sum += arr2[k][j];
       }
    }
  2. int sum = 0;
    for(int j = arr2.length − 1; j >= 0; j−−)
    {
       for(int k = 0; k < arr2[j].length; k++)
       {
          sum += arr2[j][k];
       }
    }
  3. int sum = 0;
    for(int[] m : arr2)
    {
       for(int n : m)
       {
          sum += n;
       }
    }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

2 and 3

2)

int sum = 0;
for(int j = arr2.length − 1; j >= 0; j−−)
{
   for(int k = 0; k < arr2[j].length; k++)
   {
      sum += arr2[j][k];
   }
}

3)

int sum = 0;
for(int[] m : arr2)
{
   for(int n : m)
   {
      sum += n;
   }
}

Add a comment
Know the answer?
Add Answer to:
Assume arr2 is declared as a two-dimensional array of integers. Which of the following segments of...
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
  • Write a java code that Declares and initialize a two-dimensional int array named grades. It should...

    Write a java code that Declares and initialize a two-dimensional int array named grades. It should have 10 rows and 6 columns where it stores the values and then calculates the average of all the elements in the grades array that you have declared

  • Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array...

    Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array is randomly initialized (using Math.random()). Write a method to find the maximum value in this two dimensional array; Write a method to find the average value in the array; Write a method to count how many elements are smaller than average; Write a method to copy contents in a two-dimensional array to a one-dimensional array. The method will return this one-dimensional array. Method is...

  • Use the following code, which initializes an array x with 1000 random integers, to answer the...

    Use the following code, which initializes an array x with 1000 random integers, to answer the questions a and b. java.util.Random generator = new java.util.Random( );//accessing Random class final int MAX = 1000; int[] x = new int[MAX]; for( int j = 0; j < MAX; j++ ) { x[j] = generator.nextInt( MAX ) + 1; } a. Write partial code to print all array elements of x in reverse order (one array element per line) b. Write partial code...

  • Which of the following segments of code will display the first row of a two dimensional...

    Which of the following segments of code will display the first row of a two dimensional array? -for (int i = 0; i < anArray.GetLength(1); i++) Console.Write(anArray[0, i] + “\t”); - for (int i = 0; i < anArray.GetLength(0); i++) Console.Write(anArray[0, i] + “\t”); -for (int i = 0; i < anArray.GetLength(0); i++) Console.Write(anArray[i, 0] + “\t”); - for (int i = 0; i < anArray.GetLength(1); i++) Console.Write(anArray[i, 0] + “\t”);

  • Write array methods that carry out the following tasks for an array of integers by completing...

    Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, provide a test program. public class ArrayMethods { private int[] values; public ArrayMethods(int[] initialValues) { values = initialValues; } public void swapFirstAndLast() { . . . } public void shiftRight() { . . . } .. . } a. Swap the first and last elements in the array. b. Shift all elements to the right by one...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • 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...

  • Write a C++ function printArray(A, m, n) that prints an m × n two dimensional array...

    Write a C++ function printArray(A, m, n) that prints an m × n two dimensional array A of integers, declared to be “int** A,” to the standard output. Each of the m rows should appear on a separate line.

  • What is the output from each of the following segments of C++ code? Record the output...

    What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 25; j > 16; j -= 3)        cout << setw(5) << j;     Answer: 2. int sum = 0;       for (int k = -2; k <= 2; k++)         sum = sum +...

  • please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class...

    please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class Chapter9a_FillInTheCode { public static void main(String[] args) { String [][] geo = {{"MD", "NY", "NJ", "MA", "CA", "MI", "OR"}, {"Detroit", "Newark", "Boston", "Seattle"}}; // ------> exercise 9a1 // this code prints the element at row at index 1 and column at index 2 // your code goes here System.out.println("Done exercise 9a1.\n"); // ------> exercise 9a2 // this code prints all the states in the...

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