Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me finish number 5-10 in this problem A java program that reads elements (randomly from 0 - 9) of a 2-dimensional array

Can someone help me finish number 5-10 in this problem

A java program that reads elements (randomly from 0 - 9) of a 2-dimensional array (5x5) using the Random Class then perform the following:

1) Output the array elements

2) Output the sum of each row.

3) Output the sum of each column.

4) Output the sum of all the elements.

5) Output the sum of prime numbers in the array

6) Output the sum of the elements in the main diagonal.

7) Output the elements below the diagonal.

8) Output the elements above diagonal.

9) Output the sum of odd numbers below the diagonal.

10) Output the sum of even numbers above the diagonal

This is my program so far.

import java.util.Random;

public class TwoDimensionalArrayAlgorithms_Valdez {

public static void main(String[] args) {

Random temp = new Random();

int twoD [][]=new int[5][5];

int i, j, sumTotal, sumRow, sumColumn;

System.out.println("1. Output the array Elements");

System.out.println("The array elements");

//allocating the elements of a 5 by 5 array using the random class

for(i=0; i<5; i++){

for(j=0; j<5; j++){

twoD[i][j] = temp.nextInt(10);

}

}

//access each of the indices in the array then display the array elements

for(i=0; i<5; i++) {

for(j=0; j<5; j++){

System.out.print(twoD[i][j] + " ");

}

System.out.println();

}

//Displays the sum of each row of the 2-dimensional array

//This also holds the sum of all elements of the array

System.out.println(" 2. Output the sum of each row ");

sumTotal = 0;

for(int x = 0; x

sumRow=0;

for(int y = 0; y

sumTotal =sumTotal + twoD[y][x];

sumRow = sumRow+ twoD[x][y];

}

System.out.println("The sum of row " + (x+1) + " is " + sumRow);

}

//Displays the sum of each column of the 2-dimensional array

System.out.println(" 2. Output the sum of each column ");

for(int x = 0; x

sumColumn=0;

for(int y = 0; y

sumColumn = sumColumn+ twoD[y][x];

}

System.out.println("The sum of column " + (x+1) + " is " + sumColumn);

}

//Displays the sum of all the elements in the array

System.out.println(" 3. Output the sum of all the elements ");

System.out.println("The sum of all the elements is " + sumTotal);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions

Question

Write a note on Historical Development of clinical Trials?

Answered: 1 week ago